ViewDidLoad is slow when you click the viewcontroller

I tried to push viewcontroller B to the navigation controller from A and then assign some properties of B to A.
In this case, a property assignment was assigned, and then viewDidLoad of view manager A was executed.

Here, property assignment in should only be done after viewDidLoad from A.

For instance,

[b.navController pushViewController:a animated:YES]; a.status = @"loaded"; 

Here, at first status was assigned, and then viewDidLoad was executed for A. This only happens in iOS 7, whereas in iOS6 it works fine.

Can anyone tell me where the problem is?

UPDATE: for me in some cases in iOS7, Push view does not work. How to cna debug and fix it?

+6
source share
8 answers

Just access the viewcontroller.view (set any thing immediately after the alloc property) after initializing the alloc;

What will be loadview / viewdidload.
See Apple documentation

+1
source

In my experience, the UIViewController loads lazily, regardless of which version of iOS you are running. If you want to initiate the loading of a view, and therefore its UIViewController viewDidLoad , you must access the view after allocating the UIViewController. For instance:

 UIViewController *aViewController = [[CustomViewController alloc] init]; [aViewController view]; 

Make sure you do not encode it as

 aViewController.view 

as this will generate a compiler warning.

So in your case it should be something like this:

 ... CustomViewController *a = [[CustomViewController alloc] init]; [b.navController pushViewController:a animated:YES]; [a view]; a.status = @"loaded"; 

Let me know if you have additional problems.

+1
source

You can know when the view controller was self.navigationController.delegate = self; stack by implementing UINavigationControllerDelegate and setting itself as a delegate self.navigationController.delegate = self; then you will get this callback after every click

 navigationController:didShowViewController:animated: 

So, you can check if the displayed viewController is the one you are interested in, and then install a.status.

0
source

Please write the code in viewWillAppear instead of viewDidLoad in the following ie class, where you click the object on

 -(void)viewWillAppear:(BOOL)animated { } 
0
source

The documentation does not have enough information to know exactly when viewDidLoad will be called.

The UIViewController documentation just says it

This method is called after the view controller has loaded its view hierarchy into memory.

I would suggest creating a custom initializer like this

 - (id)initWithStatus:(NSString *)status { } 

But if you are trying to use this variable to check whether the viewController has been β€œloaded” or not, it may not be possible because the pushViewController or presentViewController methods are not guaranteed to be synchronous.

Even in iOS 6, there was no explicit guarantee that the view would be loaded immediately after returning this method.

0
source

I suggest you call the delegate method after loading the view. Set the delegate as controller B. and after viewDidLoad (in controller A) is completed, call the delegate method. You can even pass parameters as you wish to the delegate.

Here is a sample code:

Controller B:

 a.delegate = self; [b.navigationController pushViewController:a animated:YES]; 

Implement delegate method:

 - (void)controllerIsLoaded:(ControllerA *)controllerA status:(NSString *)status { a.status = status; } 

Controller Ah File:

 @class ControllerA; @protocol ControllerADelegate <NSObject> - (void)controllerIsLoaded:(ControllerA *)controllerA status:(NSString *)status; @end @interface ControllerA : UIViewController @property (nonatomic, weak) id <ControllerADelegate> delegate; 

Am file controller:

 - (void)viewDidLoad:(BOOL)animated { [super viewDidLoad:animated]; /* your viewDidLoad code here... */ if ([_delegate respondsToSelector:@selector(controllerIsLoaded:status)]) [_delegate controllerIsLoaded:self status:@"Loaded"]; } 
0
source

Turn off animation for ios7, in my case it works

 [b.navController pushViewController:a animated:NO]; a.status = @"loaded"; 
0
source

I understand your question as follows:

 B *b = [[B alloc] init]; b.status = @"loaded"; [self.navigationController pushViewController:b animated:Yes]; 

If you want to transfer a value from one controller to another, you need to assign a value before using the pushViewController method.

-1
source

Source: https://habr.com/ru/post/955153/


All Articles