jeudi 30 juin 2016

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
    }


}

Aucun commentaire:

Enregistrer un commentaire