簡體   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