0

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

提前致谢。

4

2 回答 2

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
于 2013-04-09T15:59:48.723 回答
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
{
    //
}
于 2013-04-09T16:02:00.243 回答