我想在值为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];
}
答案 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
}