我創建了UIAlertView有2個按鈕正面的按鈕和負面的按鈕。 AlertView也是viewcontroller。如何發送事件,當一個按鈕點擊
我從Main viewController打開AlertVC。
這裏是我的AlertVC
class AlertVC: UIViewController {
var transitioner : CAVTransitioner
@IBOutlet weak var alertPositiveBtn: IFOButton!
@IBOutlet weak var alertNegativeBtn: IFOButton!
override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
self.transitioner = CAVTransitioner()
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
self.modalPresentationStyle = .custom
self.transitioningDelegate = self.transitioner
}
convenience init() {
self.init(nibName:nil, bundle:nil)
}
required init?(coder: NSCoder) {
fatalError("NSCoding not supported")
}
@IBAction func postiveBtnPressed(_ sender: IFOButton) {
}
@IBAction func negativeBtnPressed(_ sender: IFOButton) {
}
@IBAction func closeBtnPressed(_ sender: UIButton) {
self.presentingViewController?.dismiss(animated: true, completion: nil)
}
}
我想要什麼:我想MainViewController莫名其妙地檢測哪個按鈕被按下陰性或陽性。
有人可以告訴我,我怎麼能這樣做?
更新:使用委託模式後
@IBAction func positiveBtnPressed(_ sender: IFOButton) {
delegate?.positiveBtnPressed(onAlertVC: self)
self.presentingViewController?.dismiss(animated: true, completion: nil)
}
@IBAction func negativeBtnPressed(_ sender: IFOButton) {
delegate?.negativeBtnPressed(onAlertVC: self)
self.presentingViewController?.dismiss(animated: true, completion: nil)
}
這是我做了MainViewController
class MainViewController: UIViewController, AlertVCDelegate
,這裏是我的功能
func positiveBtnPressed(onAlertVC: IFOAlertVC) {
print("Pos")
}
func negativeBtnPressed(onAlertVC: IFOAlertVC) {
print("Neg")}
它仍然沒有被叫。
請給出標籤值alertPositiveBtn和alertNegativeBtn,並且您可以將此標籤值傳遞給您控制器 –
您可以使用'委託/協議'來做到這一點。 –