7

(Xcode6, iOS8, iPhone, Swift)

I would like to add a "Continue" button on the right of the navigation bar.

How can this be accomplished? I've been trying with some of the methods available on UIBarButtonItem, but can't get it working.

My best effort to date has been:

    var b = UIBarButtonItem(title: "Continue", style: UIBarButtonItemStyle, target: self, action: nil)
    self.navigationItem.rightBarButtonItem = b

But I'm getting an error on the first line. It doesn't like the "style" parameter. I've also tried

    var b = UIBarButtonItem(title: "Continue", style: UIBarButtonItemStylePlain, target: self, action: nil)

But no luck. Still stuck on the style parameter. Any ideas? tyvm Keith :D

EDIT: For posterity, the following line also includes an action setting:

    var b = UIBarButtonItem(title: "Continue", style: .Plain, target: self, action:"sayHello")

Reference: How to set the action for a UIBarButtonItem in Swift

Community
  • 1
  • 1
kmiklas
  • 13,085
  • 22
  • 67
  • 103
  • 2
    `UIBarButtonItemStyle.Plain`? Also, try to use `let` wherever possible (ie here) – Jack Jul 08 '14 at 19:49
  • FFFFUUUUU..... Thank you!!! And why use `let`? What difference does it make? Also, enter this as an answer and I'll accept it. – kmiklas Jul 08 '14 at 19:54
  • 1
    @kmiklas You should check out Apple's book on Swift. The let vs. var issue is sorted out on the first couple pages, and the answer to this question can be found in the enumerations chapter. – Mick MacCallum Jul 08 '14 at 20:05
  • 1
    I downloaded into iBooks, but have been so slammed that I haven't had time to read through it. – kmiklas Jul 08 '14 at 20:09

2 Answers2

10
enum UIBarButtonItemStyle : Int {
case Plain
case Bordered
case Done
}

So you should NOT write 'UIBarButtonItemStyle', but use '.Plain' (note the dot preceding the word):

var b = UIBarButtonItem(title: "Continue", style: .Plain, target: self, action: nil)

To add an actual action to the button you use the following (assuming the method sayHello() exists):

var b = UIBarButtonItem(title: "Continue", style: .Plain, target: self, action: Selector("sayHello"))

Edit: Typo in code

Kenneth
  • 137
  • 8
Joride
  • 3,722
  • 18
  • 22
  • Ah... the `dot` is again tripping me up. In my efforts I tried `style: Plain`, which didn't work. – kmiklas Jul 08 '14 at 20:13
  • As a follow-up question, how is the "action" parameter properly set? I've tried `action: sayHello()` and `action: self.sayHello()` and `action:@selector(sayHello:)` with no luck – kmiklas Jul 08 '14 at 20:24
  • I've edited my answer to include setting an action too. – Joride Jul 09 '14 at 06:15
2

enumerations in Swift are quite different than ObjC, in a very good way :]

Here you need to use:

UIBarButtonItemStyle.Plain

You can also do:

var b = UIBarButtonItem(title: "Continue", style: .Plain, target: self, action: nil)

for maximum simplicity!

Side note: Use let instead of var wherever you can, its better for performance, health, etc!

Jack
  • 16,677
  • 8
  • 47
  • 51