从子视图调用pushview

发布于 2024-08-27 19:30:34 字数 506 浏览 5 评论 0原文

事情是这样的。我目前有一个 tabBar 控制器,其中有几个导航控制器。就像这样:

http://tof.canardpc.com/ view/d27d1361-c81b-43a0-9b5b-abb13d78e422.jpg

在我的第一个导航控制器中,我有一个名为 NewsViewsController 的视图控制器。这是它的笔尖(见图)。

我的目标是根据分段控件的位置显示/隐藏子视图(内部有表视图)。这样,我可以为每个表视图拥有两个单独的视图控制器。这实际上是有效的。这是结果(见图)。

我的问题如下。当我尝试单击单元格时,推送视图不起作用。事实上我的 self.navigationController 是空的。如何使用父导航控制器推送我的详细视图?

我的应用程序的架构是否错误?谢谢=)

Here is the thing. I currently have a tabBar controller, with several navigation controllers in it. Just like this :

http://tof.canardpc.com/view/d27d1361-c81b-43a0-9b5b-abb13d78e422.jpg

In my first navigation controller, i have a view controller called NewsViewsController. Here is its nib (see picture).

My goal is to show/hide the subviews (with tableview inside) according to the position of the segmented control. This way, I can have two separates viewControllers for each tableview. This is actually working. And here is the result (see picture).

My problem is the following one. When i try to click on a cell, the pushview doesn't work. Indeed my self.navigationController is null. How can i push my detail view using the parent navigation controller ?

Is the architecture of my application wrong ? Thank you =)

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

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

发布评论

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

评论(2

旧时模样 2024-09-03 19:30:34

导航控制器设置为推送和弹出管理整个视图的视图控制器。实际上,API 中没有任何内容可以轻松支持多个视图控制器,这些视图控制器的视图显示在同一个超级视图中。

您应该从“新闻视图”控制器管理导航,而不是尝试将下一个视图推送到表视图子视图之一。更好的是,您应该只有“新闻视图”控制器,并有单独的类来实现每个表的委托和数据源(它们不必位于视图控制器中,这通常很方便。)

像这样:

@interface NewsViewController {
    CustomClass1 *newsTableDelegateAndDataSource;
    CustomClass2 *categoryTableDelegateAndDataSource;
}

@interface CustomClass1 {
    UINavigationController *nav;
}

在 NewsViewController.m 中

- (void) viewWillAppear{
    self.newsTableDelegateAndDataSource.nav= // reference to the navigation controller;
    self.categoryTableDelegateAndDataSource.nav = // reference to the navigation controller;
}

然后在其中一个 CustomClasses 的 didSelectRow 方法中:

nextViewController=// intialize or get reference to next view controller
[self.nav pushViewController:nextViewController animated:YES];

导航会将 nextViewController 推到 NewsViewController 之上并显示新屏幕。完成后,它会自行弹出,并且 NewsViewController 会以您保留的状态重新出现。

这样,您在任何时候都只有一个视图控制器处于活动状态,因此 API 可以顺利工作,但您可以将每个表的行为封装在其自己的类中。

每个人都很高兴。

The navigation controller is setup to push and pop the view controller that manages the entire view. Really nothing in the API easily supports multiple view controllers whose views display within the same superview.

Instead of trying to push the next view on one of the tableview subviews, you should manage the nav from the "News View" controller. Even better, you should have just the "News View" controller and have separate classes that implement each table's delegate and datasources (they don't have to be in the view controller its just usually convenient.)

Something like this:

@interface NewsViewController {
    CustomClass1 *newsTableDelegateAndDataSource;
    CustomClass2 *categoryTableDelegateAndDataSource;
}

@interface CustomClass1 {
    UINavigationController *nav;
}

in NewsViewController.m

- (void) viewWillAppear{
    self.newsTableDelegateAndDataSource.nav= // reference to the navigation controller;
    self.categoryTableDelegateAndDataSource.nav = // reference to the navigation controller;
}

Then in the didSelectRow methods of one of CustomClasses:

nextViewController=// intialize or get reference to next view controller
[self.nav pushViewController:nextViewController animated:YES];

The nav will push the nextViewController on top of NewsViewController and display the new screen. When it is done, it pops itself and NewsViewController reappears in the state you left it in.

This way you only have one view controller active at anyone time so the API works smoothly but you encapsulate the behavior of each table in its own class.

Everybody is happy.

流殇 2024-09-03 19:30:34

我通过从子视图调用父方法来实现这一点 - 父方法执行推送。

子视图:

@protocol SubUIViewDelegate
    -(void)pushViewController:(UIViewController*)ctrl;
@end

@interface ... {
   ...
   id<SubUIViewDelegate> delegate;
}
@property (nonatomic,assign) id <SubUIViewDelegate> delegate;
...
@end

然后@synthesize委托并使用以下代码从子视图调用推送

[self.delegate pushViewController : new_view_ctrl];

父视图控制器:

@interface parentview : ... <SubUIViewDelegate> {
    ...
}
-(void) pushViewController: (UIViewController*) ctrl;

实现:

subview.delegate = self;

-(void)pushViewController:(UIViewController*)ctrl {
    [self.navigationController pushViewController:ctrl animated:YES];
}

希望这会有所帮助。

I achieved this by calling a parent method from the subview - the parent method performs the push.

Subview:

@protocol SubUIViewDelegate
    -(void)pushViewController:(UIViewController*)ctrl;
@end

@interface ... {
   ...
   id<SubUIViewDelegate> delegate;
}
@property (nonatomic,assign) id <SubUIViewDelegate> delegate;
...
@end

Then @synthesize the delegate and use the following to call a push from the subview

[self.delegate pushViewController : new_view_ctrl];

Parent View Controller:

@interface parentview : ... <SubUIViewDelegate> {
    ...
}
-(void) pushViewController: (UIViewController*) ctrl;

Implements:

subview.delegate = self;

-(void)pushViewController:(UIViewController*)ctrl {
    [self.navigationController pushViewController:ctrl animated:YES];
}

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文