如何使UITextField首先选择所有,然后以编程方式编辑?

时间:2015-09-27 06:45:16

标签: ios objective-c uitextfield uitextfielddelegate

我有UITextField。在第一次单击时,我想以编程方式select all文本。所以我在[textField selectAll:nil]委托方法中调用textFieldDidBeginEditing:。当我进行下一次点击时,我希望它成为正常的编辑模式。如何以编程方式实现这个?。

提前致谢。

1 个答案:

答案 0 :(得分:2)

当用户点击所选文字时,可以防止出现弹出广告。

要允许第一次点击始终选择所有文字,然后取消选择第二次,请将当前代码保留在- (BOOL) canPerformAction:(SEL)action withSender:(id)sender { /* Prevent action popovers from showing up */ if (action == @selector(paste:) || action == @selector(cut:) || action == @selector(copy:) || action == @selector(select:) || action == @selector(selectAll:) || action == @selector(delete:) || action == @selector(_define:) || action == @selector(_promptForReplace:) || action == @selector(_share:) ) { //Get the current selection range UITextRange *selectedRange = [self selectedTextRange]; //Range with cursor at the end, and selecting nothing UITextRange *newRange = [self textRangeFromPosition:selectedRange.end toPosition:selectedRange.end]; //Set the new range [self setSelectedTextRange:newRange]; return NO; } else { //Handle other actions? } return [super canPerformAction:action withSender:sender]; } 方法中,并展开//Select the text when text field receives focus - (void) textFieldDidBeginEditing:(UITextField *)textField { [textField selectAll:nil]; } //Hide the keyboard when the keyboard "Done" button is pressed - (BOOL)textFieldShouldReturn:(UITextField *)textField { [textField resignFirstResponder]; return TRUE; } 以覆盖if (pass) { this.message = "Expected " + this.actual + " not to be quite so goofy"; } else { this.message = "Expected " + this.actual + " to be goofy, but it was not very goofy"; } ,以防止弹出窗口出现,如:

UITextField子类

this.message = function() {
    return [
        "Expected " + this.actual.hyuk + " to be gawrsh",
        "Expected " + this.actual.hyuk + " not to be gawrsh"
    ];
};

UITextFieldDelegate方法

SET @date = '2013-05-03 10:05:00';
SELECT CAST(@date AS DATETIME) AS Date,
   DATE_FORMAT(@date ,'%Y-01-01 00:00:00') AS Year_Allign,
   CASE EXTRACT(QUARTER FROM @date)
      WHEN 1 THEN DATE_FORMAT(@date ,'%Y-01-01 00:00:00')
      WHEN 2 THEN DATE_FORMAT(@date ,'%Y-04-01 00:00:00')
      WHEN 3 THEN DATE_FORMAT(@date ,'%Y-07-01 00:00:00')
      WHEN 4 THEN DATE_FORMAT(@date ,'%Y-10-01 00:00:00')
      ELSE NULL END AS Quarter_Allign,
   DATE_FORMAT(@date ,'%Y-%m-01 00:00:00') AS Month_Allign,
   DATE_FORMAT(@date ,'%Y-%m-%d 00:00:00') AS Day_Allign;  

我希望有所帮助!