UINavigationController - 基础知识

发布于 2024-11-20 00:24:36 字数 1209 浏览 2 评论 0 原文

我正在尝试使用 UINavigationController 但我不确定如何使用。到目前为止(大约一年),我一直在使用presentModalViewController 和dismissModalViewController 来呈现/关闭视图控制器。

所以,这就是我所做的。我的主视图控制器(启动时显示的第一个)称为 MainViewController,它扩展了 UIViewController。

所以我在我的应用程序委托中创建了这个启动函数:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

    MainViewController *controller = [[MainViewController alloc] init];
    UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:controller];  
    [self.window addSubview:navigationController.view];  
    [self.window makeKeyAndVisible];

    return YES;
}

在我的 MainViewController 的 viewDidLoad 方法中:

- (void)viewDidLoad {

        [super viewDidLoad];

        self.title = @"Title";
        self.navigationController.navigationBar.tintColor = [Constants barColor];

            ....more code...
    }

但是,在我的 MainViewController 中,我想提供另一个名为 SecondViewController 的视图控制器,它需要一个带有后退箭头按钮的 UINavigationBar。那么我是否可以让 SecondViewController 扩展 UIViewController 并通过在 viewDidLoad 方法中设置 title 和 backButton 来执行相同的操作?我该如何呈现它?我应该怎么做才能实现这个目标?

I'm trying to use a UINavigationController but I'm uncertain how. Up till now (for about a year), I've been using presentModalViewController and dismissModalViewController to present/dismiss view controllers.

So, this is what I did. My main view controller (the first one that shows on launch) is called MainViewController, and it extends UIViewController.

So I made this launch function in my app delegate:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

    MainViewController *controller = [[MainViewController alloc] init];
    UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:controller];  
    [self.window addSubview:navigationController.view];  
    [self.window makeKeyAndVisible];

    return YES;
}

And in my MainViewController's viewDidLoad method:

- (void)viewDidLoad {

        [super viewDidLoad];

        self.title = @"Title";
        self.navigationController.navigationBar.tintColor = [Constants barColor];

            ....more code...
    }

But, in my MainViewController, I'd like to present another view controller called SecondViewController, which needs a UINavigationBar with a back arrow button. So do I make SecondViewController extend UIViewController and do the same thing by setting the title and backButton in the viewDidLoad method? And how do I present it? What should I do to accomplish this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

心凉怎暖 2024-11-27 00:24:36

您需要设置一个根视图控制器,最简单的方法是从苹果模板开始。

这就是奇迹发生的地方:

UIViewController *controller = [[UIViewController alloc] initWithNibName:@"MyNib" bundle:nil];
[self.navigationController pushViewController:controller animated:YES];
[controller release];

导航控制器为您完成所有工作(后退按钮、标题、动画) - 它会保持跟踪!


我的工作流程是这样的:

在 vi​​ewDidLoad 中设置 MutableArray,向其中添加控制器,例如:

NSMutableArray *array = [[NSMutableArray alloc] init];
MyCustomViewController *customView = [[MyCustomViewController alloc] initWithNibName:@"nib" bundle:@"nil"];
customView.title = @"Second Level";
[array addObject:customView];
self.controllers = array;

然后在您的委托中:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSUInteger row = [indexPath row];
    UIViewController *childControllerToBe = [controllers objectAtIndex:row];
    [self.navigationController pushViewController:childControllerToBe animated:YES];
}

这以及更多内容可以通过阅读一本像样的初学者书籍来学习,例如 开始 iPhone 开发

另外,苹果文档总是好的:)

You'll need to set a root view controller up, it's easiest starting from the apple template.

Here's where the magic happens:

UIViewController *controller = [[UIViewController alloc] initWithNibName:@"MyNib" bundle:nil];
[self.navigationController pushViewController:controller animated:YES];
[controller release];

The nav controller does all the work for you (back buttons, titles, animations) - it keeps track!


My workflow is this:

Setup MutableArray in the viewDidLoad, add controllers to it, e.g:

NSMutableArray *array = [[NSMutableArray alloc] init];
MyCustomViewController *customView = [[MyCustomViewController alloc] initWithNibName:@"nib" bundle:@"nil"];
customView.title = @"Second Level";
[array addObject:customView];
self.controllers = array;

Then in your delegate:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSUInteger row = [indexPath row];
    UIViewController *childControllerToBe = [controllers objectAtIndex:row];
    [self.navigationController pushViewController:childControllerToBe animated:YES];
}

This, along with a lot more can be learnt by reading a decent beginner book such as Beginning iPhone Development

Also, apple docs are always good :)

时光病人 2024-11-27 00:24:36

UINavigationController 是 UIViewController 的子类,但与 UIViewController 不同,它通常不适合您进行子类化。这是因为除了导航栏的视觉效果之外,导航控制器本身很少进行自定义。 UINavigationController 的实例可以在代码中或在 XIB 文件中相对轻松地创建。
请访问“如何以编程方式添加 UINavigationController

UINavigationController is a subclass of UIViewController, but unlike UIViewController it’s not usually meant for you to subclass. This is because navigation controller itself is rarely customized beyond the visuals of the nav bar. An instance of UINavigationController can be created either in code or in an XIB file with relative ease.
Please visit "How to add UINavigationController Programmatically"

情徒 2024-11-27 00:24:36

您应该将其推送到导航堆栈上。

斯坦福大学 iPhone 的讲座课程将教您很多有关导航栏的知识。 (快速阅读)

基本上,您需要这段代码的核心:

[self.navigationController pushViewController:SecondView];

您可以使用 PopViewController 以编程方式返回,但后退按钮是自动创建的。

这是一些源代码 摘自讲座。它准确地涵盖了您遇到的问题。

You should Push it onto the navigation stack.

This Lecture by Stanford's iPhone Course will teach you a lot about Navigation Bars. (It's a quick read)

Basically at the heart of it you need this code:

[self.navigationController pushViewController:SecondView];

You can use PopViewController to go back programmatically, but the Back Button is automatically created.

Here's some source code from the Lecture. It covers exactly what you are having issues with.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文