Back Button for UIWebView

时间:2016-12-09 12:47:34

标签: ios objective-c webview uiwebview theos

Im attempting to create a left navigation button to send my webview back a page, but when trying to compile theos throws the following error :

property 'webView' not found on object of type 'DDRootViewController *'if ([self.webView canGoBack]) {

the code Im using is below, any help would be greatly appreciated :)

DDRootControllerView.m

#import "DDRootViewController.h"

@implementation DDRootViewController

- (void)viewDidLoad{

    [super viewDidLoad];

        //Create UIWebView
            UIWebView *webView = [[UIWebView alloc]initWithFrame:self.view.frame];
            webView.delegate = self;
            [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"https://dylanduff.github.io/WidgetStore/"]]];
            [self.view addSubview:webView];
            webView.backgroundColor = [UIColor whiteColor];

            //NSString *theTitle=[webView stringByEvaluatingJavaScriptFromString:@"document.title"];
            self.title = @"Widget Store";

        //Create back button
            //self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addButtonTapped:)] autorelease];


}

- (void)updateBackButton {
    if ([self.webView canGoBack]) {
        if (!self.navigationItem.leftBarButtonItem) {
            [self.navigationItem setHidesBackButton:YES animated:YES];
            UIBarButtonItem *backItem = [[[UIBarButtonItem alloc] initWithTitle:@"<" style:UIBarButtonItemStylePlain target:self action:@selector(backWasClicked:)] autorelease];
            [self.navigationItem setLeftBarButtonItem:backItem animated:YES];
        }
    }
    else {
        [self.navigationItem setLeftBarButtonItem:nil animated:YES];
        [self.navigationItem setHidesBackButton:NO animated:YES];
    }
}

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

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

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


@end

DDRootControllerView.m

@interface DDRootViewController : UIViewController
<UIWebViewDelegate> {}
@end

2 个答案:

答案 0 :(得分:2)

As You have taken local instance of webView, you can not access it out of the scope of ViewDidLoad. For accessing the instance of webView outside the viewDidLoad you need to take public property for that like this

@property(nonatomic, weak) UIWebView *webView

Now you need store webView object in the public property declared earlier so that you can use this property all over the class, your viewDidLoad method will look like this

- (void)viewDidLoad{

    [super viewDidLoad];

     //Create UIWebView
            self.webView = [[UIWebView alloc]initWithFrame:self.view.frame];
            self.webView.delegate = self;
            [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"https://dylanduff.github.io/WidgetStore/"]]];
            [self.view addSubview:self.webView];
            self.webView.backgroundColor = [UIColor whiteColor];

            //NSString *theTitle=[self.webView stringByEvaluatingJavaScriptFromString:@"document.title"];
            self.title = @"Widget Store";

        //Create back button
            //self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addButtonTapped:)] autorelease];

}

答案 1 :(得分:0)

In viewDidload you create local webView variable. Of course it is not visible from another method.

If you have property webView defined (as I can suggest from the code), use self.webView instead of UIWebView *webView in viewDidLoad method.