swift - 如何通过代码而不是拖放将 Storyboard中的多个按钮连接到一个 Action ?

标签 swift xcode uibutton actionevent

我目前正在使用 Xcode 7.3,我只是想知道如何跨不同的 UIView 选择多个按钮,但在单个 View Controller 下。 CMD 点击似乎不起作用。我需要此功能,因为在 Xcode 7.3 中,让 IB 上的两个按钮共享相同操作的唯一方法是一次选择它们,然后将操作拖动到 View Controller 。我是通过代码来做的,没有拖放

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]


}


}

最佳答案

编辑后的答案:

好的,我想我现在明白你在问什么了。您希望相同的函数 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 在类之上。

要添加一个 Action ,我们在您的类 (viewController) 中调用上述函数,该函数可以访问变量按钮:

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

您需要为每个 UIButton 重复此操作,但选择器函数将完全相同(ei、button1.addTarget(...)、button2.addTarget(...) 等)。

你的函数看起来像这样:

func sender(sender: UIButton) { }

关于swift - 如何通过代码而不是拖放将 Storyboard中的多个按钮连接到一个 Action ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39087028/

相关文章:

ios - 如何通过 Swift 3 在字典中获取具有单个值的不同键

ios - observeValueForKeyPath 在 swift 中崩溃应用程序

ios - fatal error : Index out of range, 由于反复按赞和心形按钮

cocoa-touch - 标题未显示为 UIButton

ios - Swift:在内部类中添加按钮目标

ios - viewDidLoad 中的代码在每次调用时运行

swift + OS X 沙盒:将 'NSVBOpenPanel' 视为 'NSOpenPanel'::因为我需要在委托(delegate)方法中获取发件人

iPhone 开发人员与任何有效证书不匹配 (Xcode)

iphone - 按下按钮时停止声音 iOS

ios - React-Native - iOS - 禁用手动屏幕旋转