UITextField停止震动事件?

时间:2010-05-25 02:30:19

标签: iphone keyboard uitextfield ios

对于那些以图形方式工作的人,这是一个视图层次结构图:

Root View -> UINavigationController -> UITableView -> Edit View -> Problem UITextfield

基本上,当设备震动时,我的根视图中会调用- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event;

当用户点击“编辑”图标(屏幕底部的笔 - 而不是传统的UINavigationBar编辑按钮)时,主视图会为自身添加一个子视图,并使用自定义将其动画到屏幕上动画。

此子视图包含一个UINavigationController,它包含一个UITableView。点击单元格时,UITableView会将子视图加载到自身中。第二个子视图是罪魁祸首。出于某种原因,第二个子视图中的UITextField会导致问题。

当用户点击视图时,除非UITextField处于活动状态(在编辑模式下?),否则主视图不会响应抖动。

其他信息:

我的动作事件处理程序:

- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event {
 NSLog(@"%@", [event description]);
 SystemSoundID SoundID;
 NSString *soundFile = [[NSBundle mainBundle] pathForResource:@"shake" ofType:@"aif"];
 AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:soundFile], &SoundID);
 AudioServicesPlayAlertSound(SoundID); 
 [self genRandom:TRUE];
}

genRandom:方法:

/* Generate random label and apply it */
-(void)genRandom:(BOOL)deviceWasShaken{
 if(deviceWasShaken == TRUE){
  decisionText.text = [NSString stringWithFormat: (@"%@", [shakeReplies objectAtIndex:(arc4random() % [shakeReplies count])])];
 }else{
  SystemSoundID SoundID;
  NSString *soundFile = [[NSBundle mainBundle] pathForResource:@"string" ofType:@"aif"];
  AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:soundFile], &SoundID);
  AudioServicesPlayAlertSound(SoundID);
  decisionText.text = [NSString stringWithFormat: (@"%@", [pokeReplies objectAtIndex:(arc4random() % [pokeReplies count])])];
 }
}

shakeRepliespokeReplies都是字符串的NSArray。一个用于当屏幕的某一部分被戳戳时,一个用于当设备被摇动时。该应用程序将从NSArray中随机选择一个字符串并显示在屏幕上。

与往常一样,我们非常感谢代码示例。 我添加了自己的内容以帮助解释问题。

2 个答案:

答案 0 :(得分:1)

键盘:查找UIKeyboardAppearanceAlert

关于摇动检测。一旦UITextField辞职第一响应者(不在焦点,键盘隐藏),您需要确保链中的另一个UIResponder成为firstResponder。例如,如果您有一个向文本字段发送-resignFirstResponder的按钮,则需要执行以下操作:

- (IBAction)pressButton {
    [textField resignFirstResponder];
    [self becomeFirstResponder];
}

答案 1 :(得分:1)

尝试使用[self.view endEditing:YES];

以及使用玻璃键盘:

[textfield setKeyboardAppearance:UIKeyboardAppearanceAlert];

希望这会有所帮助。 谢谢, Madhup

相关问题