我刚刚将我的 Xcode 从 4.2 更新到 4.3.3,但我一直遇到问题 是否可以在单个 View 应用程序中添加导航 Controller ,因为当我尝试将一个导航 Controller 嵌入到 Controller 中时,什么也没有发生。我想有两个 View Controller 通过一个按钮连接到第二个 Controller 和一个导航栏回到第一个 View Controller 。
我想不出任何其他方式来连接 View Controller 请帮助我 任何想法。
最佳答案
如果您不想添加导航 Controller ,您可以使用
presentViewController
在现有 View Controller 之间转换,从第一个转到第二个,并且dismissViewControllerAnimated
返回。假设您使用的是 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 中的“顶部栏”属性以包含导航栏模拟指标,以便您可以布局屏幕并清楚地了解它的外观:
很明显,如果你有一个非 ARC 项目,那些带有 View Controller 的 alloc/init 的行也应该有一个 autorelease
(当你看你的应用委托(delegate))。
关于ios - 嵌入导航 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11341337/