2013-03-04 80 views
0

我有一個tableViewController從中導航到UIViewController。爲了保存indexPath.row的值,我使用了一個整數,並基於該整數操作在UIViewController中執行。問題是,當我按下後退按鈕並選擇表的另一個索引時,它導航到UIViewContoller,但執行的操作是第一個。 ViewDidLoad不會再被調用。 這是我的代碼。ViewDidLoad不是第二次調用Xcode

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

回答

6

使用viewWillAppear:(BOOL)animated

-(void)viewWillAppear:(BOOL)animated { 

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

} 

視圖加載時viewDidLoad中只會被調用;推送新的ViewController然後刪除它不會強制重新加載視圖。 viewWillAppear在視圖呈現之前被調用,所以你有機會做任何事情,只要它成爲主視圖。

2

ViewDidLoad在實例化視圖時僅被調用一次。根據你的代碼,你在這個視圖中來回切換,因此視圖不是每次都被實例化(加載),而是被加載一次,現在當視圖消失並出現時,視圖。

將代碼放在ViewWillAppear或ViewDidAppear中。

2

方法

(void)viewDidLoad 

當視圖被加載運行。 如果你的視圖已經加載(意味着它仍然在內存中),也許它已經從屏幕上消失了。但這並不意味着記憶消失了;當視圖加載到內存時只需重新運行。