简体   繁体   English

ViewDidLoad第二次未调用Xcode

[英]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.

相关问题 每次在xCode 9.4和iOS 11中调用的ViewDidLoad()方法 - ViewDidLoad() method called every time in xCode 9.4 and iOS 11 每次调用ViewdidLoad - ViewdidLoad called every time 实际上,每次有segue转换时都会调用viewDidLoad - viewDidLoad is in fact called every time there is a segue transition 在UIImagePickerController()的dismiss()之后每次调用viewDidLoad()。 - viewDidLoad() is called every time after dismiss() of UIImagePickerController() viewDidLoad中的代码每次调用时都会运行 - Code in viewDidLoad runs every time it is called 为什么没有调用我对 viewDidLoad 的第二次覆盖? - Why doesn’t my second override of viewDidLoad get called? didUpdateToLocation没有第二次调用 - didUpdateToLocation not called second time 调用awakeFromNib,不调用viewDidLoad - awakeFromNib is called, viewDidLoad is not called 每次将子级添加到firebase数据库时,都会调用viewDidLoad - viewDidLoad getting called every time a child is added to firebase database 在调用初始ViewController ViewDidLoad时,CoreData和NSUserDefaults是否始终会加载? - Will CoreData and NSUserDefaults have always loaded by the time the initial ViewController ViewDidLoad is called?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM