我无法弄清楚如何将导航控制器添加到我的 iOS 应用程序。我需要除了“主”屏幕之外的所有视图都有一个后退按钮,但我不知道如何添加它。
这是我的项目的链接: https ://www.dropbox.com/s/sv0y3oh1aftxl95/KFBNewsroom%204.zip
我无法弄清楚如何将导航控制器添加到我的 iOS 应用程序。我需要除了“主”屏幕之外的所有视图都有一个后退按钮,但我不知道如何添加它。
这是我的项目的链接: https ://www.dropbox.com/s/sv0y3oh1aftxl95/KFBNewsroom%204.zip
从所有 NIB 中删除导航栏并使用导航控制器(例如在 NeverBe 概述的应用程序委托中),然后通过 apushViewController
而不是presentViewController
像您当前所做的那样转换到子控制器,您应该得到您的 "后退”按钮自动。您还需要删除对 的任何引用dismissViewControllerAnimated
(如果有的话),因为您的后退按钮现在popViewControllerAnimated
将为您执行。但是,如果您需要以编程方式在任何地方弹出,您可以使用popViewControllerAnimated
.
在您的 NIB 中,您可能还想调整模拟指标,以便您可以设计带有图形表示的导航栏的 NIB,例如:
请参阅视图控制器目录的导航控制器部分并参阅UINavigationController
类参考。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:[[KFBViewController alloc] initWithNibName:@"KFBViewController" bundle:nil]];
self.window.rootViewController = nav;
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
调用新的视图控制器
KFBYouTubeView *youtubeView = [[KFBYouTubeView alloc] initWithNibName:@"KFBYouTubeView" bundle:nil];
[self.navigationController pushViewController:youtubeView animated:YES];
更新:
添加自定义导航栏按钮的方法
- (void)customizeNavigationButtonWithType:(NavigationBarButtonType)type
normalImageName:(NSString *)normalImageName
selectedImageName:(NSString *)selectedImageName
selector:(SEL)selector {
UIImage *img = [UIImage imageNamed:normalImageName];
UIImage *imgPressed = [UIImage imageNamed:selectedImageName];
UIButton *customButton = [UIButton buttonWithType:UIButtonTypeCustom];
[customButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[customButton setImage:img forState:UIControlStateNormal];
[customButton setImage:imgPressed forState:UIControlStateHighlighted];
customButton.frame = CGRectMake(0, 0, img.size.width, img.size.height);
[customButton addTarget:self action:selector forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *btn = [[UIBarButtonItem alloc] initWithCustomView:customButton];
switch (type) {
case NavigationBarButtonTypeLeft:
[self.navigationItem setLeftBarButtonItem:btn animated:YES];
break;
case NavigationBarButtonTypeRight:
[self.navigationItem setRightBarButtonItem:btn animated:YES];
break;
}
}
用法:
[self customizeNavigationButtonWithType:NavigationBarButtonTypeRight
normalImageName:@"create.png"
selectedImageName:@"create_highlight.png"
selector:@selector(pressButton:)];