简体   繁体   中英

ViewDidLoad is not called for the second time Xcode

I have a tableViewController from which i navigate to UIViewController. To save the value of indexPath.row I have used an integer and based on that integer action is performed in UIViewController. Problem is that when I press the back button and selects another index of table, It navigates to UIViewContoller but the action performed is the first one. the ViewDidLoad is not called for the second time. here is my code.

TableView Code:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (!self.iaRecipieViewController) {
        self.iaRecipieViewController = [[[iRecipieViewController alloc] initWithNibName:@"iRecipieViewController" bundle:nil] autorelease];
    }
    if (indexPath.row == 0) {
        self.iaRecipieViewController.myLableArray =labelSoupArray ;
    }

    if (indexPath.row == 1) {
        self.iaRecipieViewController.myLableArray =labelSoupArray ;
    }

    if (indexPath.row == 2) {
        self.iaRecipieViewController.myLableArray =labelSoupArray ;
    }
    // custom string is an NSinteger
    self.iaRecipieViewController.customString = indexPath.row;
    NSLog(@"  int  %i",self.iaRecipieViewController.customString);
    NSLog(@"index path %i ", indexPath.row) ;

    [self.navigationController pushViewController:iaRecipieViewController animated:YES];

    [detailTable reloadData];
}

UIViewController Code.

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.

    myLabel.text = [self.myLableArray objectAtIndex:customString];
}

Use viewWillAppear:(BOOL)animated

-(void)viewWillAppear:(BOOL)animated {

  [super viewWillAppear:animated];
  myLabel.text = [self.myLableArray objectAtIndex:customString];

}

viewDidLoad will only get called when the view is loaded; pushing a new ViewController and then removing it won't force the view to be reloaded. viewWillAppear is called before the view is rendered, so you have an opportunity to do things whenever it becomes the primary view.

ViewDidLoad gets called only once when the view is instantiated. Based on your code, you are going back and forth on this view and as such the view is not instantiated (loaded) everytime but rather it was loaded once and now it is really the view disappearing and appearing as you go back and forward to that view.

Put your code in ViewWillAppear or ViewDidAppear.

The method

(void)viewDidLoad

is run when the view gets loaded. If your view already loaded(means it's still in the memory),maybe it's gone from the screen. But it doesn't mean that the memory is gone; Just re-Run when the view should load to the memory.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM