user190494
user190494

Reputation: 509

How to set up an action with UIBarButtonItem made in Storyboard

I know if I were to do so programmatically, I would create the action menu item with:

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

I am trying to do so using the storyboard. Is there a simple way of doing this? I am trying to create a share button that will copy a string and prompt the user to share it (via email, printer, etc.). Thanks. Here is my code:

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
    }


}

Upvotes: 0

Views: 1528

Answers (1)

Pete
Pete

Reputation: 84

You will need to create the function you want executed, and then go onto storyboard and click on the UI Bar Button Item. Then you will need to click on the connections inspector at the very right of the properties menu. Then drag "Touch Up Inside" to the view and select the function.

Upvotes: 3

Related Questions