2010-07-08 76 views
5

我想禁用默认回导航控制器的按钮我们可以禁用iPhone的视图控制器后退按钮leftBarButtonItem的导航控制器吗?

self.navigationItem.rightBarButtonItem.enabled = NO; 
// Below code does not work since leftBarButtonItem is always nil. 
self.navigationItem.leftBarButtonItem.enabled = NO; 

我有如下所示手工做了,但没有任何财产来禁用只有单线默认后退按钮?

backButton = [[UIButton alloc] initWithFrame:CGRectMake(5, 5, 100, 30)]; 
[backButton setBackgroundImage:[UIImage imageNamed:@"backbutton_100.png"] forState:UIControlStateNormal]; 
[backButton addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside]; 
[backButton setTitle:@" All Customers" forState:UIControlStateNormal]; 
backButton.titleLabel.font = [UIFont boldSystemFontOfSize:12]; 
[buttonView addSubview:backButton]; 

UIBarButtonItem* leftButton = [[UIBarButtonItem alloc] initWithCustomView:buttonView]; 
self.navigationItem.leftBarButtonItem = leftButton; 
[leftButton release]; 

// Now it is working. 
self.navigationItem.leftBarButtonItem.enabled = NO; 
+0

后退按钮是左侧的,所以你为什么要设置rightBarButtonItem.enabled为NO?它不应该留下吗?另外,请更新您的问题,并尝试让您的代码正确显示。每行使用4个空格(“”)使其右对齐。我们在这里帮助,而不是解密;-) – Kalle 2010-07-08 09:06:47

回答

5

使用“hidesBackButton = YES”确实不是一个优雅的解决方案,因为它隐藏了不是我们想要的按钮。一个可接受的解决方法是将UILabel添加到刚刚在后退按钮上的窗口,至少禁用按钮上的触摸。

此方法添加到您的AppDelegate类:

- (void) disableLeftBarButtonItemOnNavbar:(BOOL)disable 
{ 
    static UILabel *l = nil; 

    if (disable) { 
     if (l != nil) 
      return; 
     l = [[UILabel alloc] initWithFrame:CGRectMake(0, 20, 160, 44)]; 
     l.backgroundColor = [UIColor clearColor]; 
     l.userInteractionEnabled = YES; 
     [self.window addSubview:l]; 
    } 
    else { 
     if (l == nil) 
      return; 
     [l removeFromSuperview]; 
     [l release]; 
     l = nil; 
    } 
} 

你可以从任何视图控制器调用它禁用:

MyAppDelegate *appDeleg = (MyAppDelegate *) [[UIApplication sharedApplication] delegate]; 
[appDeleg disableLeftBarButtonItemOnNavbar:YES]; 

要启用:

MyAppDelegate *appDeleg = (MyAppDelegate *) [[UIApplication sharedApplication] delegate]; 
[appDeleg disableLeftBarButtonItemOnNavbar:NO]; 
+1

感谢您的回复 – 2010-08-02 11:29:09

+0

不客气。 – 2010-08-03 05:48:21

+0

代码和旋转设备存在一个错误,您必须执行仿射变换才能使其在其他方向上正常工作。 – Heckman 2011-10-19 15:33:13

2

对于视图控制器调用[self.navigationItem setHidesBackButton:YES];你不想有后退按钮。然后将leftBarButtonItem设置为正常。

+0

很高兴我能帮上忙。 – jrtc27 2010-07-08 09:44:02

+1

我想禁用不隐藏按钮的按钮,感谢您的回复 – 2010-07-08 09:56:44

+0

为什么不创建UIBarButtonItem实例,将其设置为leftBarButtonItem,隐藏backButton并将leftBarButtonItem设置为您的按钮。然后禁用leftBarButtonItem。 – jrtc27 2010-07-10 17:43:25

11

其非常容易.....只是试试这个

self.navigationController.navigationBar.userInteractionEnabled = NO; //for disabling 

self.navigationController.navigationBar.userInteractionEnabled = YES; //for again enabling 
+2

它不会显示为禁用(使用深灰色文本),但它可以工作,并且比隐藏按钮更好。 +1 – 2013-06-22 01:18:21

+0

它适用于我,thanx – derpoliuk 2013-08-31 12:34:42

1

您还可以使用

[[_navigationController.topViewController.navigationItem leftBarButtonItem] setEnabled:NO]; // The top view controller on the stack. 
[[_navigationController.visibleViewController.navigationItem leftBarButtonItem] setEnabled:NO];// Return modal view controller if it exists. Otherwise the top view controller. 

你可以使用这个时候你要禁用或从AppDelegate中,或任何其他viewcontroler使UIViewControler。

相关问题