-1

我正在尝试开发一种使最喜欢的收藏夹完美运行的功能,但是当我按下下一个或上一个按钮时,它会崩溃...

我不知道如何检查 FavouriteArray 中是否存在当前消息..?if is Exist Then FavouriteButton images 更改为 Favourite-ON.png 否则其图像 Favourite-OFF.png..

我的代码在下面===================

    // Adding Current Message to Favourite Array

    - (IBAction)FavouritebtnClick:(id)sender  //Favourite Button Work Perfectly 
    {

        Make_Fav_Text = [NSString stringWithFormat:@"%@",[[[TabBarTutorialAppDelegate shareDelegate].level_array valueForKey:@"SMS"] objectAtIndex:row_no]];

        NSLog(@"Make Favourite TExt====%@",Make_Fav_Text);

        [DatabaseFiles InsertFav:[NSString stringWithFormat:@"%@",Make_Fav_Text]];//insert to database

        [FavouriteBtnChange setImage:[UIImage imageNamed:@"FAv_On-iphone.png"] forState:UIControlStateNormal];
    }

// 检查当前消息在 FavouriteArray 中是否可用

-(void)Check_Fav_Available
{
    for (NSString *Myfav in Check_Fav_Avail)
    {
        NSLog(@"MYFAV===%@",Myfav);
        if ([Myfav isEqual:sms.text])
        {
            [FavouriteBtnChange setImage:[UIImage imageNamed:@"FAv_On-iphone.png"] forState:UIControlStateNormal];
            NSLog(@"Found Favourite");

            //break;
        }
        else{
            NSLog(@"NOt Found");
            [FavouriteBtnChange setImage:[UIImage imageNamed:@"FAv_On-copy-iphone"] forState:UIControlStateNormal];
        }
    }

}

//Next Button 

- (IBAction)Nextbtn:(id)sender
{


    row_no=row_no+1;
    lblno=row_no;
    lblno=lblno+1;

   [self Check_Fav_Available]; //Check Current Message in Favourite Array;

    if (row_no==[TabBarTutorialAppDelegate shareDelegate].level_array.count)
    {
        row_no=row_no-1;
//        lblno=lblno-1;
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alert" message:@"This is last SMS" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [alert show];
        NSLog(@"this is last");
        NSLog(@"row no is %d",row_no);
    }
    else
    {
        if (row_no <=4)
        {
            sms.text=[[[TabBarTutorialAppDelegate shareDelegate].level_array objectAtIndex:row_no] objectForKey:@"SMS"];
            NSString *str = [NSString stringWithFormat:@"%d - %d",lblno,[TabBarTutorialAppDelegate shareDelegate].level_array.count];
            no_lbl.text=str;

            NSLog(@"row no is %d",row_no);
           [self Check_Fav_Available];


        }
        else
        {
            UIAlertView *Purchasealert = [[UIAlertView alloc] initWithTitle:@"Purchase Category" message:@"Purchase to Enjoy More" delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:@"Buy Message", nil];
            Purchasealert.tag=1710;
            [Purchasealert show];

            [self gostore];
            NSLog(@"this is last");
            NSLog(@"row no is %d",row_no);

        }
     }

}


//Previous Button

- (IBAction)Previousbtn:(id)sender
{

    //[FavouriteBtnChange setImage:[UIImage imageNamed:@"FAv_On-copy-iphone.png"] forState:UIControlStateNormal];

    row_no=row_no-1;
    lblno=lblno-1;

    if (row_no<0)
    {
        row_no=row_no+1;
        lblno=lblno+1;

        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alert" message:@"This is First SMS" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [alert show];
        NSLog(@"this is last");
    } 
    else
    {
        [self Check_Fav_Available]; //Check Favourite available

        sms.text=[[[TabBarTutorialAppDelegate shareDelegate].level_array objectAtIndex:row_no] objectForKey:@"SMS"];

        NSString *str = [NSString stringWithFormat:@"%d - %d",lblno,[TabBarTutorialAppDelegate shareDelegate].level_array.count];
        no_lbl.text=str;
        [self Check_Fav_Available];

        NSLog(@"arr is %d",[TabBarTutorialAppDelegate shareDelegate].level_array.count);
        NSLog(@"row no is %d",row_no);
    }

}
4

3 回答 3

0

试试这个代码......

-(void)Check_Fav_Available
    {

            if (!([Check_Fav_Avail indexOfObject:sms.text]==NSNotFound))
            {
                [FavouriteBtnChange setImage:[UIImage imageNamed:@"FAv_On-iphone.png"] forState:UIControlStateNormal];
                NSLog(@"Found Favourite");

                //break;
            }
            else
              {
                NSLog(@"NOt Found");
                [FavouriteBtnChange setImage:[UIImage imageNamed:@"FAv_On-copy-iphone"] forState:UIControlStateNormal];
            }


    }
于 2013-10-25T07:11:48.110 回答
0

用这个来比较...

if ([Myfav isEqualToString:sms.text])
        {
            [FavouriteBtnChange setImage:[UIImage imageNamed:@"FAv_On-iphone.png"] forState:UIControlStateNormal];
            NSLog(@"Found Favourite");

            //break;
        }

这可能是错误的..

Make_Fav_Text = [NSString stringWithFormat:@"%@",[[[TabBarTutorialAppDelegate shareDelegate].level_array valueForKey:@"SMS"] objectAtIndex:row_no]];

相反,使用这个...

Make_Fav_Text = [NSString stringWithFormat:@"%@",[[[TabBarTutorialAppDelegate shareDelegate].level_array objectAtIndex:row_no] valueForKey:@"SMS"]];
于 2013-10-25T07:12:04.073 回答
0

将您的代码更改为

for (NSString *Myfav in Check_Fav_Avail){
    NSRange favRange = [Myfav rangeOfString:sms.text options:NSCaseInsensitiveSearch];

    if (favRange.location != NSNotFound) {
        NSLog(@"Found");
    } else {
        NSLog(@"Not found");
    }
}
于 2013-10-25T07:07:50.530 回答