有一個變量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*
對象。