【问题标题】:ios : UIWebview and its delegate methods in same class not workingios:同一类中的 UIWebview 及其委托方法不起作用
【发布时间】:2012-11-20 13:08:27
【问题描述】:

我想在 safari 浏览器中打开 uiwebview 的链接,如果我在 viewController 中实现 shouldStartLoadWithRequest 方法,我的代码可以正常工作,但是当我在同一个类中实现 shouldStartLoadWithRequest 并将 UIWebView 的委托设置为 self 时,它不起作用它会停止之间并显示错误的汇编级代码 EXC_BAD_ACCESS(code=2, address=0x9) 我的文件如下

//ShowView.h 文件内容

#import <UIKit/UIKit.h>
@interface ShowView : UIView <UIWebViewDelegate> {

}

- (void) showViewFunction;

@property (nonatomic, assign) UIViewController *mainViewContObj;

@end

//ShowView.m 文件内容为:

#import "ShowView.h"

@implementation ShowView

- (void) showViewFunction {

    UIWebView *aWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 150)];
    aWebView.autoresizesSubviews = YES;
    aWebView.autoresizingMask = (UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth);
    [aWebView setDelegate:self];
    NSString *urlAddress = @"http://localhost/test/index.php";
    NSURL *url = [NSURL URLWithString:urlAddress];
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
    aWebView.delegate = self;
    [aWebView loadRequest:requestObj];
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
    [[[self mainViewContObj] view] addSubview:aWebView];
}



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

    NSLog(@"In shouldStartLoadWithRequest method");

    if ([[[request URL] absoluteString] isEqual:@"http://localhost/test/index.php"])
        return YES;

    [[UIApplication sharedApplication] openURL:[request URL]];
    return NO;
}


@end

// ViewController.h 的内容

#import "ViewController.h"
#import "ShowView.h"

@interface mnetViewController ()

@end

@implementation mnetViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    MNETMobAd *bannerObj = [[MNETMobAd alloc] init];
    bannerObj.mainViewContObj = self;    
    [bannerObj showAd];

}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

有时会显示 html 页面,但是当我单击链接时,它会在同一个 UIWebview 窗口中打开,甚至没有进入 shouldStartLoadWithRequest 方法,我做错了什么吗?

【问题讨论】:

    标签: ios uiwebview uiwebviewdelegate


    【解决方案1】:

    您的代码不清楚,可能需要更多信息,但据我所知,ShowView 类从未实例化,因此它甚至不应该显示。

    我猜你应该做这样的事情:

    //mnetViewController.m

        #import "mnetViewController.h"
        #import "ShowView.h"
    
        @interface mnetViewController ()
    
        @end
    
        @implementation mnetViewController
    
        - (void)viewDidLoad {
            [super viewDidLoad];
    
        ShowView* theShowView = [[ShowView alloc] initWithFrame:CGRectMake(insert the frame you want your webview to have)];
    theShowView.autoresizesSubviews = YES;
    
        [self.view addSubview:theShowView];
        [theShowView release];
    
            MNETMobAd *bannerObj = [[MNETMobAd alloc] init];
            bannerObj.mainViewContObj = self;    
            [bannerObj showAd];
    
        }
    

    现在对于 ShowView 类,试试这样的:

    //ShowView.h

    #import <UIKit/UIKit.h>
    @interface ShowView : UIView <UIWebViewDelegate> {
    
    }
    @end
    

    //ShowView.m

    #import "ShowView.h"
    
    @implementation ShowView
    
    - (id)initWithFrame:(CGRect)frame
    {
    self = [super initWithFrame:frame];
        if (self) 
        {
     UIWebView *aWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
    
        aWebView.scalesPageToFit = YES;
        [aWebView setDelegate:self];
    [self addSubview:aWebView];    
    
    NSString *urlAddress = @"http://localhost/test/index.php";
        NSURL *url = [NSURL URLWithString:urlAddress];
        NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
        [aWebView loadRequest:requestObj];
    
    [aWebView release];
    }
        return self;
    }
    
    - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request
     navigationType:(UIWebViewNavigationType)navigationType {
    
        NSLog(@"In shouldStartLoadWithRequest method for URL : %@",[request [URL absolutString]]);
    
        if ([[[request URL] absoluteString] isEqual:@"http://localhost/test/index.php"])
            return YES;
    
        [[UIApplication sharedApplication] openURL:[request URL]];
        return NO;
    }
    

    这个应该可以,我没试过,有需要我明天再来试试。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多