自定义视图不会解雇键盘?

时间:2017-06-30 01:14:57

标签: ios objective-c uitextview uikeyboard uitextviewdelegate

尝试[[self jotNotes] resignFirstResponder];,尝试了[self endEditing:YES];

所以我有我的NoteViewController,它继承了UIViewController,并试图像这样实现委托

@interface NOTEController : UIViewController <UITextViewDelegate>
@end

@implementation NOTEController 
-(id)init {
  self = [super init];
    if (self) {
    //  self.delegate = self; //doesnt let me set this, so i assume i do not do that here
      NOTEControllerView * mainView = [[NOTEControllerView alloc] initWithFrame:[UIScreen mainScreen].bounds];
      self.view = mainView; //just a plain custom uiview subclass its boring and not special
    }
  return self;
}
@end

然后在mainView中,我有一堆子视图,基本上是一个内置UITextView的正方形。

正方形类是这样的,它们是我试图解除键盘的,我设置委托,dismissKB方法和UITextView代码。目前,它会在按下完成按钮时记录我的键盘方法,但键盘仍然存在。如果有人能帮助我理解为什么

,真的很感激
@interface NOTESubview : UIView <UITextFieldDelegate>
@property (nonatomic, weak) id<UITextFieldDelegate> delegate;
-(UITextView *)jotNotes;
@end

@implementation NOTESubview
-(id)initWithFrame:(CGRect)arg1 {
  self = [super initWithFrame:arg1];
    if (self) {
        self.delegate = self;
        [self addSubview:[self jotNotes]];
    }
  return self;
}
-(UITextView *)jotNotes {
  UITextView * jotNotes = [[UITextView alloc] initWithFrame:CGRectMake(0, self.frame.size.height/5.7, self.frame.size.width, self.frame.size.height -  self.frame.size.height/5.7)];
  UIToolbar* keyboardTextViewBar = [[UIToolbar alloc] init];
  keyboardTextViewBar.barStyle = UIBarStyleDefault;
  [keyboardTextViewBar sizeToFit];
   UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
   UIBarButtonItem* doneButton = [[UIBarButtonItem alloc] initWithTitle:@"Done"
                    style:UIBarButtonItemStylePlain target:self
                    action:@selector(dismissKB:)];
  [keyboardTextViewBar setItems:[NSArray arrayWithObjects:flexSpace, doneButton, nil]];
  jotNotes.inputAccessoryView = keyboardTextViewBar;
  jotNotes.delegate = self.delegate;
  return jotNotes;
}
-(void)dismissKB:(UIBarButtonItem *)sender {
  //this will log, so im not sure why it wont resign the board no matter what i try
  NSLog(@"keyboard attempted to dismiss by %@", sender);
  [[self jotNotes] resignFirstResponder];
}

2 个答案:

答案 0 :(得分:0)

我怀疑当调用dismissKB方法时,它实际上并不是当前第一个响应者。

然而,有一个技巧,你可以解雇键盘&#34;来自您应用中的任何位置。你可能想尝试一下:

[[[[UIApplication sharedApplication] delegate] window] endEditing:YES];

答案 1 :(得分:0)

在您尝试关闭键盘的位置添加以下行:

[self endEditing:YES];