0

我有一个变量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]];
4

1 回答 1

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*包含标签文本的对象。

于 2013-04-15T15:14:14.767 回答