Sportica   Sportica

UITextView 关闭键盘

//开始触发键盘
-(BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
    return YES;
}

//在触发键盘之前可以触发一些必要的事件。
-(void)textViewDidBeginEditing:(UITextView *)textView
{
    NSLog(@"Begin");
/*
    UIBarButtonItem *done =    [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(leaveEditMode)] autorelease];  
    self.navigationItem.rightBarButtonItem = done; 
*/    
}


//下面这段搞定键盘关闭。点return 果断关闭键盘
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text  
{  
    if ([text isEqualToString:@"\n"]) {  
        [textView resignFirstResponder];  
        return NO;  
    }  
    return YES;  
}  


//下面这段来自网络
UIToolbar * topView = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 30)];  
    [topView setBarStyle:UIBarStyleBlack];  
      
    UIBarButtonItem * helloButton = [[UIBarButtonItem alloc]initWithTitle:@"Hello" style:UIBarButtonItemStyleBordered target:self action:nil];  
      
    UIBarButtonItem * btnSpace = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];  
      
    UIBarButtonItem * doneButton = [[UIBarButtonItem alloc]initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(dismissKeyBoard)];  
      
      
    NSArray * buttonsArray = [NSArray arrayWithObjects:helloButton,btnSpace,doneButton,nil];  
    [doneButton release];  
    [btnSpace release];  
    [helloButton release];  
      
    [topView setItems:buttonsArray];  
    [tvTextView setInputAccessoryView:topView];  
  
-(IBAction)dismissKeyBoard  
{  
    [tvTextView resignFirstResponder];  
}  
posted @ 2012-08-30 14:00  qingjoin  阅读(2854)  评论(0编辑  收藏  举报
  Sportica