0

I have a UITextField that moves the view up/down when the keyboard moves in/out.

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

I move the view up by "Editing Did Begin" and down by "Did End On Exit" (which I found in some thread here in StackOverflow).

However, the view does not move down, when the button (bottom right) "hide keyboard" is hit. This obviously does respond to "Editing Did End".

The strange thing is, if I also connect "Editing Did End" to moveDown, the method moveDown will be invoked 2x (and moves to far down screen!)
If I then diconnect-connect "Did End On Exit", the view does not disappear by hitting the 'return' button, as it did before.

Any idea what goes wrong here?

//Stefan

4

3 回答 3

1

与其连接你的方法来控制文本字段上的事件,你应该触发你的方法来响应两个通知,UIKeyboardDidShowNotification并且UIKeyboardDidHideNotification. 只需将自己添加为这些通知的观察者,-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];
}
于 2012-01-11T18:34:04.750 回答
0

确保您连接到“Did End On Exit”,而不是“Editing Sid End”。这些名字有点令人困惑。

于 2012-01-11T18:27:12.147 回答
0

我建议您不要使用 UIControl 定义的“Editing Did Begin”和“Editing Did End on Exit”,而是为您的文本字段分配一个委托,并使用 UITextFieldDelegate 协议定义的 textFieldDidEndEditing: 方法。我相信这会在你想要的时候触发。

于 2010-11-07T21:08:51.067 回答