按钮触发另一个视图

时间:2016-07-10 01:09:22

标签: iphone xcode swift button view

现在我有这个:

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

如何让按钮将我带到另一个视图或做其他事情,比如打开相机?

PS:showButton函数没有做任何事情。

2 个答案:

答案 0 :(得分:1)

所以你有

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

但你需要

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

func showButton(sender:UIButton) {
    //Code

}

答案 1 :(得分:0)

我在7.2.1 Xcode

中尝试了这个
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

}

}