我已经为我的iOS应用程序中的重复创建了一个.xib文件,其中有一个UIButton。
我在故事板中的多个UIViewControllers中包含了.xib视图。我想将@IBAction和@IBOutlet链接到我的.xib视图中的按钮,该视图特定于每个UIViewController。换句话说,我希望每个UIViewController都能完全管理和处理.xib视图中的UIButton。
知道上述是否可行?
有几种方法可以做你想要的。
我这样做的方法是给你的自定义视图一个在触发IBAction方法时运行的闭包。并且从xib加载视图的每个视图控制器都可以将闭包传递给视图,并且当单击按钮时操作将运行。
所以这是我迄今为止提出的最佳解决方案。
在包含xib视图的ViewController中,只需将操作分配给xib视图中的按钮
class YourViewController:UIViewController{
override func viewDidLoad(){
//ListTitleView : a xib view, action button witch called theManageButton is inside it.
let theListTitleView = ListTitleView.init(frame:CGRect.init(x: 0, y: 0, width: self.view.frame.width, height: 100))
//Add action to theManageButton
theListTitleView.theManageButton.addTarget(self, action: #selector(yourFunction(Sender:)), for: .touchUpInside)
}
func yourFunction(Sender:UIButton){
//...Do something here
}
}