我有UIViewController
(AbcViewController
)NavigationController
。 AbcViewController
使用UIView(AbcView
)作为其视图。 AbcView
有一个UITableView
。我已在TableView
中设置此AbcViewController
的数据源,并在其SuperView
即AbcView
中进行委托。当我在此表中选择一行时,如何将另一个UIViewController
(XyzViewcontroller
)推入navigationController,因为它提供了
“错误:请求成员'navigationController',而不是结构或联合”
当我这样做时:
[self.navigationController pushViewController:xyzViewcontroller animated:TRUE];
我也这样做了:
AbcViewController *parentVC;
[parentVC.navigationController pushViewController:xyzViewcontroller animated:TRUE];
虽然它成功构建,但XyzViewcontroller
的视图不会出现。它不会将XyzViewcontroller
的视图推送到navigationController。
答案 0 :(得分:3)
我们可以在AbcView
上创建一个委托,以便我们可以返回视图的viewController,在这种情况下是AbcViewController
。我们可以将委托设置为:
@interface AbcView : UIView
{
id delegate;
}
@property(nonatomic, assign) id delegate;
然后在AbcViewController
中,像这样设置视图的委托:
[abcView setDelegate:self];
现在在表格方法中使用AbcView中的委托作为:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[delegate pushOtherView];
}
它会在- (void)pushOtherView
中调用AbcViewController
方法,其定义为:
- (void)pushOtherView
{
XyzViewcontroller * xyzViewcontroller = [[[XyzViewcontroller alloc] init] autorelease];
[self.navigationController pushViewController:xyzViewcontroller animated:YES];
}
但在使用此方法之前,应在协议中提及否则将显示
Warniing:
-pushOtherView'在协议中找不到。