如何从使用此视图的不同UIViewController的单独UIView推送UIViewController到UINavigationController

时间:2010-03-24 10:06:32

标签: iphone uiview uiviewcontroller uinavigationcontroller

我有UIViewControllerAbcViewControllerNavigationControllerAbcViewController使用UIView(AbcView)作为其视图。 AbcView有一个UITableView。我已在TableView中设置此AbcViewController的数据源,并在其SuperViewAbcView中进行委托。当我在此表中选择一行时,如何将另一个UIViewControllerXyzViewcontroller)推入navigationController,因为它提供了

  

“错误:请求成员'navigationController',而不是结构或联合”

当我这样做时:

[self.navigationController pushViewController:xyzViewcontroller animated:TRUE]; 

我也这样做了:

AbcViewController *parentVC;
[parentVC.navigationController pushViewController:xyzViewcontroller animated:TRUE]; 

虽然它成功构建,但XyzViewcontroller的视图不会出现。它不会将XyzViewcontroller的视图推送到navigationController。

1 个答案:

答案 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'在协议中找不到。