How do you assign an IBOutlet programmatically using Swift?

Aaron

I added a bar button item programmatically to a view controller:

override func viewDidLoad() {
    super.viewDidLoad()
    self.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Menu", style: .Plain, target: nil, action: nil)
}

Now I want to assign that bar button item to an IBOutlet programmatically. How would I do that using Swift?

nhgrif

The IB of IBOutlet stands for Interface Builder and it represents a connection from the interface builder to the source code. Assigning an IBOutlet programmatically couldn't make less sense.

An IBOutlet is simply an instance variable of a class which is tied to part of the interface or can be set via the interface builder. If we want a reference to our button (in the same way we'd have a reference to it if we made it in the interface builder), we simply add a property to our class, and then assign our newly created button to that:

class MyViewController: UIViewController {

    var someBarButton: UIBarButtonItem?

    override func viewDidLoad() {
        super.viewDidLoad()
        self.someBarButton = UIBarButtonItem(title: "Menu", style: .Plain, target: nil, action: nil)
        self.navigationItem.leftBarButtonItem = self.someBarButton
    }
}

And now, someBarButton is our "IBOutlet".


Some notes...

There's a pretty decent chance we might not actually want to do things this way.

This creates an extra strong reference to the button (self.navigationItem already holds a strong reference to its leftBarButtonItem).

You'll notice, if you make an IBOutlet from interface builder, it is set up as a weak property. So perhaps we want a weak property?

But we can actually do ourselves one better. Try this on for size:

var leftNavBarButton: UIBarButtonItem? {
    get {
        return self.navigationItem.leftBarButtonItem
    }
    set (newValue) {
        self.navigationItem.leftBarButtonItem = newValue
    }
}

Now, self.leftNavBarButton is essentially just a convenient way of accessing self.navigationItem.leftBarButtonItem.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do I assign an IBOutlet a value using an Initializer?

From Java

How do you create a UIImage View Programmatically - Swift

From Dev

How can i replace a IBOutlet uiview programmatically with a custom view in swift

From Dev

How do you assign a string value to a property in another class in Swift?

From Dev

How do you make buttons programmatically using xamarin?

From Dev

How to assign a custom class programmatically in swift for UIButton

From Dev

How do you have the same IBOutlet on two different ViewControllers if possible?

From Dev

How do I programmatically present a popover in iOS using Swift?

From Dev

How do you programmatically make UIViews which are the height of textLabels inside them? (In Swift)

From Dev

Swift - Using IBOutlet & IBAction in func?

From Dev

How do you programmatically reload a jquery datatable?

From Dev

JavaScript - how do you programmatically calculate colours?

From Dev

How do you create a UIImageView programmatically in Xcode

From Dev

How do you programmatically close a wxPython frame?

From Dev

How do you make this plist file programmatically?

From Dev

How do you create a UIImageView programmatically in Xcode

From Dev

How do you programmatically close a wxPython frame?

From Dev

How do you make this plist file programmatically?

From Dev

How do you programmatically reload a jquery datatable?

From Dev

How do you set the height of a view programmatically?

From Dev

How do you read FULL address using coreLocation ( iOS/ Swift )

From Dev

How do you check if all the contents in the array are the same? Using swift

From Dev

How do you assign a storage file to a ByteArray?

From Dev

How do you assign characterAtIndex to NSString variable?

From Dev

How do you assign an action to a button in JScript?

From Dev

How do you assign commands to keys in Terminal?

From Dev

How to detect if a property is an IBOutlet programmatically at runtime?

From Dev

How do you programmatically create layers in google maps engine lite using ajax and json

From Dev

How do you programmatically create layers in google maps engine lite using ajax and json

Related Related

  1. 1

    How do I assign an IBOutlet a value using an Initializer?

  2. 2

    How do you create a UIImage View Programmatically - Swift

  3. 3

    How can i replace a IBOutlet uiview programmatically with a custom view in swift

  4. 4

    How do you assign a string value to a property in another class in Swift?

  5. 5

    How do you make buttons programmatically using xamarin?

  6. 6

    How to assign a custom class programmatically in swift for UIButton

  7. 7

    How do you have the same IBOutlet on two different ViewControllers if possible?

  8. 8

    How do I programmatically present a popover in iOS using Swift?

  9. 9

    How do you programmatically make UIViews which are the height of textLabels inside them? (In Swift)

  10. 10

    Swift - Using IBOutlet & IBAction in func?

  11. 11

    How do you programmatically reload a jquery datatable?

  12. 12

    JavaScript - how do you programmatically calculate colours?

  13. 13

    How do you create a UIImageView programmatically in Xcode

  14. 14

    How do you programmatically close a wxPython frame?

  15. 15

    How do you make this plist file programmatically?

  16. 16

    How do you create a UIImageView programmatically in Xcode

  17. 17

    How do you programmatically close a wxPython frame?

  18. 18

    How do you make this plist file programmatically?

  19. 19

    How do you programmatically reload a jquery datatable?

  20. 20

    How do you set the height of a view programmatically?

  21. 21

    How do you read FULL address using coreLocation ( iOS/ Swift )

  22. 22

    How do you check if all the contents in the array are the same? Using swift

  23. 23

    How do you assign a storage file to a ByteArray?

  24. 24

    How do you assign characterAtIndex to NSString variable?

  25. 25

    How do you assign an action to a button in JScript?

  26. 26

    How do you assign commands to keys in Terminal?

  27. 27

    How to detect if a property is an IBOutlet programmatically at runtime?

  28. 28

    How do you programmatically create layers in google maps engine lite using ajax and json

  29. 29

    How do you programmatically create layers in google maps engine lite using ajax and json

HotTag

Archive