2016-08-22 34 views
1

我目前正在使用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

不知道你在做什麼。你是否想要做一些按鈕(s).addTarget(~~~)? – JSNoob

回答

1

EDITED答:

好吧,我想我明白你現在問什麼。您希望具有相同的功能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) { } 
+0

'在這裏我想做多個動作,但所有的按鈕'我不明白這個說法。 –

+0

現在還不清楚你在問什麼,@RudinaTafhasaj。我建議編輯你的問題以更好地解釋事情。添加代碼很有幫助! –

+0

我寫了代碼,我會很感激。我是初學者:) –

0

你可以通過你要添加動作

for button in [button1, button2, button3, button4] { 
    button.addTarget(self, action: #selector(self.someFunction), forControlEvents: .TouchUpInside) 
} 
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) 
     } 
    } 
} 
+0

如果我的回答對你有幫助,你能接受嗎?謝謝! – penatheboss

相關問題