Viewwillappear and viewdidappear are not called

This question is very frequent, but I can not solve it with any available answers.

I am working on iOS 5.1. My navigation controller is one tab between tab view controllers. There is a table view in which row selection pushes new view controllers.

This problem occurs only when the second row is selected, and only occasionally. This is not regular. The pushed view is empty - viewWillAppear / viewDidAppear not called. When you click the back button on the navigation bar, the root view viewWillAppear / viewDidAppear also not called, making it empty.

I click the view to select the first row / second row in the same way. But the problem arises only in the second line.

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ switch (indexPath.row) { case 0: AViewController *aObj = [[AViewController alloc] init]; aObj.homeObj = self; [self.navigationController pushViewController:aObj animated:YES]; [aObj release]; break; case 1: BViewController *bVCObj = [[BViewController alloc] init]; bVCObj.homeObj = self; [self.navigationController pushViewController:bVCObj animated:YES]; [bVCObj release]; break; default: break; } [tableView deselectRowAtIndexPath:indexPath animated:YES]; } 

I tried this and this , but in vain.

  • viewDidLoad is called when viewDidLoad is clicked, however, viewWillAppear and viewDidAppear not called. Below is my viewDidLoad :

     - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor blackColor]; NSLog(@"nav stack: %@", [self.navigationController viewControllers]); NSLog(@"nav stack: %@", [[self.navigationController visibleViewController] description]); //some initialization and call of methods } 
  • This is not regular. Sometimes I get this script, and it continues until I close the application from the background and restart it. But sometimes it works fine. I just push my view controller onto the nab stack.

  • As I mentioned in the commentary, this is a regular navigation controller in the tab bar controller.

+4
source share
1 answer

How do you define your views for AViewController and BViewController ? Usually you use initWithNibName , for example.

 AViewController *aObj = [[AViewController alloc] initWithNibName:@"mynibname" bundle:nil]` 

As Carl noted, you can only use init (although I don’t see this class documented in the UIViewController Reference class ), but then the system will be very specific with respect to the name of your NIB file. The documentation does say that you can use initWithNibName and pass nil for the NIB name, in which case it will try to find it for you. Personally, if you have inconsistent results, I will try to use initWithNibName and explicitly pass the name of your NIB and see if this fixes the situation.

Or do you create your view programmatically using loadView in two controllers? Then you need to show us those loadView procedures (not to be confused with viewDidLoad ).

But according to the documentation, you need to either specify your NIB or use loadView . See Discussion Viewing Management in the UIViewController Link to a Class.

Update:

Given your feedback, I have a few thoughts:

  • Needless to say, the problem is apparently not related to the code above. You need to expand your search and show us more code. Perhaps show us your viewDidLoad from B?

  • Usually, when you do not receive such events, this is due to the fact that the hierarchy of the view manager is not synchronized with the hierarchy of the view. The most common way people do this is to do something like β€œ [addSubview someNewController.view] ” at some point. If you use the view controller in any context than (a) the initial configuration of the application delegate; (b) presentViewController (or termination); or (c) pushViewController (or pop), then you can share what you have done.

  • As andreamazz noted, your comment: β€œMy navigation controller is inside the tab view controller's view controller,” is a little worrying if you read it literally. You can put the navigation bar in the view controller view, but you cannot put the navigation controller in the view controller (unless you close the controller controller, which is a completely different beast). Similarly, in another question, you said: "Embedding a UINavigationController or UITabBarController (my case) in the UIViewController somehow interrupts the call to these methods." The fact is that you do not embed navigation controllers in other view controllers (if it is not itself, a container controller, such as a tab view controller), but vice versa. But if you literally mean that you have a controller that contains a navigation controller, you must show us how you do it (the correct containment of the controller?), Because it is very unusual.

  • This is unusual, but I had projects that were damaged, ending up in strange conditions. At a minimum, I can offer a β€œProduct” - β€œClean” and rebuild. If the problem persists and you have identified the problem for the B NIB, then temporarily rename it and create a quick and dirty one from scratch.

0
source

All Articles