设置标题并添加按钮到导航控制器

时间:2013-04-09 15:41:00

标签: objective-c

我创建了一个导航控制器,但我无法设置标题或添加按钮到导航栏。怎么做? 这是文件AppDelegate.m中的应用程序代码DidFinishLauchingOption:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:view];

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    self.view = [[ViewController alloc] init];
    self.window.rootViewController = self.view;
    [self.window addSubview:navController.view];
    [self.window makeKeyAndVisible];
    return YES;
}

提前致谢。

2 个答案:

答案 0 :(得分:0)

您需要将窗口对象的rootViewController属性设置为导航控制器,而不是您的ViewController实例。这应该指向正确的方向:

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Create and configure a window
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.backgroundColor = [UIColor whiteColor];

    // Create a view controller
    UIViewController *viewController = [[ViewController alloc] init];

    // Create a navigation controller and set its root view controller to the instance of `ViewController`
    UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:viewController];

    // Add the navigation controller to the window
    self.window.rootViewController = navController;

    [self.window makeKeyAndVisible];

    return YES;
}

// ...

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Set the view controller's title
    self.title = NSLocalizedString(@"View Controller", @"");

    // Add a navigation bar button
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(refreshButtonPressed:)];
}

- (void)refreshButtonPressed:(id)sender
{
    // Do something when the refresh button is pressed.
}

// ...

@end

答案 1 :(得分:0)

要创建初始设置,请使用视图控制器创建导航控制器,并将其设置为应用委托窗口的根视图控制器:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    //create window
    [self setWindow:[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]];

    //create and set root view controller
    [[self window] setRootViewController:[[UINavigationController alloc] initWithRootViewController:[[RootViewController alloc] init]]];

    //make window key and visible
    [self.window makeKeyAndVisible];

    //bail
    return YES;
}

然后在视图控制器中,设置标题并添加导航项:

- (void)viewWillAppear:(BOOL)animated
{
    //call parent implementation
    [super viewWillAppear:animated];

    //set view controller title
    [self setTitle:@"Root View Controller"];

    //add navigation bar button
    [[self navigationItem] setRightBarButtonItem:[[UIBarButtonItem alloc] initWithTitle:@"Button Title" style:UIBarButtonItemStyleBordered target:self action:@selector(handleBarButtonItemEvents:)]];
}

通过以下方式收听按钮事件:

- (void)handleBarButtonItemEvents:(id)sender
{
    //
}