iPhone:如何从视图控制器中的导航栏中删除右键?

时间:2013-05-23 13:46:44

标签: iphone ios uinavigationcontroller uinavigationbar

我已经制作了一个自定义导航栏,通过实现以下方法对所有视图都一样 - :

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
//    viewController.navigationItem.rightBarButtonItem = cancelButton;
// -- Adding INFO button to Navigation bar --
UIBarButtonItem *infoButton =  [[UIBarButtonItem alloc]
                                initWithTitle:@"i"
                                style:UIBarButtonItemStyleBordered
                                target:self
                                action:@selector(showInfo)];
infoButton.tag = 10;
self.navCntrl.topViewController.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:infoButton, nil];
self.navCntrl.navigationBar.tintColor = [UIColor colorWithRed:45/255.0 green:77/255.0 blue:67/255.0 alpha:1];
//    NSLog(@"Inside implemented method");
}
UINavigationControllerDelegate

在上面的方法中,我在导航项目中添加了一个右键。现在我想在特定视图中隐藏此右键。我怎样才能做到这一点? 感谢。

6 个答案:

答案 0 :(得分:7)

尝试使用此

self.navigationItem.rightBarButtonItem = nil;
self.navigationItem.rightBarButtonItem.enabled = NO;

答案 1 :(得分:6)

在viewDidLoad中,尝试

self.navigationItem.rightBarButtonItem = nil;

在viewWillDisappear中,不要忘记把它放回去。

答案 2 :(得分:1)

使用此

self.navigationItem.rightBarButtonItem = nil;

答案 3 :(得分:0)

一种很好的方法是让你的视图控制器实现一个协议。您选择名称,但它可以是CustomNavigationCustomization,并且只有一个方法:

@protocol CustomNavigationCustomization

- (BOOL)shouldShowRightButton;

@end

然后,您可以将方法更改为:

- (void)navigationController:(UINavigationController *)navigationController   
      willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {

    BOOL shouldShowRightButton = YES;

    if ([viewController conformsToProtocol:@protocol(CustomNavigationCustomization)) {
        UIViewController <CustomNavigationCustomization> *customizableViewController =
                       (UIViewController <CustomNavigationCustomization>)viewController;

        shouldShowRightButton = [customizableViewController shouldShowRightButton];
    }

    if (shouldShowRightButton) {

        //    viewController.navigationItem.rightBarButtonItem = cancelButton;
        // -- Adding INFO button to Navigation bar --
        UIBarButtonItem *infoButton =  [[UIBarButtonItem alloc] initWithTitle:@"i"
                                                                        style:UIBarButtonItemStyleBordered
                                                                       target:self
                                                                       action:@selector(showInfo)];
        infoButton.tag = 10;
        self.navCntrl.topViewController.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:infoButton, nil];
        self.navCntrl.navigationBar.tintColor = [UIColor colorWithRed:45/255.0 green:77/255.0 blue:67/255.0 alpha:1];
        //    NSLog(@"Inside implemented method");
    }
}

请注意,导航控制器委托中的方法非常防御:它会检查您的视图控制器是否符合协议,然后才会调用该方法。这样,您不需要在大多数视图控制器中符合协议,只需要在您希望自定义的视图控制器中使用。

答案 4 :(得分:0)

只需检查viewController是否推送了你不想要righ bar按钮的类型:

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {

    // Replace the YourViewController with the type of the viewcontroller 
    // you want not the have the right bar button.
    if ([viewController isKindOfClass:[YourViewController class]]) {
        return;
    }

    //    viewController.navigationItem.rightBarButtonItem = cancelButton;
    // -- Adding INFO button to Navigation bar --
    UIBarButtonItem *infoButton =  [[UIBarButtonItem alloc]
                                    initWithTitle:@"i"
                                    style:UIBarButtonItemStyleBordered
                                    target:self
                                    action:@selector(showInfo)];
    infoButton.tag = 10;
    self.navCntrl.topViewController.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:infoButton, nil];
    self.navCntrl.navigationBar.tintColor = [UIColor colorWithRed:45/255.0 green:77/255.0 blue:67/255.0 alpha:1];
    //    NSLog(@"Inside implemented method");
}

答案 5 :(得分:0)

对于那些仍在寻找答案的人,这段代码在AppDelegate.m

中适用于我
 - (BOOL)application:(UIApplication *)applicationdidFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    ...
    //  Get rid of the edit button in UITabBarController's moreNavigationController
            tabBarController.customizableViewControllers = nil;
    ...
        }
相关问题