1

Is there a way to check if the current view already has a right bar button, something like:

if (FOO_CODE.rightBarButtonItem != nil) {
    // don't create one
} else {
    // create one
}
Lauren Quantrell
  • 2,647
  • 6
  • 36
  • 49

2 Answers2

3
if (self.navigationItem.rightBarButtonItem != nil)
{
   // I exists
} else {
   // I don't exists
}
Jordan
  • 21,746
  • 10
  • 51
  • 63
0
if (self.navigationItem.rightBarButtonItem)

has the same effect, testing equality to nil is redundant

Stefan Ticu
  • 2,093
  • 1
  • 12
  • 21
  • This is incorrect in the case of the rightBarButtonItem because it's an optional. The compiler will actually flag this as an error if you try it on Swift 4 and above, and will tell you to add `!= nil` – kakubei Sep 25 '19 at 10:56