0

所以我有一个UIViewController(主应用程序控制器是一个TabBarController)。在这里有一个UINavigationBar和一个UIBarButtonItem。我很清楚我在Interface Builder中正确连接了所有东西,并且代码中的插座连接到了.xib中的按钮。应该是因为该方法正常工作。隐藏两个UIViews中的第一个按钮,但它在第二个可见

现在我在这个视图上还有另一个按钮,它带来了第二个视图,一个UIWebView。我想要这个标记为“Back”的UIBarButtonItem使UIWebView消失,并且带回第一个UIView,它正确地做了。但是,当你在第一个UIView上时,不需要看到UIBarButtonItem,所以我怎么能隐藏它,然后把它提交给UIWebView。顺便说一下,这两个视图使用相同的UINavigationBar,UIWebView在标签栏和导航栏内引入。

这里是我的代码:

#import "WebViewController.h" 

@implementation WebViewController 
@synthesize webButton; 
@synthesize item; 
@synthesize infoView; 

UIWebView *webView; 


+ (UIColor*)myColor1 { 
    return [UIColor colorWithRed:0.0f/255.0f green:76.0f/255.0f blue:29.0f/255.0f alpha:1.0f]; 
} 

// Creates Nav Bar with default Green at top of screen with given String as title 
+ (UINavigationBar*)myNavBar1: (NSString*)input { 

    UIView *test = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]]; 
    UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0.0, 0.0, test.bounds.size.width, 45)]; 
    navBar.tintColor = [WebViewController myColor1]; 

    UINavigationItem *navItem; 
    navItem = [UINavigationItem alloc]; 
    navItem.title = input; 
    [navBar pushNavigationItem:navItem animated:false]; 

    return navBar; 
} 

- (IBAction) pushWebButton { 

    self.navigationItem.rightBarButtonItem = item; 

    CGRect webFrame = CGRectMake(0.0, 45.0, 320.0, 365.0); 
    webView = [[UIWebView alloc] initWithFrame:webFrame]; 

    [webView setBackgroundColor:[UIColor whiteColor]]; 
    NSString *urlAddress = @"http://www.independencenavigator.com"; 
    NSURL *url = [NSURL URLWithString:urlAddress]; 
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url]; 
    webView.scalesPageToFit = YES; 
    [webView loadRequest:requestObj]; 

    [self.view addSubview:webView]; 
    [webView release]; 

} 

- (void) pushBackButton { 

    [webView removeFromSuperview]; 
} 

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib. 

- (void)viewDidLoad 
{ 
    self.navigationItem.rightBarButtonItem = nil; 

    [super viewDidLoad]; 
} 

@end 

任何人都知道吗?

回答

1

编辑︰这个答案不适用于nil,但我留在这里,因为它确实工作,当你想暂时用另一个按钮替换后退按钮。在评论中看到正确的答案。

您可以尝试这样的事:

在一个App我的工作有,我想暂时换后退键取消按钮的情况下,

所以我节省了指向它的指针:

tempButtonItem = self.navigationItem.leftBarButtonItem;

变化navigationItem.leftBarButtonItem到取消按钮: self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancelButtonPressed)] autorelease];

再后来,当我想有后退按钮,我再次恢复它: self.navigationItem.leftBarButtonItem = self.tempButtonItem;

+0

奇怪的是,即使我的栏按钮项设置为nil这样的: self.navigationItem.rightBarButtonItem =零; 无论如何它仍然出现。我甚至在viewDidLoad上设置它为零,就像界面生成器没有听代码。 – Scott 2010-03-19 00:33:25

+0

我想我从来没有注意到,因为我用另一个按钮替换了后退按钮。你可以试试这个:self.navigationItem.hidesBackButton = YES; – 2010-03-19 02:15:05

+0

仍然不能正常工作我在这一点上没有线索。 In viewDidLoad I do self.navigationItem.leftBarButtonItem = nil; 后退按钮方法在其中具有相同的代码。我尝试通过代码生成按钮,所以在web按钮上单击我做 self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancelButtonPressed)] autorelease]; – Scott 2010-03-19 04:23:39

相关问题