如何在值为null时显示警报

时间:2012-08-07 08:23:43

标签: iphone objective-c ipad uialertview

我想在值为null时显示警告我有titleCategory如果它的值为null则它应该显示警告

NSString*test=titleCategory;
if ([titleCategory isEqualToString:nil]) 
{
    UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Alert" message:@"Select Category " delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alert show];
    [alert release];
}
else
{
    FirstViewController*targetController=[[FirstViewController alloc]init];
    [self.navigationController pushViewController:targetController animated:YES];
}

3 个答案:

答案 0 :(得分:1)

if(test == nil)
{
    UIAlertView* alert = [[[UIAlertView alloc] initWithTitle:@"" message:@"Test is null" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil] autorelease];
    [alert show];
}

答案 1 :(得分:0)

这样做:

if (!(titleCategory.length > 0) || titleCategory == nil ) 
{
  UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Alert" message:@"Select Category " delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
  [alert show];
  [alert release];
}
else 
{
   FirstViewController*targetController=[[FirstViewController alloc]init];
   [self.navigationController pushViewController:targetController animated:YES];
   [targetController release]; // if not ARC 
}

答案 2 :(得分:0)

请使用此代码:

if([titleCategory.text isKindOfClass:[NSNull class]]){
    UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Alert" message:@"Select 
    Category " delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alert show];
    [alert release];
  }
  else{
     FirstViewController*targetController=[[FirstViewController alloc]init];
     [self.navigationController pushViewController:targetController animated:YES];
     [targetController release]; // if not ARC 
  }