在两个UIViews中的第一个上隐藏按钮,但在第二个时可见

时间:2010-03-19 00:10:58

标签: objective-c iphone uiviewcontroller uinavigationbar uibarbuttonitem

所以我有一个UIViewController(主应用程序控制器是一个TabBarController)。在这上面有一个UINavigationBar和一个UIBarButtonItem。我很确定我在Interface Builder中正确地连接了所有内容,并且代码中的插座连接到.xib中的按钮。应该是因为该方法可以正常工作。

现在我在这个视图上有另一个按钮,它会显示第二个视图,一个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 个答案:

答案 0 :(得分:1)

编辑:此答案不适用于nil,但我将其保留在此处,因为当您想要使用其他按钮暂时替换后退按钮时它会起作用。请在评论中查看下面的正确答案。

您可以尝试这样的事情:

在我正在研究的应用程序中,有些情况下我想暂时更换后退按钮以取消按钮,

所以我保存了一个指向它的指针:

tempButtonItem = self.navigationItem.leftBarButtonItem;

将navigationItem.leftBarButtonItem更改为取消按钮: self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancelButtonPressed)] autorelease];

然后当我想再次使用后退按钮时,我将其恢复: self.navigationItem.leftBarButtonItem = self.tempButtonItem;