2014-07-19 40 views
1

当我点击导航栏的标题时,什么方法可以使弹出窗口显示(特别是日期选择器)?我实际上使用xcode6/swift,但假设一个ios7解决方案就足够了!UINavigationItem标题执行操作

非常感谢!

回答

1

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() 

} 

编辑UIAlertView在iOS8.Using UIAlertViewController代替

弃用您可以设置按钮
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) 

} 
+0

非常感谢您的帮助。这也教会了我如何让弹出窗口显得很棒。有没有办法将UIDatePicker添加到UIAlertView中? – NullHypothesis

+0

你可以参考http://stackoverflow.com/questions/12166734/add-uidatepicker-to-uialertview – codester

+0

使用alertView.setValue(customContentView,“accessoryView”)在这里你'customContentView'是你的日期选择器 – codester

1

你需要做一个按钮的内心触摸的具体行动,然后定义为titleview的navigationItem:

self.navigationItem.titleView = yourButton; 

现在你有“可触摸”导航称号。

+0

OK谢谢你 - 这样的想法是这里的一切都发生动态(弹出,日期选择器,甚至按钮......都是用代码实例化的)对吗? – NullHypothesis