我是 Objective-C 的新手,我开始研究将有几个 View 的应用程序。我想在没有 XIB
的情况下以编程方式 100% 创建它们文件。我知道我必须创建我的 ViewControllers
我的屏幕的类,但我想问你如何用它管理导航。假设我有一个 ViewController
其中包含 tableView
.我想创建下一个屏幕。所以我明白 row
在 tableViewController
类调用
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { //... }
但是下一个屏幕呢?我还没有。我应该创建一个“准备好的”
ViewController
类(class)?在这种情况下如何处理导航?
最佳答案
在你的情况下,当一个单元格被选中时,你会:
因此,首先,您需要确保您的第一个 View Controller (带有表格 View 的那个)包含在 UINavigationController 中。
// AppDelegate, in applicationDidFinishLanching:
UIViewController *firstViewController = [[[MyCustomTableViewController alloc]
initWithNibName:nil bundle:nil]
autorelease];
UINavigationController *navigationController = [[[UINavigationController alloc]
initWithRootViewController:firstViewController]
autorelease];
[self.window setRootViewController:navigationController];
然后,当您选择表格 View 中的一个单元格时,您可以编写:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UIViewController *nextViewController = [[[MyNextViewController alloc]
initWithNibName:nil bundle:nil]
autorelease];
[self.navigationController pushViewController:nextViewController
animated:YES]
}
关于iphone - 以编程方式创建新 View 并管理它们,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10329953/