单击按钮时打开webview

时间:2014-12-08 23:56:18

标签: ios objective-c xcode webview uibutton

我创建了一个按钮,通常应该打开一个webview控制器。但是,当我将代码连接到按钮时,它似乎没有做任何事情。

这是我的代码:

ViewController.h:

@interface ViewController : UIViewController {
    IBOutlet UIButton *webscanner;
}

-(IBAction)webscanner:(id)sender;
@property (nonatomic,strong) IBOutlet UIWebView *webView;

ViewController.m:

@synthesize webView;

-(IBAction)webscanner:(id)sender
{
     NSString *urlString = @"http://example.com/reader.html";
    //Create a URL object.
     NSURL *url = [NSURL URLWithString:urlString];

     //URL Request Object
     NSURLRequest *webRequest = [NSURLRequest requestWithURL:url];

     //Load the request in the UIWebView.
     [webView loadRequest:webRequest];
}

我做错了吗?

1 个答案:

答案 0 :(得分:0)

按照以下步骤操作: -

1)在.h文件中添加UIWebViewDelegate,就像那样

@interface ViewController : UIViewController< UIWebViewDelegate > {

2)现在实现以下委托方法,该方法将显示您的网页正在加载。

- (void)webViewDidStartLoad:(UIWebView *)webView {     
   NSLog(@"page is loading");       
 }

-(void)webViewDidFinishLoad:(UIWebView *)webView {
       NSLog(@"finished loading");
 }