ios - 嵌入导航 Controller

标签 ios xcode xcode4.2 xcode4.3 navigationcontroller

我刚刚将我的 Xcode 从 4.2 更新到 4.3.3,但我一直遇到问题 是否可以在单个 View 应用程序中添加导航 Controller ,因为当我尝试将一个导航 Controller 嵌入到 Controller 中时,什么也没有发生。我想有两个 View Controller 通过一个按钮连接到第二个 Controller 和一个导航栏回到第一个 View Controller 。

我想不出任何其他方式来连接 View Controller 请帮助我 任何想法。

最佳答案

  1. 如果您不想添加导航 Controller ,您可以使用 presentViewController 在现有 View Controller 之间转换,从第一个转到第二个,并且 dismissViewControllerAnimated 返回。

  2. 假设您使用的是 NIB(否则您只需使用 Storyboard的嵌入命令),如果您想添加一个与 NIB 一起使用的导航 Controller ,您可以相应地更改您的应用委托(delegate)。

所以,您可能有一个应用委托(delegate),上面写着类似这样的话:

//  AppDelegate.h

#import <UIKit/UIKit.h>

@class YourViewController;

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

@property (strong, nonatomic) YourViewController *viewController;

@end

更改它以添加导航 Controller (您可以在此处删除之前对主视图 Controller 的引用):

//  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

然后,在您的应用委托(delegate)的实现文件中,您有一个 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;
}

您可以将其更改为:

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

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

enter image description here

很明显,如果你有一个非 ARC 项目,那些带有 View Controller 的 alloc/init 的行也应该有一个 autorelease(当你看你的应用委托(delegate))。

关于ios - 嵌入导航 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11341337/

相关文章:

ios - 为 iOS 编译 libogg + libvorbis

ios - 安装 Xcode 7 后,我无法再构建和运行项目,因为文档无法打开

xcode - Cocoa:主窗口中央的 HUD 窗口

ios - 更新的 ios5 应用程序 xcode 不会在 __dyld__dyld_start 运行停止

ios - 无法使用 Xcode 4.2 存档临时分发

ios - iPad 的 SplitView 和 iPhone 的菜单抽屉

ios - 更改字形中单个字符的颜色

ios - 设置文本字段的数值范围

ios - 如何隐藏特定版本的 Apple Watch 应用程序

ios - 使用现有的自定义键盘代码 xcode