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

Posted by Scott on Stack Overflow See other posts from Stack Overflow or by Scott
Published on 2010-03-19T00:10:57Z Indexed on 2010/03/19 2:21 UTC
Read the original article Hit count: 366

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?

© Stack Overflow or respective owner

Related posts about iphone-sdk

Related posts about objective-c