Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a "Continue" button to a top Navigation Bar in iOS/Swift

(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

like image 622
kmiklas Avatar asked Jul 08 '14 19:07

kmiklas


1 Answers

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

like image 123
Joride Avatar answered Nov 07 '22 06:11

Joride