1

What is a good way to get a popup to appear (specifically a date picker) when I tap the title of my navigation bar? I'm actually using xcode6/swift but assume an ios7 solution would suffice!

Thanks so much!

NullHypothesis
  • 4,286
  • 6
  • 37
  • 79

2 Answers2

1

You need to make a button with the specific action on touch up inside and then define titleView for navigationItem:

self.navigationItem.titleView = yourButton;

Now you have "touchable" navigation title.

malex
  • 9,874
  • 3
  • 56
  • 77
  • ok thank you - so the idea is everything here happens dynamically (the popup, the date picker, and even the button.. all are instantiated with code) right? – NullHypothesis Jul 20 '14 at 12:59
1

You can set button as title view

override func viewDidLoad() {
    super.viewDidLoad()

    var button:UIButton  = UIButton.buttonWithType(UIButtonType.System) as UIButton

    (button as UIView).frame = CGRectMake(0, 0, 100, 44)
    button.setTitle("Your title", forState: UIControlState.Normal)
    button.addTarget(self, action: "buttonClicked:", forControlEvents: UIControlEvents.TouchUpInside)
    self.navigationItem.titleView = button;
}

func buttonClicked(sender:UIButton)
{

    UIAlertView(title: "Message title", message: "POP UP", delegate: nil, cancelButtonTitle: "OK").show()

}

Edit UIAlertView deprecated in iOS8.Using UIAlertViewController instead

func buttonClicked(sender:UIButton)
{

  var alert = UIAlertController(title: "Title", message: "Message", preferredStyle: UIAlertControllerStyle.Alert)
 alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil))
 self.presentViewController(alert, animated: true, completion: nil)

}
codester
  • 36,891
  • 10
  • 74
  • 72
  • Perfect thanks for your help. This also taught me how to make popups appear which is great. Is there a way to add a UIDatePicker into the UIAlertView? – NullHypothesis Jul 20 '14 at 13:07
  • You can refer http://stackoverflow.com/questions/12166734/add-uidatepicker-to-uialertview – codester Jul 20 '14 at 13:11
  • Use alertView.setValue(customContentView,"accessoryView") here you `customContentView` is your date picker – codester Jul 20 '14 at 13:13
  • Oh one other thing - it actually seems to crash on UIAlertView (BAD_INSTRUCTION). is there something else I need to do for the uialertview to function? thanks so much! – NullHypothesis Jul 20 '14 at 13:21
  • are you using same code that i have posted in answer?? – codester Jul 20 '14 at 13:23
  • Yeah same code. What do you think? A lot of people are saying it has to do with doing this in main thread does that have something to do with it? – NullHypothesis Jul 20 '14 at 20:06
  • it is working fine.Nothing to do with threading here as it will execute on main thread as i posted.If you aree doing something diffrent than i have to look that Can you please give me where getting error – codester Jul 20 '14 at 20:17
  • someone wrote it is deprecated in swift and you do it this way in ios8 ? I tried and it works now, although it jus t makes a popup (still interested in how I can make a date picker but I can start to work on that next) http://stackoverflow.com/questions/24029768/uialertview-in-swift-getting-exc-bad-access – NullHypothesis Jul 20 '14 at 20:20