djbp
djbp

Reputation: 715

Programatically instantiating UIBarButtonItem throws missing argument exception

I am trying to do something very simple in Swift - instantiate a UIBarButtonItem:

let btnImg = UIImage(named: "imgName")
let btn = UIBarButtonItem(image: btnImg, style: .Plain, target: self, action: doSomthing)

This code results in a build failure:

Missing argument for parameter 'landcapeImagePhone' in call

According to the docs, both functions are available:

initWithImage:style:target:action:

init(image image: UIImage?,
    style style: UIBarButtonItemStyle,
    target target: AnyObject?,
    action action: Selector)

initWithImage:landscapeImagePhone:style:target:action:

init(image image: UIImage?,
    landscapeImagePhone landscapeImagePhone: UIImage?,
    style style: UIBarButtonItemStyle,
    target target: AnyObject?,
    action action: Selector)

If I implement the second init though, I get the following error:

Extra argument 'landscapeImagePhone' in call

Does anyone know a way in which I can achieve what I am trying to achieve? Is this a bug in Swift / Xcode? I'm running Xcode 6.1.1.

I have done a clean (Cmd + SHIFT + K) and a build folder clean (Cmd + alt + SHIFT + K).

Upvotes: 0

Views: 699

Answers (1)

gabuh
gabuh

Reputation: 1166

If doSomthing is a function without parameters, the correct way to instantiate the UIBarButtonItem should be:

   let btn = UIBarButtonItem(image: btnImg, style: .Plain, target: self, action: "doSomthing")

Check this post for info about Selectors in swift : @selector() in Swift?

Upvotes: 3

Related Questions