使用后退按钮添加UINavigationController

时间:2012-11-05 14:51:22

标签: ios uinavigationcontroller

我无法弄清楚如何将导航控制器添加到我的iOS应用程序中。我需要除“主屏幕”以外的所有视图都有一个后退按钮,但我不知道如何添加它。

这是我项目的链接: https://www.dropbox.com/s/sv0y3oh1aftxl95/KFBNewsroom%204.zip

2 个答案:

答案 0 :(得分:1)

从所有NIB中删除导航栏并使用导航控制器(例如在应用程序委托中,如NeverBe概述),然后通过pushViewController而不是presentViewController转换到子控制器就像你现在正在做的那样,你应该得到你的回复"按钮自动。您还要删除对dismissViewControllerAnimated的任何引用(如果有的话),因为您的后退按钮现在会为您popViewControllerAnimated。但是,如果您需要以编程方式在任何地方弹出,则可以使用popViewControllerAnimated

在您的NIB中,您可能还想调整模拟的指标,以便您可以使用图形表示的导航栏来设计NIB,例如:

simulated metrics for nav bar

请参阅View Controller Catalog的导航控制器部分,并参阅UINavigationController Class Reference

答案 1 :(得分:0)

- (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:)];