菜鸟教程小白 发表于 2022-12-11 19:05:39

ios - 开始使用 UIDatePicker 时的操作


                                            <p><p>我想在用户开始更改 UIDatePicker 的值时触发一个操作。我的第一个直觉是使用 <code>datePickerEditingDidBegin</code> 方法,但似乎没有用。</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>直接来自文档:</p>
<p> <a href="https://developer.apple.com/reference/uikit/uidatepicker" rel="noreferrer noopener nofollow">UIDatePicker: Responding to User Interaction</a> </p>
<blockquote>
<p>Date pickers use the Target-Action design pattern to notify your app
when the user changes the selected date. To be notified when the date
picker’s value changes, register your action method with the
valueChanged event. At runtime the date picker calls your methods in
response to the user selecting a date or time.</p>
<p>You connect a date
picker to your action method using the addTarget(_:action:for:) method
or by creating a connection in Interface Builder. The signature of an
action method takes one of three forms, which are listed in Listing 1.
Choose the form that provides the information that you need to respond
to the value change in the date picker.</p>
<p>Listing 1Action methods for date pickers</p>
<pre><code>@IBAction func doSomething()
@IBAction func doSomething(sender: UIDatePicker)
@IBAction func doSomething(sender: UIDatePicker, forEvent event: UIEvent)
</code></pre>
</blockquote>
<p>连接您的日期选择器 <code>valueChanged</code> 将事件发送到您的 <code>IBAction</code></p>
<p> <a href="/image/kDjJi.png" rel="noreferrer noopener nofollow"><img src="/image/kDjJi.png" alt="enter image description here"/></a> </p>
<p><strong>编辑</strong>:</p>
<p>根据文档,<code>UIDatePicker</code> 没有任何委托(delegate)方法,因此像 <code>datePickerEditingDidBegin</code> 这样的东西没有任何意义。</p>
<p>你必须使用 <code>UIControlEvent</code>。如果 <code>valueChanged</code> 不适合您的需要,我可能会建议 <code>touchDragInside</code>。</p>
<blockquote>
<p>An event where a finger is dragged inside the bounds of the control.</p>
</blockquote>
<p>其他 <code>UIControlEvent</code> 描述位于 <a href="https://developer.apple.com/reference/uikit/uicontrolevents" rel="noreferrer noopener nofollow">here in the docs</a> .</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 开始使用 UIDatePicker 时的操作,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/42492579/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/42492579/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 开始使用 UIDatePicker 时的操作