从它的@IBAction访问按钮的@IBInspectable变量

时间:2016-07-19 18:04:44

标签: swift

我在故事板中创建了一个打开URL的按钮视图,其中URL可以在Inspector中设置为每个按钮的@IBInspectable。

我的问题是我只能将按钮的动作连接到ViewController中的@IBAction,自定义类CustomButton的变量或函数似乎无法访问。

ViewController.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.
}

}

Button.swift

import UIKit

class CustomButton: UIButton {
    @IBInspectable var myURL: String?
    @IBInspectable var myPhone: String?
}

1 个答案:

答案 0 :(得分:1)

更改

let theURL = self.myURL

let theURL = sender.myURL