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

发布于 2024-08-25 07:30:22 字数 2169 浏览 4 评论 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

有人知道吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

§对你不离不弃 2024-09-01 07:30:22

编辑:这个答案不适用于 nil,但我将其留在这里,因为当您想暂时用另一个按钮替换后退按钮时它确实有效。请参阅下面评论中的正确答案。

您可能会尝试这样的操作:

在我正在开发的应用程序中,有时我想暂时将后退按钮替换为取消按钮,

因此我保存了指向它的指针:

tempButtonItem = self.navigationItem .leftBarButtonItem;

将 navigationItem.leftBarButtonItem 更改为取消按钮:
self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem 分配]
initWithBarButtonSystemItem:UIBarButtonSystemItemCancel
目标:自己
动作:@selector(cancelButtonPressed)]
autorelease];

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

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;

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文