使用默认

时间:2015-05-21 17:07:49

标签: ios objective-c uinavigationcontroller uinavigationbar uibarbuttonitem

关于如何使用自定义后退按钮替换默认后退按钮有很多问题,但我不知道如何使用默认值替换自定义后退按钮。

我有一个网页视图,当[self.webView canGoBack] == YES时,会出现一个自定义后退按钮。但是在你一直回头之后,那个自定义后退按钮仍然存在,而不是默认值。当[self.webView canGoBack] == NO?

时,我可以用默认值替换我的自定义后退按钮吗?

以下是我的相关代码:

@interface MerchViewController () <UIWebViewDelegate>

@property UIWebView *webView;

@end

@implementation MerchViewController

- (instancetype)init
{
self = [super init];
if (self) {
    self.navigationItem.title = @"Merchandise";
}
return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];
UIWebView *webView = [[UIWebView alloc] init];
webView.scalesPageToFit = YES;
self.view = webView;
self.webView = webView;
webView.delegate = self;
[self setURL];

}

- (void)updateBackButton {

if ([self.webView canGoBack]) {
    {
        [self.navigationItem setHidesBackButton:YES animated:NO];
        //UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"backTickets"] style:UIBarButtonItemStylePlain target:self action:@selector(backWasClicked:)];
        UIBarButtonItem *backItem = [[UIBarButtonItem alloc] backButtonWith:@"Back" tintColor:[UIColor orangeColor] target:self andAction:@selector(backWasClicked:)];
        backItem.tintColor = [UIColor orangeColor];
        [self.navigationItem setLeftBarButtonItem:backItem animated:NO];
    }
}

else {
    /*
 [self.navigationItem setHidesBackButton:YES animated:NO];
 //UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"backTickets"] style:UIBarButtonItemStylePlain target:self action:@selector(backWasClicked:)];
 UIBarButtonItem *backItem = [[UIBarButtonItem alloc] backButtonWith:@"Fan Zone" tintColor:[UIColor orangeColor] target:self andAction:@selector(openMenu:)];
 backItem.tintColor = [UIColor orangeColor];
 [self.navigationItem setLeftBarButtonItem:backItem animated:NO];
     */
}


}

- (void)webViewDidStartLoad:(UIWebView *)webView {
[self updateBackButton];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView {
[self updateBackButton];
[_spinner stopAnimating];
}

- (void)backWasClicked:(id)sender {
if ([self.webView canGoBack]) {
    [self.webView goBack];
}
}

- (void)setURL
{
NSURLRequest *req = [NSURLRequest requestWithURL:[NSURL URLWithString:@"https://www.lawlorscustom.com/omaha-lancers-hockey"]];
NSLog(@"loadRequest: %@", req);
[(UIWebView *)self.view loadRequest:req];

}

@end

&#34; else&#34;部分在&#34; updateBackButton&#34;被注释掉了,因为如果我没有对它进行评论,那么当视图首次加载时,会有两个后退按钮重叠。

1 个答案:

答案 0 :(得分:1)

经过一段时间的努力,答案就是:

[self.navigationItem setHidesBackButton:NO animated:NO];

self.navigationItem.leftBarButtonItem = self.navigationItem.backBarButtonItem;