4

this question is very frequent, but I am not able to solve it with any answers available.

I am working on iOS 5.1. My navigation controller is one tab amongst tab bar view controllers. There's a tableview, in which selecting of a row pushes new view controllers.

This problem occurs Only on selecting of the second row and only sometimes. It's not regular. The Pushed view comes blank - viewWillAppear/viewDidAppear are not being called. On clicking the back button of the navigation bar - the root view's viewWillAppear/viewDidAppear are also not being called, making it blank.

I am pushing the view on select of first row/second row in exactly the same way. But the problem occurs only on the second row.

- (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 have tried this and this but in vain.

  1. viewDidLoad is being called on pushing the BViewController, However, viewWillAppear and viewDidAppear is not being called. Following 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
    }
    
  2. It's not regular. Sometimes I get this scenario, and this continues until I close the app from the background and restart it. But sometimes it works just fine. I am just pushing my view controller to the nab stack.

  3. As I mentioned in the comment, It's a regular navigation controller in tab bar controller.

Community
  • 1
  • 1
Nikita P
  • 4,226
  • 5
  • 31
  • 55
  • What do you mean by 'My navigation controller is inside a view controller of the tab bar controller'. The navcontroller should *be* one of the TabBar view controllers, you don't need to wrap it in another viewController. – andreamazz Sep 01 '12 at 07:45
  • 1
    i meant nav controller as a part of the tab bar controllers. :) – Nikita P Sep 01 '12 at 13:47

1 Answers1

0

How are you defining your views for AViewController and BViewController? Generally you'd use initWithNibName, e.g.

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

As Carl pointed out, you can apparently use just init (though I don't see this documented in the UIViewController Class Reference), but then the system will be very particular about the name of your NIB file. The documentation does say, though, that you can use initWithNibName and pass a nil for the NIB name, in which case it will try to find it for you. Personally, if you're having inconsistent results, though, I'd try using initWithNibName and explicitly pass the name of your NIB, and see if that rectifies the situation.

Or are you building your view programmatically with loadView in your two controllers? Then you need to show us those loadView routines (not to be confused with viewDidLoad).

But according to the documentation, you need to either specify your NIB or use loadView. See the View Management discussion in the UIViewController Class Reference.

Update:

Given your feedback, I have a couple of thoughts:

  1. Needless to say, the problem is apparently not related to the above code. You need to broaden you search and show us more code. Perhaps show us your viewDidLoad of B?

  2. Generally when you don't get these sorts of events, it's because the view controller hierarchy has gotten out of sync with the view hierarchy. The most common way that people do this is if they've done something like "[addSubview someNewController.view]" at some point. If you're using a view controller in any context either than (a) your app delegate's initial configuration; (b) presentViewController (or dismiss); or (c) pushViewController (or pop), then you might want to share what you've done.

  3. As andreamazz pointed out, your comment, "My navigation controller is inside a view controller of the tab bar controller," is a little disturbing if one reads it literally. You can put navigation bar in a view controller's view, but you can't put a navigation controller in a view controller (unless you're doing view controller containment, which is a whole different beast). Equally concerning is where, in another one of your questions, you said, "Embedding a UINavigationController or UITabBarController (my case) in a UIViewController somehow interrupts with the calling of these methods." Thing is, you don't embed nav controllers in other view controllers (unless it is, itself, a container controller such as a tab view controller), but rather its the other way around. But if you literally mean that you have a controller that contains a nav controller, you have to show us how you're doing that (proper view controller containment?) because that's highly unusual.

  4. It's unusual, but I've had projects get corrupted, ending up in weird states. At a minimum, I might suggest "Product" - "Clean" and rebuild. If problem persists, and you've isolated the problem to to B's NIB, then temporarily rename the it and build a quick and dirty one from scratch.

Rob
  • 415,655
  • 72
  • 787
  • 1,044
  • 1
    `init` method will call `initWithNibName:bundle:` passing nib name of the class name and the main bundle though, and if OP added through Xcode dialog their nib is probably the same name as the class. So it's not strictly necessary to provide a nib name without implementing `loadView`. But worth checking out that OP has indeed followed this convention! – Carl Veazey Sep 01 '12 at 07:13
  • I did not know that. I don't see that documented in the `UIViewController` class reference, but I'll take your word for it. I know that if you call `initWithNibName` and pass `nil` for the nib name, it will do as you describe, but I'm not aware that `init` would do that, as well. – Rob Sep 01 '12 at 07:16
  • Hm, well, very possibly it's passing nil to initWithNibName: actually if the implementation of initWithNibName takes care of that. That would make a bit more sense I think! – Carl Veazey Sep 01 '12 at 07:18
  • 1
    I am loading the view with a nib..I tried using intwithNibname. but it still din't work. – Nikita P Sep 01 '12 at 12:42
  • 1
    @Rob: viewDidLoad is being called on pushing the BViewController, However, viewWillAppear and viewDidAppear is not being called. – Nikita P Sep 01 '12 at 17:51
  • 1
    @Rob : Have you found some solution for this? I am still not able to resolve this.. and the case comes rarely... but it is still not a good thing. – Nikita P Sep 06 '12 at 08:55
  • @NikitaP No. I've dug around a little, but didn't find anything. The intermittent nature of the problem is the real mystery, and without the code, we can't offer any insights. If you really cannot reproduce the problem consistently, it makes me suspect some memory problem (but I assume you've run it through the Xcode "Analyze" feature, as well through instruments, checking for didReceiveMemoryWarning, religiously checking for return values from all of your functions, run it through instruments, etc.). If you've done all of that and the problem persists, I'd be happy to take a look. – Rob Sep 07 '12 at 16:14
  • may be Your class calling multiple times/ your view controller is adding from another class. – Madhu Feb 01 '13 at 06:35
  • if you are loading from a nib, have you checked if `- (void) awakeFromNib` gets called at all? if it does you should use that instead of viewWillAppear – Fonix Feb 01 '13 at 07:43