2

I'm currently using Xcode 7.3 and I'm simply wondering how to select multiple buttons across different UIViews, but under a single view controller. CMD clicking doesn't seem to work. I need this functionality because in Xcode 7.3 the only way to let two buttons on IB to share the same action is to select them at once and then drag the action to your view controller. I'm doing this by code and no drag & drop

class LogIn: UIViewController {

var mail_tf : UITextField!
var pass_tf : UITextField!
var btnL : UIButton!
var btnC : UIButton!
let cl1 = UIColor (red: 255/255.0, green: 258/255.0, blue: 196/255.0, alpha: 1)
let cl2 = UIColor (red: 238/255.0, green: 233/255.0, blue: 191/255.0, alpha: 1)



override func loadView() {
    super.loadView()


    let backgroundView  = UIImageView(image: UIImage(named: "4")!)
    //backgroundView.layer.cornerRadius = backgroundView.frame.size.width / 2
    backgroundView.frame = CGRectMake(0, 0, 900, 900)
    //backgroundView.clipsToBounds = true
    //backgroundView.layer.borderColor = UIColor.whiteColor().CGColor
   // backgroundView.layer.borderWidth = 1

    self.view.addSubview(backgroundView)




    mail_tf = UITextField()
    mail_tf.frame = CGRectMake(95, 90, 190, 60)
    mail_tf.layer.cornerRadius = 10
    mail_tf.layer.borderWidth = 1
    mail_tf.layer.borderColor = UIColor.blackColor().CGColor
    mail_tf.placeholder = "e-mail"
    mail_tf.textColor = UIColor.redColor()
    mail_tf.backgroundColor = cl1
    mail_tf.borderStyle = UITextBorderStyle.RoundedRect


    self.view.addSubview(mail_tf)


    pass_tf = UITextField()
    pass_tf.frame = CGRectMake(95, 190, 190, 60)
    pass_tf.layer.cornerRadius = 10
    pass_tf.layer.borderWidth = 1
    pass_tf.layer.borderColor = UIColor.blackColor().CGColor
    pass_tf.placeholder = "password"
    pass_tf.secureTextEntry = true
    pass_tf.textColor = UIColor.redColor()
    pass_tf.backgroundColor = cl1
    pass_tf.borderStyle = UITextBorderStyle.RoundedRect

    self.view.addSubview(pass_tf)


    btnL = UIButton()
    btnL.frame = CGRectMake(195, 260, 90, 40)
    btnL.setTitle("Log In ", forState: UIControlState.Normal)
    btnL.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
    btnL.layer.borderWidth = 1
    btnL.layer.borderColor =  UIColor.blackColor().CGColor
    btnL.layer.cornerRadius = 10
    btnL.backgroundColor = cl1


    self.view.addSubview(btnL)
    btnL.addTarget(self , action: #selector(self.action(_:)), forControlEvents: UIControlEvents.TouchUpInside)


    btnC = UIButton()
    btnC.frame = CGRectMake(228, 350, 150, 40)
    btnC.setTitle("Create new account ", forState: UIControlState.Normal)
    btnC.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
    btnC.layer.borderWidth = 1
    btnC.layer.borderColor =  UIColor.blackColor().CGColor
    btnC.layer.cornerRadius = 10
    btnC.backgroundColor = cl1

    self.view.addSubview(btnC)
    btnC.addTarget(self , action: #selector(self.action(_:)), forControlEvents: UIControlEvents.TouchUpInside)
}



 func action (sender: UIButton!) {

    // btnL & btnC open the Logged_in file . there is the problem 

        let vc = Logged_In()
        let navigation = UINavigationController(rootViewController:vc)
        self.navigationController?.presentViewController(navigation, animated: true, completion: nil)



        let vc1 = create()
        let navigation1 = UINavigationController(rootViewController:vc1)
        self.navigationController?.presentViewController(navigation1, animated: true, completion:  nil)



}

override func viewDidLoad() {
    super.viewDidLoad()

    self.navigationItem.title = "Log in"
    self.navigationController?.navigationBar.barTintColor = UIColor.blackColor()
    self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: cl1]


}


}
Pranav Wadhwa
  • 7,666
  • 6
  • 39
  • 61
  • Not sure what you are trying to do. Are you trying to do something like button(s).addTarget(~~~)? – JSNoob Aug 22 '16 at 19:15

3 Answers3

1

EDITED ANSWER:

Okay, I think I understand what you are asking now. You want to have the same function action(sender: UIButton) do a different action depending on the button that calls it.

You can actually set a variable called tag and set a number to make it unique. For example btnL.tag = 0 and btnC.tag = 1

So then your function action would look like this:

    func action (sender: UIButton!) {
    if sender.tag == 0 {
        let vc = Logged_In()
        let navigation = UINavigationController(rootViewController:vc)
        self.navigationController?.presentViewController(navigation, animated: true, completion: nil)
    } else if sender.tag == 1 {
        let vc1 = create()
        let navigation1 = UINavigationController(rootViewController:vc1)
        self.navigationController?.presentViewController(navigation1, animated: true, completion:  nil)
    }
}

A UIButton has a function that does this, specifically:

public func addTarget(target: AnyObject?, action: Selector, forControlEvents controlEvents: UIControlEvents)

The buttons need to be declared like this: @IBOutlet var button: UIButton above the class.

To add an action we call the above function in your class (viewController) that has access to variable button:

button.addTarget(self, action: #selector(self.sender(_:)), forControlEvents: .TouchDown)

You'll need to repeat this for each UIButton but the selector function will just be the same (ei, button1.addTarget(...), button2.addTarget(...), etc).

Your function will look something like this:

func sender(sender: UIButton) { }
John Riselvato
  • 12,854
  • 5
  • 62
  • 89
0

You can probably iterate through all the buttons you want to add action to

for button in [button1, button2, button3, button4] {
    button.addTarget(self, action: #selector(self.someFunction), forControlEvents: .TouchUpInside)
}
John Riselvato
  • 12,854
  • 5
  • 62
  • 89
JSNoob
  • 1,477
  • 2
  • 18
  • 31
0

Search for all of your buttons and add the same target:

override func viewDidLayoutSubviews() {//This cannot be view did load; it can be view did appear or view will appear
    super.viewDidLayoutSubviews()
    for subview in self.view.subviews {
         if let button = subview as? UIButton {
             button.addTarget(target: self, action: #selector(self.someFunc), forControlEvents: .touchUpInside)
         }
    }
}
Pranav Wadhwa
  • 7,666
  • 6
  • 39
  • 61