在TableViewController中使用UINavigationController方法

时间:2012-09-27 14:29:37

标签: iphone ios uinavigationcontroller

我有关于UINavigationController的问题。我做的是,我设置了一个导航控制器来处理一个视图控制器,让我们命名为A.现在我做的是我在视图控制器A上放置一个UIView,在该视图的顶部,一个tableview。 tableViewcontroller及其委托是在一个单独的类中定义的。现在我想做的是在用户点击表格单元格时使用“pushViewController”-Method推送一个新的视图控制器。我是否必须将引用传递给导航控制器一直到我的tableview控制器?或者我应该如何从tabelviewcontroller类获取我的导航控制器?

2 个答案:

答案 0 :(得分:1)

这是我能够工作的tutorial

我还阅读了有关该主题的官方SDK文档:Combining Tab Bar and Navigation Controllers。由于我还在学习,tutorial比文档更能帮助我。

否则尝试此代码

- (void)applicationDidFinishLaunching:(UIApplication *)application { 

// Create instance of UINavigationController
UINavigationController *myNavigationController;
// Create initialized instance of UITabBarController
UITabBarController *tabBarController = [[UITabBarController alloc] init];
// Create initialized instance of NSMutableArray to hold our UINavigationControllers
NSMutableArray *tabs = [[NSMutableArray alloc] init];

// Create first UIViewController
UIViewController *myFirstViewController = [[UIViewController alloc] init];
[myFirstViewController setTitle:@"First"];
// Initialize the UINavigationController
myNavigationController = [[UINavigationController alloc] initWithRootViewController:myFirstViewController];
// Add UINavigationController to you tabs
[tabs addObject:myNavigationController];
// Release UIViewController and UINavigationController
[myFirstViewController release], [myNavigationController release];

// Create second UIViewController
UIViewController *mySecondViewController = [[UIViewController alloc] init];
[mySecondViewController setTitle:@"Second"];
// Initialize the UINavigationController
myNavigationController = [[UINavigationController alloc] initWithRootViewController:mySecondViewController];
// Add UINavigationController to you tabs
[tabs addObject:myNavigationController];
// Release UIViewController and UINavigationController
[mySecondViewController release], [myNavigationController release];

// Add the tabs to the UITabBarController
[tabBarController setViewControllers:tabs];
// Add the view of the UITabBarController to the window
[self.window addSubview:tabBarController.view];
}

答案 1 :(得分:1)

在单独的UITableViewclass中说(youTableView)你有UITableViewDelegates,声明一个属性id delegate; 从你的viewController说A,主题yourTableView.delegate = self;

并且可能在yourTableViewClass的didSelectRowAtIndexPath中 推动didSelect  一如既往地创建要推送的newController的实例(例如newController)  最后..

[delegate.navigationController pushViewController:newController animated:YES];

这里'委托'原来是viewController A,你已经分配了一个UINavigationController。 希望这有效