Iphone UITextField使用NSUndoManager支持替换文本

Iphone UITextField使用NSUndoManager支持替换文本,iphone,ios,ipad,uitextfield,nsundomanager,Iphone,Ios,Ipad,Uitextfield,Nsundomanager,我只是用一个简单的 self.customerTextField.text = @"text"; 但我希望有撤销支持,如果他们摇晃手机,他们可以撤销更改 我有点不知所措,不知如何才能做到这一点。我找到的所有示例都是针对UITextView =====================================当前代码迭代====================== -(void)getUniqueItemID { [self.inventoryLibrarian generate

我只是用一个简单的

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的
应用程序:didFinishLaunchingWithOptions:
方法:

    application.applicationSupportsShakeToEdit = YES;
在相关的viewController.m中

    -(BOOL)canBecomeFirstResponder {
        return YES;
    }
这段代码的其余部分放在viewController.m中

    -(BOOL)canBecomeFirstResponder {
        return YES;
    }
属性
把这个放在类扩展中

    @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超类继承了一个实例)

撤消操作
取消抖动不需要,但可能很有用

    - (IBAction)undo:(id)sender {
        [self.undoManager undo];
    }
    - (IBAction)redo:(id)sender {
        [self.undoManager redo];
    }
撤销方法的调用
下面是更改文本字段内容的两个不同示例

示例1
通过按钮操作设置文本字段的内容

    - (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;
    }

我刚刚意识到你可能是说用户改变文本的行为不一定是用键盘编辑的,而是程序化的结果,在这种情况下,我的答案有点没用。我认为操作是在程序化细节之后,但是,很高兴有人提醒你,它也适用于用户操作。选中此项可检测抖动手势,然后自己实现撤消功能。我尝试了此功能,但无效。可能是因为viewController是UITableViewCell?我添加了迄今为止使用的代码。我还向appDelegate添加了shaketoedit。当我添加“canBecomeFirstResponder”时,它不再支持单元格的“通过抖动撤消”。@Hackmodford:“可能是因为viewController是UITableViewCell?”…听起来有点。。。不可能的。。。您的意思是,textField位于表视图单元格中吗?还是说您有一个viewController来控制每个tableViewCell的contentView?或者…?@Hackmodford,我已经用静态表中
UITableViewCell
中的文本字段测试了这段代码。它只是工作。所有不在应用程序委托中的代码都在
TableViewController
中。文本字段必须有两个到
TableViewController
-(1)
委托
是TableVC的连接;(2) IBOutlet是
self.inputTextField
(或者在您的情况下是
customTextField
canBecomeFirstResponder
位于TableViewController中,工作正常。我有一个带有nib的自定义uitableviewcell设置。我更改文本字段的代码位于uitableviewcell的类文件中。我尝试将customcell.m设置为textfield的代理。但这没什么区别。canbecomefirstresponder从未被调用,据我所知我在tableviewcell控制器中工作。我不得不打电话给[自我成为第一反应者];在更改文本字段中的文本之前。现在它起作用了。谢谢