How to add the Continue button to the top navigation bar in iOS / Swift

(Xcode6, iOS8, iPhone, Swift)

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

How can I do that? I tried to use some of the methods available in the UIBarButtonItem, but I can't get it to work.

My best so far:

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

But I get an error on the first line. He doesn't like the "style" parameter. I also tried

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

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

EDIT: for posterity, the following line also includes the action parameter:

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

Link: How to set an action for a UIBarButtonItem in Swift

+7
swift uibarbuttonitem
source share
2 answers
 enum UIBarButtonItemStyle : Int { case Plain case Bordered case Done } 

Therefore, 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 a valid action to the button, you use the following (assuming the sayHello () method exists):

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

Edit: Typo in code

+10
source share

Enumerations in Swift are very different from ObjC:]

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!

Note: use let instead of var , wherever you are, it's better for performance, uptime, etc.

+2
source share

All Articles