2017-01-13 27 views
0

我創建了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")} 

它仍然沒有被叫。

+0

請給出標籤值alertPositiveBtn和alertNegativeBtn,並且您可以將此標籤值傳遞給您控制器 –

+0

您可以使用'委託/協議'來做到這一點。 –

回答

2

這是delegate pattern的典型例子。

添加一個協議,你AlertVC:

protocol AlertVCDelegate : class { 
    func positiveBtnPressed(onAlertVC: AlertVC) 
    func negativeBtnPressed(onAlertVC: AlertVC) 
} 

然後在您的AlertVC類中創建一個weak財產並通過按下按鈕到它:

class AlertVC : UIViewController { 
    weak var delegate: AlertVCDelegate? 
    ... 
    @IBAction func postiveBtnPressed(_ sender: IFOButton) { 
     delegate?.positiveBtnPressed(onAlertVC: self) 
    } 


    @IBAction func negativeBtnPressed(_ sender: IFOButton) { 
     delegate?.negativeBtnPressed(onAlertVC: self) 
    } 
} 

落實AlertVCDelegate協議在MainViewController,並在您的MainViewController中提供AlertVC時設置delegate

如果您顯示來自segue的alert vc,請使用prepare(for: sender:)方法將MainViewController設置爲委託。

+0

這是一個非常好的答案。 (投票) –

+0

@DuncanC是的,你說得對,這是非常好的答案。 – pmb

+0

@David Ganster謝謝你的回答。我完全按照你所說的做了,但在MainViewController上的功能沒有被調用。我有什麼可以錯過的嗎?查看我更新的問題plz – pmb

0

您必須按CTRL +將您的應用程序Storyboard中的按鈕拖放到自己的方法中。

您的IFOButton應該是一個UIButton派生類(繼承)。

即使你只需要一個方法,這樣做

@IBAction internal func handleButtonTap(_ sendder: UIButton) -> Void 
{ 
    if sender === alertPositiveBtn 
    { 
     // Do something positive here 
    } 
    else 
    { 
     // Do something negative here 
    } 
}