xcode 嵌入导航控制器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11341337/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 00:47:38  来源:igfitidea点击:

embed navigation controller

iosxcodexcode4.2xcode4.3navigationcontroller

提问by Picm

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.

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

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

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

回答by Rob

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

  2. 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.

  1. 如果您不想添加导航控制器,您可以使用presentViewController从第一个到第二个并dismissViewControllerAnimated返回的现有视图控制器之间进行转换。

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

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

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

然后,在您的应用程序委托的实现文件中,您有一个didFinishLaunchingWithOptions可能是这样的:

- (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;
}

You can change that to say:

您可以将其更改为:

- (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;
}

Having done that, you can now navigate from one NIBs view controller to another's using pushViewControllerand return with popViewControllerAnimated. In your viewDidLoadyou 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:

完成后,您现在可以从一个 NIB 视图控制器导航到另一个使用pushViewController并返回popViewControllerAnimated. 您viewDidLoad还可以使用该self.title = @"My Title";命令来控制视图导航栏中显示的内容。您可能还想更改 NIB 中的“顶部栏”属性以包含导航栏模拟指标,以便您可以布局屏幕并对其外观有一个很好的了解:

enter image description here

在此处输入图片说明

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).

显然,如果你有一个非 ARC 项目,那些带有视图控制器的 alloc/init 的行也应该有一个autorelease, (当你查看你的应用程序委托时会很明显)。