mmk
mmk

Reputation: 192

Navigate to viewController when programmatically created button click in swift

I'm creating a framework module using swift. Need to pop up an alert or ViewController when the button tapped. Buttons are creating programmatically. Currently button is displaying, but actions are not working. Below is the code.
In Framework class

public class CreateAdminMenu {

    let alertVC = AlertView()

    public init() {

    }

    public func createMenuItem(itemTitle: String) -> UIButton{
        let menuItem = UIButton(type: .system)
        menuItem.frame = CGRect(x: 30, y: 30, width: 150, height: 100)
        menuItem.backgroundColor = .blue
        menuItem.setTitle(itemTitle, for: .normal)
        menuItem.addTarget(self, action: #selector(menuItem_touchUpInside(sender:)), for: UIControl.Event.touchDown)
        return menuItem
    }

    @IBAction func menuItem_touchUpInside(sender: UIButton) {
        print("Menu Item tapped")
        alertVC.showAlert()
    }
}

class AlertView: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        //self.showAlert()
    }

    func showAlert() {
        let alert = UIAlertController(title: "Test Item", message: "button tapped", preferredStyle: .alert)
        alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))

        self.present(alert, animated: true)
    }
}

In App, ViewController

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.

        let menu = CreateAdminMenu()
        let item = menu.createMenuItem(itemTitle: "Test Item 1")
        view.addSubview(item)
    }
}

What am I missing?

Upvotes: 0

Views: 300

Answers (1)

mahan
mahan

Reputation: 15035

First and foremost, you should definitely not add the IBAction in framework.

Two issues

  1. You just initialize AlertView without add/embedding it in ViewController.
  2. You are presenting the UIAlertController in another view.
       public class CreateAdminMenu {

            public init() {

            }

            public func createMenuItem(itemTitle: String) -> UIButton {
                let menuItem = UIButton(type: .system)
                menuItem.frame = CGRect(x: 30, y: 30, width: 150, height: 100)
                menuItem.backgroundColor = .blue
                menuItem.setTitle(itemTitle, for: .normal)
                return menuItem
            }
        }

        class AlertView  {
            func showAlert() -> UIAlertController {
                let alert = UIAlertController(title: "Test Item", message: "button tapped", preferredStyle: .alert)
                alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
                return alert
            }
        }


        class ViewController: UIViewController {

            override func viewDidLoad() {
                super.viewDidLoad()
                // Do any additional setup after loading the view.

                let menu = CreateAdminMenu()
                let item = menu.createMenuItem(itemTitle: "Test Item 1")
                item.addTarget(self, action: #selector(menuItem_touchUpInside(sender:)), for: UIControl.Event.touchDown)
                view.addSubview(item)
            }

            @IBAction func menuItem_touchUpInside(sender: UIButton) {
                    let alert = AlertView()
                self.present(alert.showAlert(), animated: true, completion: nil)
            }
        }

Upvotes: 1

Related Questions