开发者

how to add UIWebview inside a view by coding

In my app I dragged a View from the library of the interface builder and linked it to the AppDelegate.

my question is "can I add UIWebView inside the V开发者_高级运维iew ?"

and if make a special class (UIView class) for the view which method behaves like viewDidload ?


Yes you can add a UIWebView inside the View.

[UIWebView *aWebView =[[UIWebView alloc] initWithFrame:CGRectMake(x,y,width,height)]; aWebView.delegate=self; 
[self.view addSubview:aWebView]; 
[aWebView release];

In the above code you set the delegate of the WebView as self. So you can use the delegate methods of UIWebView like

 - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
 - (void)webViewDidFinishLoad:(UIWebView *)webView

Refer this link for more details.

http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UIWebViewDelegate_Protocol/Reference/Reference.html#//apple_ref/occ/intf/UIWebViewDelegate


Yes you can simply use -

In your h file add

    @interface YourView:UIView<UIWebViewDelegate>{

in your m file add

    CGRect frame = CGRectMake(0,0,200,200);
    UIWebView *webView =[[UIWebView alloc] initWithFrame:frame)];
    webView.delegate = self;
    [self addSubview:webView];


   CGRect  rect=CGRectMake(x,y,width,height)  ;
   UIWebView *webView1 =[[UIWebView alloc] initWithFrame:rect)];
   webView1.delegate=self;
   [self.view addSubview:webView1];
   [webView1 release];


If you view's outlet is named myView:

.h file

@interface myViewController:UIViewController <UIWebViewDelegate> {
    IBOutlet UIView *myView;
}

.m file

UIWebView *,myWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0,0,myView.frame.size.width,myView.frame.size.height)];
myWebView.delegate = self;
[myView addSubView:myWebView];

It's not necissary to create another subclass to listen for deleagte methods of your view, i would now treat your UIView as a UIWebView and thus use UIWebView's delegate methods, like so:

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

more delegate methods can be found here in the documentation. You could also use NSNotificationCenter to listen for your own events application wide if you wish to fire something in a different class when the UIWebView is loaded.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜