CJ90
CJ90

Reputation: 99

how to programmatically perform segue, or present view, when button pressed in swift3

I'm coding an app programatically (no storyboard) and have login screen. the login screen has input boxes for the user details to login and a login and register button.

How do I programatically interact with the buttons to effectively perform a segue to a new view once a button is pressed?

I know that for a navigation button I would use

    navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Logout", style: .plain, target: self, action: #selector(handleLogout))

but is there an equivalent for any button in the view controller? I'm sure there is, and its probably a simple question, but any help would be appreciated.

my button code is

let registerButton: UIButton = {
        let button = UIButton(type: .system)
        button.backgroundColor = UIColor.black
        button.setTitle("Register", for: .normal)
        button.layer.cornerRadius = 5
        button.translatesAutoresizingMaskIntoConstraints = false
        return button
    }()

and is added to the login page as a subview

view.addSubview(registerButton)

Thanks.

Upvotes: 1

Views: 1084

Answers (3)

DoesData
DoesData

Reputation: 7047

Add a target to you button so that a function is executed when the button is tapped.

registerButton.addTarget(self, action: #selector(self.registerTapped(_:)), for: .touchUpInside) 

Create the function registerTapped.

// Swift 4 requires @objc
@objc func registerTapped(_ sender: UIButton){  
    // Segue here
}

Write your segue code and place it inside registerTapped

self.performSegue(withIdentifier: "segueName", sender: nil)

All together now

override func viewDidLoad() {
    super.viewDidLoad()

    registerButton.addTarget(self, action: #selector(self.registerTapped(_:)), for: .touchUpInside) 
}

@objc func registerTapped(_ sender: UIButton) {  
    self.performSegue(withIdentifier: "segueName", sender: nil)
}

Upvotes: 1

slushy
slushy

Reputation: 12385

segue is Interface Builder lingo. In iOS, you can only display a view controller three ways: push, present, show. Programmatically, you wouldn't call it segue. First, add a target to your button:

registerButton.addTarget(self, action: #selector(registerButtonAction), for: .touchUpInside)

Create the action method:

@objc func registerButtonAction() {
    ...
}

And if you want to push to a view controller (using a navigation controller), push to it from inside the action method:

@objc func registerButtonAction() {
    let destination = SomeViewController()
    navigationController?.pushViewController(destination, animated: true)
}

If you want to present it (modally):

@objc func registerButtonAction() {
    let destination = SomeViewController()
    destination.transitioningDelegate = SomePresentationAnimationVendor() // for custom presentations
    destination.modalPresentationStyle = .custom // for custom
    present(destination, animated: true, completion: nil)
}

Upvotes: 1

Arash Etemad
Arash Etemad

Reputation: 1909

you should write function for your button and assign it to button

override func viewDidLoad() {
  super.viewDidLoad()
  ...
  button.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
}

func buttonAction(sender: UIButton!) {
  print("Button tapped")
}

Upvotes: 0

Related Questions