【问题标题】:How to add alert 'if data parameter = nil'如何添加警报'如果数据参数 = nil'
【发布时间】:2014-06-19 00:21:15
【问题描述】:

您好,如果我无法连接到 Web 服务,我正在尝试创建警报。本质上,如果我看到 'data parameter is 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];

感谢您的任何建议。

【问题讨论】:

    标签: mysql ios uialertview nserror


    【解决方案1】:

    所以,逐字逐句:

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

    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
    }
    

    【讨论】:

    • 另外一个相关问题。因此,如果我的 hostTextField.text 错误,这很有效。但如果它是正确的(即使密码错误),它也不会加载任何内容,也不会崩溃。无论如何要解决这个问题?
    • FIXED:我在 json 之后添加了另一个警报。 if(json == nil) { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Camaleon Alert" message:@"Incorrect Host/Pass" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; [警报显示];返回; } 其他 {
    猜你喜欢
    • 1970-01-01
    • 2022-10-01
    • 1970-01-01
    • 2021-07-22
    • 1970-01-01
    • 2011-07-06
    • 2011-05-13
    • 2017-01-19
    • 2019-07-20
    相关资源
    最近更新 更多