0

所以我有一个 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

有人知道吗?

4

1 回答 1

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;

于 2010-03-19T00:22:42.600 回答