我目前正在使用Xcode 7.3,我只是想知道如何在不同的UIViews中选择多个按钮,但在单个视图控制器下。 CMD点击似乎不起作用。我需要这个功能,因为在Xcode 7.3中,让IB上的两个按钮共享相同动作的唯一方法是一次选择它们然后将动作拖动到视图控制器。我是通过代码执行此操作而不是拖放操作降
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]
}
}
答案 0 :(得分:1)
编辑回答:
好的,我想我明白你现在在问什么。您希望具有相同的功能action(sender: UIButton)
根据调用它的按钮执行不同的操作。
您实际上可以设置一个名为tag
的变量并设置一个数字以使其唯一。例如btnL.tag = 0
和btnC.tag = 1
那么你的函数action
看起来像这样:
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)
}
}
UIButton具有执行此操作的功能,具体为:
public func addTarget(target: AnyObject?, action: Selector, forControlEvents controlEvents: UIControlEvents)
按钮需要像这样声明:@IBOutlet var button: UIButton
在类之上。
要添加一个动作,我们在您的类(viewController)中调用可以访问变量按钮的上述函数:
button.addTarget(self, action: #selector(self.sender(_:)), forControlEvents: .TouchDown)
你需要为每个UIButton重复这个,但是选择器函数将是相同的(ei,button1.addTarget(...),button2.addTarget(...)等)。
您的功能将如下所示:
func sender(sender: UIButton) { }
答案 1 :(得分:0)
您可以遍历要添加操作的所有按钮
for button in [button1, button2, button3, button4] {
button.addTarget(self, action: #selector(self.someFunction), forControlEvents: .TouchUpInside)
}
答案 2 :(得分:0)
搜索所有按钮并添加相同的目标:
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)
}
}
}