English 中文(简体)
Unable to pushViewController for subview
原标题:

I have a UINavigationController and I have seperate UIViews that I switch between using a UISegmentControl. On switching the views, I add the view as a subview to my navigation controller s view:

[self.view addSubview:segmentTab1.view];

and

[self.view addSubview:segmentTab2.view];

Then, in the subViews, each has a UITableView, but my issue is, that I am unable to push a new viewController into view in the didSelectRowAtIndexPath method.

The method is called correctly and by setting breakpoints, I can see the method for pushing the view gets called as well, but nothing happens. This is my code for pushing it:

[self.navigationController pushViewController:detailsViewController animated:YES];

I also tried

[super.navigationController pushViewController:detailsViewController animated:YES];

What am I doing wrong - or is is just not possible to do it with a subview?

最佳回答

When you call -pushViewController, which view controller is self? If you are calling that from within one of your tab subviews, self likely doesn t have a reference to the navigation controller from the top level view that you added it to. You can verify this by getting the memory address of the navigation controller from within the top level view and comparing it to what you have in the subview. Obviously if it s nil or doesn t match, then that s your problem.

You only get a navigation controller "for free" when it s been added to the navigation stack itself with -pushViewController which your subviews haven t been added that way.

问题回答

I had a similar issue when implementing a common header for all the views After many tries , i have fixed it by this -

In all the viewController

- (void)viewWillAppear:(BOOL)animated {

    [self.view addSubview:[[(NavAppAppDelegate *)[[UIApplication sharedApplication] delegate] headerview] view]];
    [[(NavAppAppDelegate *)[[UIApplication sharedApplication] delegate] headerview] viewWillAppear:YES];

}

I have referred following post to implement the header view Common XIB in multiple View Controller in iPhone

[self.view addSubview:[[(NavAppAppDelegate *)[[UIApplication sharedApplication] delegate] headerview] view]];     // line will load the header subview 

[[(NavAppAppDelegate *)[[UIApplication sharedApplication] delegate] headerview] viewWillAppear:YES]; // this is to call the viewWillAppear method of HeaderController where we can write code to check the user is logged or not and change the login button to logout button etc ..

Instead of

[self.view addSubview:segmentTab1.view];

and

[self.view addSubview:segmentTab2.view];

you may use

[self pushViewController: segmentTab1 animated: NO];

and

[self pushViewController: segmentTab2 animated: NO];

to add your viewControllers to the navigation hierarchy, and make [super.navigationController pushViewController:detailsViewController animated:YES]; work.

It is not entirely clear to me what your view hierarchy is, but in general if your navigation controllers view is not the first subview of a window or an element of one of Apple s collection views (either another navigation view controller s content view or a tab controller s content view) it won t work correctly.

One possibility, if you are not averse to singletons, is to make your product s UINavigationController object be a singleton, accessible from (for example) your application delegate.

You would invoke it thus:

[[((MyApplicationDelegate*)[UIApplication delegate]) navController] 
                pushViewController: viewControllerToPush animated: YES];

where within MyApplicationDelegate, navController returns the singleton object.

LogEntryDetailViewController *leController = [[LogEntryDetailViewController alloc] initWithNibName:@"LogEntryDetailView" bundle:[NSBundle mainBundle]];
    [leController setTitle:[NSString stringWithFormat:@"%@", [logEntriesArray objectAtIndex:row]]];
    [[self navigationController] pushViewController:leController animated:NO];      
    [leController release], leController = nil;




相关问题
Code sign Error

I have created a new iPhone application.I have two mach machines. I have created the certificate for running application in iPhone in one mac. Can I use the other mac for running the application in ...

ABPersonViewController Usage for displaying contact

Created a View based Project and added a contact to the AddressBook using ABAddressBookRef,ABRecordRef now i wanted to display the added contact ABPersonViewController is the method but how to use in ...

将音频Clips从Peter改为服务器

我不禁要问,那里是否有任何实例表明从Peit向服务器发送音响。 I m不关心电话或SIP风格的解决办法,只是一个简单的袖珍流程......

• 如何将搜查线重新定位?

我正试图把图像放在搜索条左边。 但是,问题始于这里,搜索条线不能重新布署。

热门标签