iPad键盘,UITextField,奇怪的行为

时间:2010-11-07 20:31:00

标签: objective-c ipad keyboard uitextfield uitextfielddelegate

我有一个UITextField,当键盘移入/移出时,可以向上/向下移动视图。

- (IBAction)moveUp;
- (IBAction)moveDown;

我通过“编辑开始”向下移动视图,向下移动“在退出时结束”(我在StackOverflow中的某个线程中找到)。

然而,当按下(右下角)“隐藏键盘”按钮时,视图不会向下移动。 这显然会回应“编辑结束”。

奇怪的是,如果我还将“Editing Did End”连接到moveDown,方法moveDown将被调用2x(并移动到远处的屏幕!)
如果我然后diconnect-connect“在退出时结束”,则按下“返回”按钮,视图不会像之前那样消失。

知道这里出了什么问题吗?

// Stefan

3 个答案:

答案 0 :(得分:1)

您应该触发方法以响应两个通知UIKeyboardDidShowNotificationUIKeyboardDidHideNotification,而不是将您的方法连接到控制文本字段上的事件。只需在-viewDidLoad中添加自己作为这些通知的观察者,然后在-viewDidUnload中删除自己。

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
    [notificationCenter addObserver:self selector:@selector(moveUp) name:UIKeyboardDidShowNotification object:nil];
    [notificationCenter addObserver:self selector:@selector(moveDown) name:UIKeyboardDidHideNotification object:nil];
}

- (void)viewDidUnload
{
    NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
    [notificationCenter removeObserver:self name:UIKeyboardDidShowNotification object:nil];
    [notificationCenter removeObserver:self name:UIKeyboardDidHideNotification object:nil];
}

答案 1 :(得分:0)

我建议您使用为文本字段分配委托,并使用由UITextFieldDelegate协议定义的textFieldDidEndEditing:方法,而不是使用由UIControl定义的“编辑已开始”和“在退出时编辑已结束”。 。我相信这会在你想要的时候开火。

答案 2 :(得分:0)

确保连接到“退出时结束”,而不是“编辑Sid End”。这些名字有点令人困惑。