2014-07-19 38 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