Swift 访问按钮';s@IB可从中检测变量';s@IBAction
我正在故事板中创建打开URL的按钮视图,URL可以在Inspector中设置为每个按钮的@IBInspectable 我的问题是,我只能将按钮的操作连接到ViewController中的@iAction,自定义类CustomButton的变量或函数似乎无法从中访问 ViewController.swiftSwift 访问按钮';s@IB可从中检测变量';s@IBAction,swift,Swift,我正在故事板中创建打开URL的按钮视图,URL可以在Inspector中设置为每个按钮的@IBInspectable 我的问题是,我只能将按钮的操作连接到ViewController中的@iAction,自定义类CustomButton的变量或函数似乎无法从中访问 ViewController.swift import UIKit class ViewController: UIViewController { @IBAction func openURL(sender: CustomButt
import UIKit
class ViewController: UIViewController {
@IBAction func openURL(sender: CustomButton) {
let theURL = self.myURL; // accesses parent ViewController, not CustomButton, where the variable is defined for this view
UIApplication.sharedApplication().openURL(NSURL(string: theURL)!)
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
import UIKit
class CustomButton: UIButton {
@IBInspectable var myURL: String?
@IBInspectable var myPhone: String?
}
}
按钮。swift
import UIKit
class ViewController: UIViewController {
@IBAction func openURL(sender: CustomButton) {
let theURL = self.myURL; // accesses parent ViewController, not CustomButton, where the variable is defined for this view
UIApplication.sharedApplication().openURL(NSURL(string: theURL)!)
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
import UIKit
class CustomButton: UIButton {
@IBInspectable var myURL: String?
@IBInspectable var myPhone: String?
}
改变
到
问问自己
self
是什么意思。