0

this is my viewDidLoad function working well :

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

As you can see if the connection failed it will show UIAlertView and two Buttons {ok , Refresh}.

I used this Method in case the refresh button been clicked to call viewDidLoad But not Working and giving me blank white view:

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

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

Any idea please ?

WrightsCS
  • 50,551
  • 22
  • 134
  • 186
Ali Alzahrani
  • 529
  • 1
  • 6
  • 29

1 Answers1

2

You should put your PHP Data Code in a method in it's own, so when you need to call that specifically you can, because you can't (or should I say shouldn't) call viewDidLoad manually. That should only be called automatically by UIViewController.

Example

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

Also, when you call viewDidLoad from your UIAlertView, you are creating a second (and third and so forth) adview because you aren't removing the previous instance. Depending on how many times the alert is shown and viewDidLoad is called, you are going to see adview stacking on top of itself. So try instead:

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

    if (buttonIndex == 1){
        [self refreshPHPData];
    }
}
WrightsCS
  • 50,551
  • 22
  • 134
  • 186
  • also it asking to download data each move, and that's consume a lot of phone data and make the app slower – Ali Alzahrani Jul 08 '12 at 21:00
  • Explain more on what is "not working". What does this code do for you, what does it not do for you? – WrightsCS Jul 08 '12 at 21:00
  • Its basically the same affect that you originally have, only you are not recreating `adview` every time the user chooses **Refresh**. By calling `viewDidLoad` from the alert, you are still downloading the data. – WrightsCS Jul 08 '12 at 21:02
  • Then in refreshPHPData, you need to refresh the tableView. add `[tableView reloadData];` **Just to note though, you make no mention of a table in your question.** – WrightsCS Jul 08 '12 at 21:03
  • ok , but I am using UIViewController and when type [tableView reloadData]; it is giving me error . – Ali Alzahrani Jul 08 '12 at 21:07
  • 1
    You need to replace **tableView** with the instance name of YOUR table view. For example `myTable`. – WrightsCS Jul 08 '12 at 21:10