Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 连接失败后调用viewDidLoad不工作_Iphone_Ios_Viewdidload - Fatal编程技术网

Iphone 连接失败后调用viewDidLoad不工作

Iphone 连接失败后调用viewDidLoad不工作,iphone,ios,viewdidload,Iphone,Ios,Viewdidload,这是我的viewDidLoad函数,运行良好: - (void)viewDidLoad { [super viewDidLoad]; //iAds code adview = [[ADBannerView alloc] initWithFrame:CGRectZero]; adview.frame = CGRectOffset(adview.frame, 0, 0); adview.requiredContentSizeIdentifiers = [NS

这是我的viewDidLoad函数,运行良好:

- (void)viewDidLoad
{
    [super viewDidLoad];
    //iAds code
    adview = [[ADBannerView alloc] initWithFrame:CGRectZero];
    adview.frame = CGRectOffset(adview.frame, 0, 0);

    adview.requiredContentSizeIdentifiers = [NSSet setWithObject:ADBannerContentSizeIdentifierPortrait];
    adview.currentContentSizeIdentifier = ADBannerContentSizeIdentifierPortrait;
    [self.view addSubview:adview];
    adview.delegate = self;
    self.adbanerinvesiable = NO;

    //php data code
    NSError *er;
    SBJsonParser *parser = [[SBJsonParser alloc] init];
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://localhost:8888/newjson.php?number=1"]];
    NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    if (response.length != 0){
        NSString *json_string = [[NSString alloc]initWithData:response encoding:NSUTF8StringEncoding];
        listt = [parser objectWithString:json_string error:&er];
    }
    else {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"No Internet conniction" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:@"Refresh", nil];
        [alert show];

        //for ID contet post 
        SBJsonParser *parser2 = [[SBJsonParser alloc] init];
        NSURLRequest *request2 = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://localhost:8888/newjson.php?number=2"]];
        NSData *response2 = [NSURLConnection sendSynchronousRequest:request2 returningResponse:nil error:nil];
        NSString *json_string2 = [[NSString alloc]initWithData:response2 encoding:NSUTF8StringEncoding];
        IDNum = [parser2 objectWithString:json_string2 error:&er];
    }
}
如您所见,如果连接失败,它将显示UIAlertView和两个按钮{ok,Refresh}

如果单击“刷新”按钮调用viewDidLoad但不工作,并给我一个空白的白色视图,则我使用此方法:

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{

    if (buttonIndex == 1){
        [self viewDidLoad];
    }
}

有什么想法吗?

你应该把你的
PHP数据代码
放在它自己的方法中,所以当你需要特别调用它时,你可以,因为你不能(或者我应该说不应该)手动调用
viewDidLoad
。只能由
UIViewController
自动调用

例子 此外,当您从
UIAlertView
调用
viewDidLoad
时,您正在创建第二个
adview
,因为您没有删除上一个实例。根据警报显示的次数和调用的
viewDidLoad
,您将看到adview堆叠在其顶部。因此,请尝试:

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{

    if (buttonIndex == 1){
        [self refreshPHPData];
    }
}

此外,每次移动都会要求下载数据,这会消耗大量手机数据,并使应用程序速度变慢,从而更详细地解释什么“不起作用”。这段代码对您有什么作用,对您没有什么作用?它的作用基本上与您最初的作用相同,只是每次用户选择刷新时,您都不会重新创建
adview
。通过从警报中调用
viewDidLoad
,您仍在下载数据。然后在refreshPHPData中,您需要刷新tableView。添加
[tableView重载数据]不过要注意,您的问题中没有提到表。您需要用表视图的实例名称替换tableView。例如
myTable
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{

    if (buttonIndex == 1){
        [self refreshPHPData];
    }
}