2013-03-04 89 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 

当视图被加载运行。 如果你的视图已经加载(意味着它仍然在内存中),也许它已经从屏幕上消失了。但这并不意味着记忆消失了;当视图加载到内存时只需重新运行。