开发者

Hide button on first of two UIViews, but have it visible on second

开发者 https://www.devze.com 2022-12-23 23:35 出处:网络
So I have a UIViewController (main application controller is a TabBarController). On this there is a UINavigationBar, and a UIBarButtonItem. I\'m PRETTY sure I hooked up everything correctly in the In

So I have a UIViewController (main application controller is a TabBarController). On this there is a UINavigationBar, and a UIBarButtonItem. I'm PRETTY sure I hooked up everything correctly in the Interface Builder and that the outlet in the code is connected to the button in the .xib. It should be because the method works correctly.

Now I have another button on this view that brings up a second view, a UIWebView. I want this UIBarButtonItem, labeled "Back", to make the UIWebView dissapear, and bring back the first UIView, which it DOES DO correctly. However, when you are on the first UIView, there 开发者_如何学Gois no need to see the UIBarButtonItem, so how can I hide it but then bring it up for the UIWebView. By the way, both views use the same UINavigationBar, the UIWebView is brought up inside the tab bar and the nav bar.

Here is my code:

#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

Anyone know?


Edit: This answer does not work with nil, but I'm leaving it here as it does work when you want to temporarily replace the back button with another button. See correct answer below in comments.

You might try something like this:

In an App I'm working on there are cases where I'd like to temporarily swap the back button for a cancel button,

so I save a pointer to it:

tempButtonItem = self.navigationItem.leftBarButtonItem;

change the navigationItem.leftBarButtonItem to a cancel button: self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancelButtonPressed)] autorelease];

And then later when I want to have the back button again I restore it: self.navigationItem.leftBarButtonItem = self.tempButtonItem;

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号