2016-07-10 1 views
0

J'ai en ce moment ceci:déclenche une autre vue

lazy var Button: UIButton = { 
    let tb = UIButton() 
    tb.backgroundColor = UIColor.yellowColor() 
    tb.clipsToBounds = true 
    tb.layer.cornerRadius = 39 
    tb.addTarget(self, action: #selector(showButton), forControlEvents: .TouchUpInside) 
    tb.setBackgroundImage(UIImage(named: "buttonMenu"), forState: UIControlState.Normal) 
    return tb 
}() 

Comment puis-je me faire prendre le bouton pour une autre vue ou faire d'autres choses comme l'ouverture de l'appareil photo? PS: La fonction showButton ne fait vraiment rien.

+0

code ci-dessus dans viewcontroller 1 et que vous voulez accéder au bouton dans un autre viewcontroller comme un droit d'accès à la fonction? –

Répondre

1

Vous avez donc

tb.addTarget(self, action: #selector(showButton), forControlEvents: .TouchUpInside) 

mais vous avez besoin

tb.addTarget(self, action: #selector(className.showButton), forControlEvents: .TouchUpInside) 

func showButton(sender:UIButton) { 
    //Code 

} 
+0

et cette classe devrait avoir les éléments de la nouvelle vue et les contraintes? ou devrais-je l'écrire à l'intérieur de func showButton? –

+0

Écrivez-le à l'intérieur. – impression7vx

+0

'Laisser view = UIView (frame: CGRect (0, 0, 100, 100))'. 'view.backgroundColor = UIColor.redColor()'. 'Self.view.addSubview (voir)' – impression7vx

0

J'ai essayé dans Xcode 7.2.1

import UIKit 
import CoreLocation 

class ViewController: UIViewController { 

override func viewDidLoad() { 
    super.viewDidLoad() 


    let Button: UIButton = { 
     let tb = UIButton() 
     tb.frame = CGRectMake(0, 0, 100, 50) 
     tb.backgroundColor = UIColor.yellowColor() 
     tb.clipsToBounds = true 
     tb.layer.cornerRadius = 39 


     tb.addTarget(self, action: "showButton:", forControlEvents: .TouchUpInside) 
     // only change the action syntax like "action: "showButton:" " 
     return tb 
    }() 

    self.view .addSubview(Button) 

      // Do any additional setup after loading the view, typically from a nib. 
} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 
func showButton(sender:AnyObject?) { 
    //Code 

} 

}