无法为子视图推送ViewController

发布于 2024-08-10 11:46:09 字数 689 浏览 5 评论 0原文

我有一个 UINavigationController 和我有单独的 UIViews,我使用 UISegmentControl 在它们之间切换。在切换视图时,我将视图作为子视图添加到导航控制器的视图中:

[self.view addSubview:segmentTab1.view];

然后

[self.view addSubview:segmentTab2.view];

,在子视图中,每个视图都有一个 UITableView,但我的问题是,我无法将新的 viewController 推送到 didSelectRowAtIndexPath 方法。

该方法被正确调用,通过设置断点,我可以看到推送视图的方法也被调用,但没有任何反应。这是我推送它的代码:

[self.navigationController PushViewController:detailsViewControllerAnimated:YES];

我也尝试了

[super.navigationController PushViewController:detailsViewControlleranimated:YES];

什么我做错了吗?或者只是不可能用子视图来做到这一点?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

碍人泪离人颜 2024-08-17 11:46:09

当你调用-pushViewController时,哪个视图控制器是self?如果您从选项卡子视图之一中调用它,则 self 可能没有从您添加到的顶级视图中对导航控制器的引用。您可以通过从顶级视图中获取导航控制器的内存地址并将其与子视图中的内存地址进行比较来验证这一点。显然,如果它为零或不匹配,那就是你的问题。

当使用 -pushViewController 将其添加到导航堆栈本身时,您只能“免费”获得一个导航控制器,而您的子视图尚未以这种方式添加。

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.

左耳近心 2024-08-17 11:46:09

在为所有视图实现通用标头时,我遇到了类似的问题
经过多次尝试,我已经通过此修复了它 -

在所有 viewController 中,

- (void)viewWillAppear:(BOOL)animated {

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

}

我都引用了以下帖子来实现标题视图
iPhone 中多个视图控制器中的通用 XIB

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

[[(NavAppAppDelegate *)[[UIApplication sharedApplication] delegate] headerview] viewWillAppear:YES]; // 这是调用 HeaderController 的 viewWillAppear 方法,我们可以在其中编写代码来检查用户是否已登录,并将登录按钮更改为注销按钮等。

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 ..

墟烟 2024-08-17 11:46:09

不是 和

[self.view addSubview:segmentTab1.view];

,并使 [super.navigationController PushViewController:detailsViewControllerAnimated:YES]; 工作。

[self.view addSubview:segmentTab2.view];

您可以使用

[self pushViewController: segmentTab1 animated: NO];

[self pushViewController: segmentTab2 animated: NO];

将 viewController 添加到导航层次结构中,而

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.

阿楠 2024-08-17 11:46:09

我并不完全清楚你的视图层次结构是什么,但一般来说,如果你的导航控制器视图不是窗口的第一个子视图或Apple集合视图之一的元素(另一个导航视图控制器的内容视图或选项卡控制器的内容视图)它将无法正常工作。

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.

爱你是孤单的心事 2024-08-17 11:46:09

如果您不反对单例,一种可能性是使产品的 UINavigationController 对象成为单例,可以从(例如)您的应用程序委托访问。

您可以这样调用它:

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

在 MyApplicationDelegate 中,navController 返回单例对象。

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.

櫻之舞 2024-08-17 11:46:09
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;
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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文