繁体   English   中英

ViewDidLoad第二次未调用Xcode

[英]ViewDidLoad is not called for the second time Xcode

我有一个tableViewController,从中我可以导航到UIViewController。 为了保存indexPath.row的值,我使用了一个整数,并基于该整数在UIViewController中执行操作。 问题是当我按下“后退”按钮并选择表的另一个索引时,它导航到UIViewContoller,但是执行的操作是第一个。 ViewDidLoad不会第二次被调用。 这是我的代码。

TableView代码:

- (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代码。

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

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

使用viewWillAppear:(BOOL)animated

-(void)viewWillAppear:(BOOL)animated {

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

}

viewDidLoad仅在加载视图时被调用; 推送一个新的ViewController然后将其删除不会强制重新加载视图。 viewWillAppear在呈现视图之前被调用,因此只要它成为主视图,您就有机会做一些事情。

实例化视图时,ViewDidLoad仅被调用一次。 根据您的代码,您将在该视图上来回移动,因此该视图并非每次都实例化(加载),而是被加载一次,现在实际上是视图在您前进和后退时消失并出现视图。

将您的代码放在ViewWillAppear或ViewDidAppear中。

方法

(void)viewDidLoad

在加载视图时运行。 如果您的视图已经加载(意味着它仍在内存中),则可能已从屏幕上消失了。 但这并不意味着内存已消失。 当视图应该加载到内存时,只需重新运行即可。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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