问题描述
- 使用变量语法打开URL地址
-
有一个变量agencyWebsite
和一个标签,应该在点击下面方法的时候打开一个网站。- (void)website1LblTapped { NSURL *url = [NSURL URLWithString:self.agencyWebsite]; [[UIApplication sharedApplication] openURL:url]; }
在编译器的警报:
Incompatible pointer types sending UILabel* to parameter of type NSString*
再点击网站应用就会崩溃。不知道应该怎么解决?请高手指点一下,谢谢。
下面是设置label点击的代码:
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]];
解决方案
If 如何agencyWebsite
是UILabel*类型,你需要访问它的text属性,不应该传递对象本身到 URLWithString
:
- (void)website1LblTapped {
NSURL *url = [NSURL URLWithString:self.agencyWebsite.text];
[[UIApplication sharedApplication] openURL:url];
}
调用 self.agencyWebsite
会返回您的UILabel*
对象。同时self.agencyWebsite.text
会返回包含标签text的NSString*
对象。
时间: 2024-09-27 22:45:20