views:

563

answers:

1

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 is 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?

A: 

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;

Eric Schweichler
The weird thing is, even if I set the bar button item to nil like this:self.navigationItem.rightBarButtonItem = nil;It's still appearing no matter what. I'm even setting it nil on viewDidLoad, it's like the interface builder is not listening to the code.
Scott
I guess I never noticed because I am replacing the back button with another button. You can try this: self.navigationItem.hidesBackButton = YES;
Eric Schweichler
Still not working right I have no clue at this point.In viewDidLoad I do self.navigationItem.leftBarButtonItem = nil;The back button method has the same code in it. I'm trying to generate the button through code now so on web button click I do self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancelButtonPressed)] autorelease];
Scott
And nothing appears on either screen now =/
Scott
Where you able to resolve this? I would remove the `navigationItem.leftButtonItem = nil` and just use `self.navigationItem.hidesBackButton = YES;` when you are presenting the first view (you can do this in viewDidLoad the first time) and then set it to NO, `self.navigationItem.hidesBackButton = NO;` when you push the webview controller, then back to YES again when you dismiss the webview.
Eric Schweichler
Well I tried hidesBackButton at first when I had the button through the IBuilder, but when I took it out, I kept trying = nil. I'll give this a shot when I get back.Thanks a lot for your help man.
Scott