0

这是我的 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];
    }
}

请问有什么想法吗?

4

1 回答 1

2

你应该把你自己的方法放在PHP Data Code一个方法中,所以当你需要专门调用它时,你可以,因为你不能(或者我应该说不应该viewDidLoad手动调用。那应该只由 自动调用UIViewController

例子

- (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;
}

-(void)viewWillAppear:(BOOL)animated    /* or: -(void)viewDidAppear: */
{
    [super viewWillAppear:animtaed];

    [self refreshPHData];
}

-(void)refreshPHPData
{
    //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];
    }
}

此外,当您viewDidLoad从调用时UIAlertView,您正在创建第二个(以及第三个等等)adview,因为您没有删除前一个实例。根据显示和viewDidLoad调用警报的次数,您将看到 adview 堆叠在其自身之上。所以试试吧:

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

    if (buttonIndex == 1){
        [self refreshPHPData];
    }
}
于 2012-07-08T20:11:22.183 回答