如果数据参数= nil,如何添加警报

时间:2014-06-19 00:21:16

标签: mysql ios uialertview nserror

您好,如果我无法连接到网络服务,我会尝试创建警报。基本上,如果我看到'数据参数为nil' ,那么我想要一个警告说"无法连接,并返回。

这是我正在使用的代码。

- (IBAction)search:(id)sender
{


 if ([hostTextField.text isEqualToString:@""] || [passwordTextField.text isEqualToString:@""]) {
 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Camaleon Alert" message:@"All fields must be filled in" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
 [alert show];


 }else{




NSString *requestStr = @"http://";
requestStr = [requestStr stringByAppendingString:hostTextField.text];
requestStr = [requestStr stringByAppendingString:@":8080/dblist.php?password="];
requestStr = [requestStr stringByAppendingString:passwordTextField.text];


NSURL * url = [NSURL URLWithString:requestStr];
NSData * data = [NSData dataWithContentsOfURL:url];


json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];


 DBList = [[NSMutableArray alloc]init];

 for (int i = 0; i < json.count; i++)
 {
 //Create the objects

 NSString * dbName = [[json objectAtIndex:i] objectForKey:@"Database"];


 //------------------------------------------

 Database * myDatabases = [[Database alloc]initWithDatabaseName: (NSString *) dbName];

 //Add the object to the array


 [DBList addObject:myDatabases];

 }

 [self.DBTable reloadData];

感谢您的任何建议。

1 个答案:

答案 0 :(得分:1)

所以,逐字逐句地说:

“基本上,如果我看到'数据参数为零',那么我想要一个警告说”无法连接,然后返回。“

if(data == nil)
{
     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Connection Problem" message:@"not able to connect" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
     [alert show];
     return;
}
else
{
    //The rest of your code
}