Reputation: 2327
I've created UIAlertView that has 2 buttons positive button and negative button. AlertView is viewcontroller as well.
I am opening AlertVC from Main viewController.
Here is my 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)
}
}
What I want: I want MainViewController somehow detect which button pressed negative or positive.
Is anyone can tell me how could I do this?
UPDATE: after using delegate pattern
@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)
}
Here is what I did on MainViewController
class MainViewController: UIViewController, AlertVCDelegate
and here is my functions
func positiveBtnPressed(onAlertVC: IFOAlertVC) {
print("Pos")
}
func negativeBtnPressed(onAlertVC: IFOAlertVC) {
print("Neg")}
It still not being called.
Upvotes: 0
Views: 122
Reputation: 1993
This is a textbook example of the delegate pattern.
Add a protocol to your AlertVC:
protocol AlertVCDelegate : class {
func positiveBtnPressed(onAlertVC: AlertVC)
func negativeBtnPressed(onAlertVC: AlertVC)
}
Then create a weak
property in your AlertVC class and pass the button presses to it:
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)
}
}
Implement the AlertVCDelegate
protocol in your MainViewController
, and set the delegate
when you present the AlertVC
from your MainViewController
.
If you present the alert vc from a segue, use the prepare(for: sender:)
method to set the MainViewController
as the delegate.
Upvotes: 2
Reputation: 1862
You have to CTRL + Drag the button from your app Storyboard to their own method.
Your IFOButton should be an UIButton derived class (inheritance).
Even you only need one method to do that
@IBAction internal func handleButtonTap(_ sendder: UIButton) -> Void
{
if sender === alertPositiveBtn
{
// Do something positive here
}
else
{
// Do something negative here
}
}
Upvotes: 0