Iphone UIAlertView将不显示
我有一个程序正在请求一个JSON字符串。我创建了一个类,其中包含下面的connect方法。当根视图出现时,它向这个类和方法发出请求,为根视图加载一些数据。当我测试错误代码时(通过将URL主机更改为127.0.0.1),我希望会显示警报。其行为是根视图变为黑色,应用程序在没有警报的情况下中止。控制台上的调试模式中也没有错误。对这种行为有什么想法吗?我已经四处寻找了好几个小时的线索,但都没有结果。提前感谢你的帮助 注意:将调用条件for(error)以及UIAlertView代码Iphone UIAlertView将不显示,iphone,objective-c,ios4,Iphone,Objective C,Ios4,我有一个程序正在请求一个JSON字符串。我创建了一个类,其中包含下面的connect方法。当根视图出现时,它向这个类和方法发出请求,为根视图加载一些数据。当我测试错误代码时(通过将URL主机更改为127.0.0.1),我希望会显示警报。其行为是根视图变为黑色,应用程序在没有警报的情况下中止。控制台上的调试模式中也没有错误。对这种行为有什么想法吗?我已经四处寻找了好几个小时的线索,但都没有结果。提前感谢你的帮助 注意:将调用条件for(error)以及UIAlertView代码 - (NSStri
- (NSString *)connect:(NSString *)urlString {
NSString *jsonString;
UIApplication *app = [UIApplication sharedApplication];
app.networkActivityIndicatorVisible = YES;
NSError *error = nil;
NSURLResponse *response = nil;
NSURL *url = [[NSURL alloc] initWithString:urlString];
NSURLRequest *req = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:10];
NSData *_response = [NSURLConnection sendSynchronousRequest: req returningResponse: &response error: &error];
if (error) {
/* inform the user that the connection failed */
//AlertWithMessage(@"Connection Failed!", message);
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Oopsie!"
message:@"Unable to connect! Try later, thanks."
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles: nil];
[alert show];
[alert release];
} else {
jsonString = [[[NSString alloc] initWithData:_response encoding:NSUTF8StringEncoding] autorelease];
}
app.networkActivityIndicatorVisible = NO;
[url release];
return jsonString;
}将if(错误)
替换为if(!\u响应)
NSURLConnection
的sendSynchronousRequest
文档:
。。。。
错误
在处理请求时发生错误时使用Out参数。可能为空
返回值
URL请求的下载数据。如果无法创建连接或下载失败,则返回nil。调用[alert show]时UIWindow是否已经存在?UIAlertView show应该在视图层次结构中插入警报,但如果还没有窗口/根视图,则无处插入警报,因此[alert release]只是过早地将其释放。谢谢,我会更改它。但是,要明确的是,会调用条件以及UIAlertView代码。它只是中止了。啊,好的。原因可能是您的jsonString变量。它未初始化,在两种情况下都返回它(失败,成功)。尝试将第一行替换为“NSString*jsonString=nil;”。我明白了…我不知道显示警报不是模态的。因此,底层代码并没有停止,它抛出了一个错误,然后就死了。我找到地址了。感谢所有提出答案的人。