iphone - UITextField 用 NSUndoManager 支持替换文本

标签 iphone ios ipad uitextfield nsundomanager

我只是用一个简单的

self.customerTextField.text = @"text";

但我希望有撤消支持,如果他们摇晃手机就可以撤消更改。

我有点不知道如何完成这个。我找到的所有示例都是针对 UITextView 的。

========================代码的当前迭代=================== ===

-(void)getUniqueItemID {

    [self.inventoryLibrarian generateUniqueItemIDwithCompletionBlock:^(NSString *string) {

        [self undoTextFieldEdit:string];
        //self.customTextField.text = string;

    }];

}

- (void)undoTextFieldEdit: (NSString*)string
{
    [self.undoManager registerUndoWithTarget:self
                                    selector:@selector(undoTextFieldEdit:)
                                      object:self.customTextField.text];
    self.customTextField.text = string;
}

-(BOOL)canBecomeFirstResponder {
    return YES;
}

最佳答案

摇动撤消
将此行放入您的 appDelegate 的 application:didFinishLaunchingWithOptions: 方法中:

    application.applicationSupportsShakeToEdit = YES;

并在相关的viewController.m中

    -(BOOL)canBecomeFirstResponder {
        return YES;
    }

此代码的其余部分放在 viewController.m 中

属性(property)
把它放在类扩展中......

    @interface myViewController()
    @property (weak, nonatomic) IBOutlet UITextField *inputTextField;
    @end

将其链接到 Interface Builder 中的文本字段。

撤消方法
将自身的调用添加到重做堆栈

    - (void)undoTextFieldEdit: (NSString*)string
    {
        [self.undoManager registerUndoWithTarget:self
                                        selector:@selector(undoTextFieldEdit:)
                                          object:self.inputTextField.text];
        self.inputTextField.text = string;
    }

(我们不需要创建一个 NSUndoManager 实例,我们从 UIResponder 父类(super class)继承了一个实例)

撤消操作
摇动撤消不需要,但可能有用...

    - (IBAction)undo:(id)sender {
        [self.undoManager undo];
    }
    - (IBAction)redo:(id)sender {
        [self.undoManager redo];
    }

撤消方法的调用
这里有两个不同的改变 textField 内容的例子……

示例 1
通过按钮操作设置 textField 的内容

    - (IBAction)addLabelText:(UIButton*)sender {
        [self.undoManager registerUndoWithTarget:self
                                        selector:@selector(undoTextFieldEdit:)
                                          object:self.inputTextField.text];
        self.inputTextField.text = @"text";
    }

冒着失去清晰度的风险,我们可以将其缩短为:

    - (IBAction)addLabelText:(UIButton*)sender {
        [self undoTextFieldEdit: @"text"];
    }

因为 undoManager 调用在两种方法中是相同的

示例 2
直接键盘输入编辑

    #pragma mark - textField delegate methods

    - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
    {
        [self.undoManager registerUndoWithTarget:self
                                        selector:@selector(undoTextFieldEdit:)
                                          object:textField.text];
        return YES;
    }

    - (BOOL)textFieldShouldReturn:(UITextField *)textField
    {
        //to dismiss the keyboard
        [textField resignFirstResponder];
        return YES;
    }

关于iphone - UITextField 用 NSUndoManager 支持替换文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14244928/

相关文章:

ios - CAShapeLayer 不可见

ios - Swift 禁用带有功能的选项卡栏项目

iphone - 如何在主视图中定位 subview ?

iOS 私有(private)目录

iphone - UIScrollView 初始化为 {0, 0} 处的框架,但宽度和高度为 {0, 0}

iphone - 在 Xcode 4 的 Interface Builder 中滚动 UIScrollView

iphone - iphone应用中的图像增强

iphone - 为什么我的应用程序描述图像在应用程序商店上如此模糊?

ios - Facebook SDK 4.5 IOS 9

iphone - iOS 上的远程 MS SQL 或 MySQL 数据库