如何检查NsmutableArray中可用的当前字符串..?

时间:2013-10-25 07:00:41

标签: ios iphone nsmutablearray

我正在努力开发一个让人感到满意的功能 最喜欢的是工作完美但当我按下一个或上一个按钮时它的崩溃...

我不知道如何检查当前消息是否存在于FavouriteArray中? 如果是Exist然后FavouriteButton图像改为Favorite-ON.png否则它的图像Favorite-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);
    }

}

3 个答案:

答案 0 :(得分: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");
    }
}

答案 1 :(得分: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];
            }


    }

答案 2 :(得分: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"]];