【问题标题】:Semantic Issue on setDelegatesetDelegate 上的语义问题
【发布时间】:2013-05-26 21:30:38
【问题描述】:

我的代码存在语义问题。我有一个 UIWebView,我添加了一条错误消息,所以如果没有互联网连接,则会弹出一个错误。

这是我的 .m 文件中 UIWebView 的编码

- (void)viewDidLoad {
[super viewDidLoad];
NSString *fullURL = @"http://example.com";
NSURL *url = [NSURL URLWithString:fullURL];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[website setDelegate:self];
[website loadRequest:requestObj]; }

错误在此代码上

[website setDelegate:self];

它说这是一个语义问题,错误是 Sending FirstViewController *const_strong to parameter of in compatible type id

如果没有互联网连接,这是错误消息的代码

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"error" message:@"error connecting to the internet" delegate:self
cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
[alert show]; }

【问题讨论】:

    标签: ios objective-c cocoa-touch uiwebview


    【解决方案1】:

    您需要指定 FirstViewController 实现协议。比如:

    @interface FirstViewController ()  <UIWebViewDelegate>
    @end
    

    编译器当前警告您,您正在设置一个委托,该委托未指定它实现所需的协议,因此不会执行编译检查。

    【讨论】:

      【解决方案2】:

      您的视图控制器应通过在@interface 行的末尾添加“&lt;UIWebViewDelegate&gt;”来指定它符合UIWebViewDelegate,或者在.h 的主要@interface 声明中:

      @interface MyViewController : UIViewController <UIWebViewDelegate>
      

      .m 文件中的私有类扩展(如果有的话):

      @interface MyViewController () <UIWebViewDelegate>
      

      请参阅Objective-C 编程指南中的Conforming to Protocols

      【讨论】:

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