如何使用Storyboard中的UIBarButtonItem设置操作

时间:2016-06-19 22:04:56

标签: ios swift storyboard uibarbuttonitem

我知道如果我以编程方式这样做,我会创建一个动作菜单项:

share = UIBarButtonItem(
                title: "Continue",
                style: .Plain,
                target: self,
                action: "Pun"

我正在尝试使用故事板。有一个简单的方法吗?我正在尝试创建一个共享按钮,它将复制一个字符串并提示用户共享它(通过电子邮件,打印机等)。谢谢。这是我的代码:

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var scrollView: UIScrollView!
    let screenSize: CGRect = UIScreen.mainScreen().bounds
    let btn1 = UIButton(frame: CGRect(x: 0, y: 0, width:100, height: 50))

    @IBOutlet weak var share: UIBarButtonItem!


    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        btn1.backgroundColor = UIColor.clearColor()
        btn1.layer.cornerRadius = 5
        btn1.layer.borderWidth = 1
        btn1.layer.borderColor = UIColor.redColor().CGColor

        btn1.setTitle("1", forState: .Normal)


        scrollView.addSubview(btn1)
        scrollView.contentSize.height = 900

        /*share = UIBarButtonItem(
            title: "Continue",
            style: .Plain,
            target: self,
            action: "Pun"
        )*/



    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // Add this
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()

        var newFrame = btn1.frame
        newFrame.size.width = scrollView.bounds.width
        btn1.frame = newFrame
    }


}

1 个答案:

答案 0 :(得分:1)

您需要创建要执行的功能,然后转到故事板并单击UI栏按钮项。然后,您需要单击属性菜单右侧的连接检查器。然后将“Touch Up Inside”拖到视图中并选择功能。