[英]ViewDidLoad is not called for the second time Xcode
I have a tableViewController from which i navigate to UIViewController. 我有一个tableViewController,从中我可以导航到UIViewController。 To save the value of indexPath.row I have used an integer and based on that integer action is performed in UIViewController. 为了保存indexPath.row的值,我使用了一个整数,并基于该整数在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. 问题是当我按下“后退”按钮并选择表的另一个索引时,它导航到UIViewContoller,但是执行的操作是第一个。 the ViewDidLoad is not called for the second time. ViewDidLoad不会第二次被调用。 here is my code. 这是我的代码。
TableView Code: 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 Code. UIViewController代码。
- (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
使用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; viewDidLoad仅在加载视图时被调用; pushing a new ViewController and then removing it won't force the view to be reloaded. 推送一个新的ViewController然后将其删除不会强制重新加载视图。 viewWillAppear is called before the view is rendered, so you have an opportunity to do things whenever it becomes the primary view. viewWillAppear在呈现视图之前被调用,因此只要它成为主视图,您就有机会做一些事情。
ViewDidLoad gets called only once when the view is instantiated. 实例化视图时,ViewDidLoad仅被调用一次。 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. 将您的代码放在ViewWillAppear或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. 当视图应该加载到内存时,只需重新运行即可。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.