Is there a way to hold a button in the UINavigationController stack?

I know how to add a button to each controller I push into the navigation controller stack. But I need to have a settings button on the upper right of the navigation bar, at all times, regardless of which controller gets pushed or popped. The button should present a settings viewController modally.

To me, having each controller’s viewDidLoad include code for the button plus including the settings controller header seems like a hack.

Seems like I should be able to insert this button from the navigation controller itself, pointing to a selector in the navigation controller. And this method would then present the settingsController modally through whichever controller happens to be active in the stack at that point.

Any ideas on how to achieve this?

Ok this is what I did:

1 – subclass UINavigationController and include the header for my settings controller

2 – override -pushViewController:aniamted:

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
    [super pushViewController:viewController animated:animated];
    UIBarButtonItem *settingsButton = [[UIBarButtonItem alloc]initWithTitle:@"Settings"
    [viewController.navigationItem setRightBarButtonItem: settingsButton];
    [settingsButton release];

3 – add two methods:

- (void)shouldPresentSettingsView
    SetupViewController *settingsView = [[SetupViewController alloc] init];
    UINavigationController *tempController = [[UINavigationController alloc]initWithRootViewController:settingsView];
    UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:@"Back"
    settingsView.navigationItem.leftBarButtonItem = cancelButton;
    [self.visibleViewController presentModalViewController:tempController animated:YES];

    [cancelButton release];
    [settingsView release];
    [tempController release];

- (void)shouldDismissSettingsView
    [self dismissModalViewControllerAnimated:YES];
Hello, buddy!稿源:Hello, buddy! (源链) | 关于 | 阅读提示

本站遵循[CC BY-NC-SA 4.0]。如您有版权、意见投诉等问题,请通过eMail联系我们处理。
酷辣虫 » 移动开发 » Is there a way to hold a button in the UINavigationController stack?

喜欢 (0)or分享给?

专业 x 专注 x 聚合 x 分享 CC BY-NC-SA 4.0

使用声明 | 英豪名录