-1

How can i set the delegate method of a UIWebView in a class?

when i do it, the app carsh.


@interface MineWebViewHandle : NSObject<UIWebViewDelegate>

@end

//.m

@implementation MineWebViewHandle

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    NSString *urlString = [[request URL] absoluteString];

    return YES;
}

- (void)webViewDidStartLoad:(UIWebView *)webView
{
    NSLog(@"did start load");
}

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

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    NSLog(@"webview error:%@",[error localizedDescription]);
}

i use it:

    self.m_pWebView = [[UIWebView alloc] initWithFrame:CGRectZero];
    MineWebViewHandle *handle = [[MineWebViewHandle alloc]init];
    self.m_pWebView.delegate = handle;
    self.m_pWebView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.m_pWebView];

how do i use it rightly?

user2305019
  • 39
  • 1
  • 6

2 Answers2

0

Your delegate look like is create in viewDidLoad,and in the end of this it´s release (put to nil).

You need create a property called: MineWebViewHandle *handle.

in your viewController.h add:

 @property (nonatomic,strong)MineWebViewHandle *handle;

and change in your code:

self.handle = [[MineWebViewHandle alloc]init];
self.m_pWebView.delegate = self. handle;

And it´s good idea your webView will have dimensions, change this:

self.m_pWebView = [[UIWebView alloc] initWithFrame:self.view.frame];
Onik IV
  • 5,007
  • 2
  • 18
  • 23
  • Thank you! this helps me! yes,i did it in viewDidLoad! but i think that In ARC, "MineWebViewHandle *handle = [[MineWebViewHandle alloc]init]" handle will have a strong point.why is it released? – user2305019 Nov 17 '14 at 12:38
  • No matter how the strong points an object has, if its reference counting is zero the object will be released. The problem is your object handle has a +1 (alloc), but at the end of the methods receive a -1, and is destructed. Nevertheless, if you put it in an strong property another +1 will be added. – Onik IV Nov 17 '14 at 13:07
-1

Webview is the UIView, Not a controler.

Please remove the NSObject and add the UIview

@interface MineWebViewHandle : UIView
@end
J. Chomel
  • 8,193
  • 15
  • 41
  • 69
Heman hijack
  • 209
  • 3
  • 9