嵌入式导航控制器 [英] embed navigation controller

查看:125
本文介绍了嵌入式导航控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚将我的Xcode从4.2更新到4.3.3并且我一直遇到问题
是否可以在单个视图应用程序中添加导航控制器,因为当我尝试将其嵌入到控制器中时什么都没发生。我希望有两个视图控制器通过按钮连接到第二个控制器,导航栏连接到第一个视图控制器。

I've just updated my Xcode from 4.2 to 4.3.3 and i keep coming across a problem is it possible to add a navigation controller in a single view application because when i try to embed one into the controller nothing happens. I would like to have two view controllers connected by a button to the second controller and a navigation bar back to the first view controller.

我想不出任何其他方式来连接视图控制器请帮我
任何想法。

I can't think of any other way to connect the view controllers please help me any-ideas.

推荐答案


  1. 如果您不想添加导航控制器,可以使用<$ c $在现有视图控制器之间切换c> presentViewController 从第一个转到第二个,并且 dismissViewControllerAnimated 返回。

  1. If you don't want to add a navigation controller, you could transition between your existing view controllers using presentViewController to go from the first one to the second one, and dismissViewControllerAnimated to return.

假设您正在使用NIB(否则您只是使用故事板的embed命令),如果您想要添加一个与您的NIB一起使用的导航控制器,您可以相应地更改您的应用程序委托。

Assuming you're using a NIB (otherwise you'd just use the embed command for the storyboard), if you want to add a navigation controller staying with your NIB, you can alter your app delegate accordingly.

所以,您可能有一个应用代理,其中包含:

So, you probably have an app delegate that says something like:

//  AppDelegate.h

#import <UIKit/UIKit.h>

@class YourViewController;

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

@property (strong, nonatomic) YourViewController *viewController;

@end

更改此项以添加导航控制器(您可以获得摆脱以前对主视图控制器的引用):

Change this to add a navigation controller (you can get rid of the previous reference to your main view controller here):

//  AppDelegate.h

#import <UIKit/UIKit.h>

//@class YourViewController;

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

//@property (strong, nonatomic) YourViewController *viewController;
@property (strong, nonatomic) UINavigationController *navigationController;

@end

然后,在你的app delegate的实现文件中,你有一个 didFinishLaunchingWithOptions 可能会说:

And then, in your app delegate's implementation file, you have a didFinishLaunchingWithOptions that probably says something like:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.

    self.viewController = [[YourViewController alloc] initWithNibName:@"YourViewController" bundle:nil];
    self.window.rootViewController = self.viewController;

    [self.window makeKeyAndVisible];
    return YES;
}

您可以将其更改为:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.

    //self.viewController = [[YourViewController alloc] initWithNibName:@"YourViewController" bundle:nil];
    //self.window.rootViewController = self.viewController;

    YourViewController *viewController = [[YourViewController alloc] initWithNibName:@"YourViewController" bundle:nil];
    self.navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
    self.window.rootViewController = self.navigationController;

    [self.window makeKeyAndVisible];
    return YES;
}

完成后,您现在可以从一个NIB视图控制器导航到另一个NIB视图控制器 pushViewController 并返回 popViewControllerAnimated 。在 viewDidLoad 中,您还可以使用 self.title = @我的标题; 命令来控制显示的内容视图的导航栏。您可能还想更改NIB中的顶栏属性以包含导航栏模拟度量标准,以便您可以布置屏幕并很好地了解它的外观:

Having done that, you can now navigate from one NIBs view controller to another's using pushViewController and return with popViewControllerAnimated. In your viewDidLoad you can also use the self.title = @"My Title"; command to control what appears in the view's navigation bar. You may also want to change the "Top Bar" property in your NIBs to include the navigation bar simulated metric, so that you can layout your screen and have a good sense of what it's going to look like:

显然,如果你有一个非ARC项目,那些带有视图控制器的alloc / init的行也应该有 autorelease (这将是当你看到你的应用代表时,这是显而易见的。)

Clearly, if you've got a non-ARC project, those lines with the alloc/init of the view controllers should also have an autorelease, too (which will be obvious when you look at your app delegate).

这篇关于嵌入式导航控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆