【问题标题】:How to connect multiple buttons in a storyboard to a single action, by code not drag&drop?如何通过代码而不是拖放将情节提要中的多个按钮连接到单个操作?
【发布时间】:2016-12-29 10:44:11
【问题描述】:

我目前正在使用 Xcode 7.3,我只是想知道如何在不同的 UIView 中选择多个按钮,但在单个视图控制器下。 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]


}


}

【问题讨论】:

  • 不确定您要做什么。您是否正在尝试执行 button(s).addTarget(~~~) 之类的操作?

标签: swift xcode uibutton actionevent


【解决方案1】:

编辑答案:

好的,我想我明白你现在在问什么了。您希望相同的函数 action(sender: UIButton) 根据调用它的按钮执行不同的操作。

您实际上可以设置一个名为tag 的变量并设置一个数字以使其唯一。例如btnL.tag = 0btnC.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) { }

【讨论】:

  • here I want to do more than one action , but all of buttons这个说法我看不懂。
  • 目前还不清楚你在问什么,@RudinaTafhasaj。我建议编辑您的问题以更好地解释事情。添加代码很有帮助!
  • 我写了代码,我会感激不尽。我是初学者:)
  • 非常感谢???。我会试试的
【解决方案2】:

您可能可以遍历所有要添加操作的按钮

for button in [button1, button2, button3, button4] {
    button.addTarget(self, action: #selector(self.someFunction), forControlEvents: .TouchUpInside)
}

【讨论】:

    【解决方案3】:

    搜索所有按钮并添加相同的目标:

    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)
             }
        }
    }
    

    【讨论】:

    • 如果我的回答对你有帮助,你能接受吗?谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-14
    • 2017-12-24
    • 2018-05-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多