当UITextField和UITextview显示在UIWindow内部时,解散键盘

时间:2013-03-04 05:55:33

标签: iphone ios objective-c uitextfield uiwindow

我已将UITextFieldUITextView添加到UIWindow内我无法在其中输入内容。两者都不允许我输入这些字段。并且,在键盘中按 return 时无法将其解除。

-(void)showAlert 
{
alertWindow = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
alertView = [[UIView alloc] initWithFrame:CGRectMake(10, 115, 300, 230)];
alertWindow.windowLevel = UIWindowLevelAlert; // puts it above the status bar
alertView.center = CGPointMake(alertWindow.frame.size.width/2, alertWindow.frame.size.height/2);
alertView.backgroundColor = [UIColor whiteColor];
alertView.layer.borderWidth = 1.0;
alertView.layer.borderColor = [[UIColor whiteColor]CGColor];

UITextField *txt = ....
....
....
txt.delegate = self;
...
...
[alertView addSubview:txt];

UITextView *txtview = ...
....
....
txtview.delegate = self;
...
[alertView addSubview:txtview];

[alertWindow addSubview:alertView];
[alertWindow addSubviewWithZoomInAnimation:alertView duration:1.0 option:curveValues[0]];
[alertWindow setHidden:NO];
}

我也声明了两种委托方法。我在那里放了一个断点并检查,即使那个方法还没有调用。

更新 -

textFieldShouldReturn委托方法

- (BOOL) textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    [reserveView resignFirstResponder];
    [reserveWindow resignFirstResponder];
    [self.reserveWindow endEditing:YES];
    [reserveView endEditing:YES];
    return YES;
}

5 个答案:

答案 0 :(得分:1)

您应该尝试将其从UIWindow

中解除键盘
[self.window endEditing:YES];

Source

答案 1 :(得分:1)

我发现,由于UIWindow而没有调用该方法所以,我将UIWindow更改为UIView而且,我使用UIView+Animation.h来解决此问题{3}}它提供了我所需要的动画。

- (void) addSubviewWithZoomInAnimation:(UIView*)view duration:(float)secs option:(UIViewAnimationOptions)option;
- (void) removeWithZoomOutAnimation:(float)secs option:(UIViewAnimationOptions)option;

感谢Google!干杯!

答案 2 :(得分:0)

键盘永远不会自行解散(即使你点击了返回!)。您会注意到,在您的委托方法中,只要按下return / done / etc。

,就会收到事件

每次有人点击返回键时,键盘关闭的一种简单方法是实现- (BOOL)textViewShouldEndEditing:(UITextView *)textView方法。

- (BOOL)textViewShouldEndEditing:(UITextView *)textView
{
    [textView endEditing:YES];
    return YES;
}

编辑:在你编辑的帖子中,你说你已经宣布了两种委托方法......有两种以上所以你能澄清一下你实施的方法吗? http://developer.apple.com/library/ios/#documentation/uikit/reference/UITextViewDelegate_Protocol/Reference/UITextViewDelegate.html#//apple_ref/occ/intf/UITextViewDelegate

答案 3 :(得分:0)

试试这个

- (void)viewDidLoad
{
  [super viewDidLoad];
  UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissKeyboard)];
  tapGesture.cancelsTouchesInView = NO;
  [self.window addGestureRecognizer:tapGesture];
  //[self.view addGestureRecognizer:tapGesture];
  [tapGesture release];
}
-(void)dismissKeyboard
{
[txt resignFirstResponder];
[textView resignFirstResponder];
}

答案 4 :(得分:0)

试试这个

这是textfield的委托方法: -

  • (BOOL)textFieldShouldReturn:(UITextField *)textField

    {

    [txtview resignFirstResponder];
    

    返回NO;

    }