我有一个变量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]];
最佳答案
如果agencyWebsite
的类型为UILabel*
,则需要访问其text
属性,而不是将对象本身传递给URLWithString:
。
- (void)website1LblTapped {
NSURL *url = [NSURL URLWithString:self.agencyWebsite.text];
[[UIApplication sharedApplication] openURL:url];
}
调用
self.agencyWebsite
将返回您的UILabel*
对象,而self.agencyWebsite.text
将返回一个包含标签文本的NSString*
对象。关于iphone - 使用可变语法打开网址,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16018723/