gpt4 book ai didi

iphone - 以编程方式创建新 View 并管理它们

转载 作者:行者123 更新时间:2023-12-01 18:29:42 26 4
gpt4 key购买 nike

我是 Objective-C 的新手,我开始研究将有几个 View 的应用程序。我想在没有 XIB 的情况下以编程方式 100% 创建它们文件。我知道我必须创建我的 ViewControllers我的屏幕的类,但我想问你如何用它管理导航。假设我有一个 ViewController其中包含 tableView .我想创建下一个屏幕。所以我明白 rowtableViewController类调用

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { //... }

但是下一个屏幕呢?我还没有。我应该创建一个“准备好的” ViewController类(class)?在这种情况下如何处理导航?

最佳答案

在你的情况下,当一个单元格被选中时,你会:

  • 创建下一个 View Controller 的新实例
  • 将这个新实例推送到 UINavigationController 堆栈上。

  • 因此,首先,您需要确保您的第一个 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/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com