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

标签 ios swift storyboard uibarbuttonitem

我知道如果我以编程方式这样做,我会使用以下命令创建操作菜单项:

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

我正在尝试使用 Storyboard 来做到这一点。有没有一种简单的方法可以做到这一点?我正在尝试创建一个共享按钮,它将复制一个字符串并提示用户共享它(通过电子邮件、打印机等)。谢谢。这是我的代码:

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
    }


}

最佳答案

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

关于ios - 如何使用 Storyboard 中制作的 UIBarButtonItem 设置操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37912304/

相关文章:

ios - iPhone 模拟器与主 Storyboard预览

iOS - 保存到库/缓存/会导致错误吗?

swift - 过滤字典数组并更改其属性

ios - 在 swift 4.2 的表格 View 中保存勾选单元格的状态

swift - 如何以 littleEndian 方式在 Swift 中对 NSData 输出进行字节反转?

ios - 当用户快速点击一个单元格时触发一个 Action

objective-c - 如何在 AppDelegate 中执行 Segue?

objective-c - 将 Storyboard添加到现有项目

ios - 在我的情况下,在父子 Controller 之间共享数据的最佳方式是什么? (不确定我是否需要委托(delegate)或共享数据状态)

ios - Sinch iOS API 支持 CallKit 框架吗?