我有一个变量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]];
答案 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*
对象。