菜鸟教程小白 发表于 2022-12-12 08:54:41

ios - 点击文本字段时如何呈现 View Controller ?


                                            <p><p>我有一个带有两个 <code>UITextField</code> 控件的 View ,一个让用户设置开始日期,另一个让用户设置结束日期。我想以模态方式呈现一个带有日历的 View ,以便在点击文本字段时选择日期,而不是显示键盘并让用户点击任何文本的默认行为。然后,一旦从日历中选择了一个日期并且该日历已被关闭,则在文本字段中以日期格式显示该日期。我一直在寻找一些与此相关的帖子,例如:<a href="https://stackoverflow.com/questions/12828398/show-datepicker-on-textfield-tap" rel="noreferrer noopener nofollow">Show datepicker on textfield tap</a> ,但它们是有点旧的帖子或未解决或答案不符合我的需求...</p>

<p>我需要支持<code>iOS 7</code>及以上。</p>

<p>有人可以帮我解决这个问题吗?</p>

<p>提前致谢</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>你应该看看 <code>UITextFieldDelegate</code> <a href="https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITextFieldDelegate_Protocol/#//apple_ref/occ/intfm/UITextFieldDelegate/textFieldShouldBeginEditing:" rel="noreferrer noopener nofollow">protocol</a> .</p>

<p>具体来说,</p>

<p><code>-(BOOL)textFieldShouldBeginEditing:(UITextField * _Nonnull)textField</code></p>

<p>只需展示您的 ViewController ,做您需要做的任何事情,然后返回 NO。 </p>

<p>如果您不是绝对需要使用 UITextField,我建议您使用带有 UITapGestureRecognizer 的 UILabel。</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 点击文本字段时如何呈现 ViewController ?,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/32654360/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/32654360/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 点击文本字段时如何呈现 View Controller ?