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

Objective c 设置标题并将按钮添加到导航控制器,objective-c,Objective C,我创建了一个导航控制器,但我无法设置标题或向导航栏添加按钮。如何操作? 这是AppDelegate.m文件中应用程序DidFinishLauchingOption的代码: - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { UINavigationController *navController = [[UINavig

我创建了一个导航控制器,但我无法设置标题或向导航栏添加按钮。如何操作? 这是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;
}

提前感谢。

您需要将窗口对象的
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

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

- (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
{
    //
}

它不起作用。我放入viewcontroller的所有内容都不会显示:(