【问题标题】:UINavigationItem Title Perform ActionUINavigationItem 标题 执行动作
【发布时间】:2014-09-10 15:41:33
【问题描述】:

当我点击导航栏的标题时,让弹出窗口(特别是日期选择器)出现的好方法是什么?我实际上使用的是 xcode6/swift 但假设 ios7 解决方案就足够了!

非常感谢!

【问题讨论】:

    标签: ios xcode ios7 storyboard swift


    【解决方案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 中已弃用。请改用 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)
    
    }
    

    【讨论】:

    • 非常感谢您的帮助。这也教会了我如何让弹出窗口出现,这很棒。有没有办法将 UIDatePicker 添加到 UIAlertView 中?
    • 在这里使用 alertView.setValue(customContentView,"accessoryView") customContentView 是您的日期选择器
    • 哦另一件事 - 它实际上似乎在 UIAlertView (BAD_INSTRUCTION) 上崩溃了。为了使 uialertview 正常工作,我还需要做些什么吗?非常感谢!
    • 您使用的代码与我在回答中发布的代码相同吗??
    【解决方案2】:

    您需要在里面制作一个带有特定动作的按钮,然后为navigationItem定义titleView:

    self.navigationItem.titleView = yourButton;
    

    现在你有了“可触摸”的导航标题。

    【讨论】:

    • 好的,谢谢 - 所以这里的一切都是动态发生的(弹出窗口、日期选择器,甚至按钮……都用代码实例化)对吧?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-14
    • 1970-01-01
    • 2011-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多