点击按钮时显示UIPickerView

时间:2018-12-14 06:01:26

标签: ios swift uibutton uipickerview

enter image description here

单击此按钮,我想打开一个UIPickerView。 我已经创建了此按钮,现在当用户单击它时,我想打开一个UIPickerView。我在互联网上看过许多教程,但所有教程都显示为:

WRAPPER_ASSIGNMENTS = ('__module__', '__name__', '__doc__')

WRAPPER_UPDATES = ('__dict__',)

def update_wrapper(wrapper,
                   wrapped,
                   assigned = WRAPPER_ASSIGNMENTS,
                   updated = WRAPPER_UPDATES):

    """Update a wrapper function to look like the wrapped function

       wrapper is the function to be updated
       wrapped is the original function
       assigned is a tuple naming the attributes assigned directly
       from the wrapped function to the wrapper function (defaults to
       functools.WRAPPER_ASSIGNMENTS)
       updated is a tuple naming the attributes of the wrapper that
       are updated with the corresponding attribute from the wrapped
       function (defaults to functools.WRAPPER_UPDATES)
    """
    for attr in assigned:
        setattr(wrapper, attr, getattr(wrapped, attr))
    for attr in updated:
        getattr(wrapper, attr).update(getattr(wrapped, attr, {}))
    # Return the wrapper so this can be used as a decorator via partial()
    return wrapper

def wraps(wrapped,
          assigned = WRAPPER_ASSIGNMENTS,
          updated = WRAPPER_UPDATES):
    """Decorator factory to apply update_wrapper() to a wrapper function

   Returns a decorator that invokes update_wrapper() with the decorated
   function as the wrapper argument and the arguments to wraps() as the
   remaining arguments. Default arguments are as for update_wrapper().
   This is a convenience function to simplify applying partial() to
   update_wrapper().
    """
    return partial(update_wrapper, wrapped=wrapped,
                   assigned=assigned, updated=updated)

但是要在没有文本字段的情况下实现它。 有可能吗,请告诉我该怎么做。

2 个答案:

答案 0 :(得分:4)

如果要从Interface Builder进行操作,则可以在StoryBoard中添加UIPickerView,并首先隐藏该UIPickerView,并在单击按钮时显示它。

如果您想通过编码来做到这一点,则可以尝试一下。

将此定义为全局。

var toolBar = UIToolbar()
var picker  = UIPickerView()

在按钮点击动作中添加以下代码。我在工具栏中添加了Done按钮以关闭选择器。

注意::如果您不想这样做,我会在按钮操作中编写整个代码,只需在viewDidLoad中编写所有代码,然后在按钮操作{{1 }}和self.view.addSubview(picker)

self.view.addSubview(toolBar)

在“完成”按钮上,您可以删除工具栏以及PickerView。

@IBAction func YOUR_BUTTON__TAP_ACTION(_ sender: UIButton) {
    picker = UIPickerView.init()
    picker.delegate = self
    picker.backgroundColor = UIColor.white
    picker.setValue(UIColor.black, forKey: "textColor")
    picker.autoresizingMask = .flexibleWidth
    picker.contentMode = .center
    picker.frame = CGRect.init(x: 0.0, y: UIScreen.main.bounds.size.height - 300, width: UIScreen.main.bounds.size.width, height: 300)
    self.view.addSubview(picker)

    toolBar = UIToolbar.init(frame: CGRect.init(x: 0.0, y: UIScreen.main.bounds.size.height - 300, width: UIScreen.main.bounds.size.width, height: 50))
    toolBar.barStyle = .blackTranslucent
    toolBar.items = [UIBarButtonItem.init(title: "Done", style: .done, target: self, action: #selector(onDoneButtonTapped))]
    self.view.addSubview(toolBar)
}

委托@objc func onDoneButtonTapped() { toolBar.removeFromSuperview() picker.removeFromSuperview() }

UIPickerView

答案 1 :(得分:1)

只需隐藏/显示您的uipickerview。将选择器添加为情节提要中的主视图子视图,但将其隐藏。单击该按钮后,将其显示。

添加带有完成按钮的工具栏,以再次隐藏选择器。 Here's的链接