日期选择器无法在swift 2.2中打开文本字段的第一次单击

时间:2016-09-15 09:17:39

标签: ios swift uitextfield uidatepicker

我的 #!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Testing out the curses lib. """ import curses def main(scr): """ Draw a border around the screen, move around using the cursor and leave a mark of the latest pressed character on the keyboard. Perhaps you could make a nice painting using asciart? Quit using 'q'. """ # Clear the screen of any output scr.clear() # Get screen dimensions y1, x1 = scr.getmaxyx() y1 -= 1 x1 -= 1 y0, x0 = 0, 0 # Get center position yc, xc = (y1-y0)//2, (x1-x0)//2 # Draw a border scr.border() # Move cursor to center scr.move(yc, xc) # Refresh to draw out scr.refresh() # Main loop x = xc y = yc ch = 'o' while True: key = scr.getkey() if key == 'q': break elif key == 'KEY_UP': y -= 1 elif key == 'KEY_DOWN': y += 1 elif key == 'KEY_LEFT': x -= 1 elif key == 'KEY_RIGHT': x += 1 else: ch = key # Draw out the char at cursor position scr.addstr(ch) # Move cursor to new position scr.move(y, x) # Redraw all items on the screen scr.refresh() if __name__ == "__main__": print(__doc__) print(main.__doc__) input("Press enter to begin playing...") curses.wrapper(main) 视图在第一次点击textField时无法打开。如果我第二次单击它,则会打开日期选择器视图。在这种情况下我很困惑。为什么它没有在第一次点击时打开?如何在datePicker上点击日期选择器视图?

代码我试过:

UITextField

输出:

enter image description here

2 个答案:

答案 0 :(得分:1)

不要使用textField设置操作,而是尝试使用UITextFieldDelegate的委托方法。

func textFieldDidBeginEditing(textField: UITextField!) {    //delegate method
    if textField == dateField {
        let datePickerView = UIDatePicker()
        datePickerView.datePickerMode = UIDatePickerMode.Date
        textField.inputView = datePickerView
        datePickerView.addTarget(self, action: #selector(SendSMS.showDatePicker), forControlEvents: UIControlEvents.ValueChanged)
    }
}

答案 1 :(得分:1)

通过设置TextField动作事件Editing Did Begin也可以使用。如果您设置操作Editing Did End,则第一次将无法正常工作。