使用可变语法打开URL

时间:2013-04-15 15:11:41

标签: iphone ios objective-c

我有一个变量agencyWebsite和一个标签,当用这种方法点击时应该打开网站:

- (void)website1LblTapped {
    NSURL *url = [NSURL URLWithString:self.agencyWebsite];
    [[UIApplication sharedApplication] openURL:url];
}

我在编译器中收到警告:

Incompatible pointer types sending UILabel* to parameter of type NSString*

单击链接时应用程序崩溃。有什么建议吗?

编辑:我正在做的是使标签可点击

UITapGestureRecognizer* website1LblGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(website1LblTapped)];
    // if labelView is not set userInteractionEnabled, you must do so
    [self.agencyWebsite setUserInteractionEnabled:YES];
    [self.agencyWebsite addGestureRecognizer:website1LblGesture];

我以前用它来工作

 NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://%@", self.agencyWebsite.text]];

1 个答案:

答案 0 :(得分:1)

如果agencyWebsite的类型为UILabel*,则需要访问其text属性,而不是将对象本身传递给URLWithString:

- (void)website1LblTapped {

    NSURL *url = [NSURL URLWithString:self.agencyWebsite.text];
    [[UIApplication sharedApplication] openURL:url];
}

调用self.agencyWebsite将返回您的UILabel*对象,而self.agencyWebsite.text将返回包含标签文字的NSString*对象。