为什么我的UIKeyboard不会在resignfirstresponder上消失

时间:2013-05-04 19:42:28

标签: objective-c xcode

这太荒谬了,我看不出我的代码有什么问题。我希望有人能帮助我 对的。问题是当resignfirstresponder触发时,似乎出现了另一个UIKeyboard,请注意完成按钮更改为键盘上的普通返回按钮。

我的testViewController.m文件

#import <QuartzCore/QuartzCore.h>
#import <UIKit/UIKit.h>
#import "testViewController.h"
#define kOFFSET_FOR_KEYBOARD 216.0
@interface testViewController ()
{
    float increase;
    int increases;
    UITextView *txt;
    UIView *vw;
    UIView *tvw;
    UITableView *tbl;
    UIButton *b;
}
@end

@implementation testViewController

- (void)viewDidLoad {
    vw =[[UITextView alloc]initWithFrame:CGRectMake(0, self.view.frame.size.height-34, 320, 1000)];
    vw.backgroundColor =[UIColor blackColor];
    tbl =[[UITableView alloc]initWithFrame:CGRectMake(0, 0, 320, self.view.frame.size.height-34)];
    txt =[UITextView new];
    txt.frame=CGRectMake(4, 4, 255, 25);
    [txt setScrollEnabled:NO];
    txt.contentInset = UIEdgeInsetsMake(4, 4, 0, 0);
    [tbl setDelegate:self];
    [tbl setDataSource:self];
    [txt setDelegate:self];
    [txt setReturnKeyType:UIReturnKeyDone];
    [self.view addSubview:tbl];
    [[txt layer] setCornerRadius:2.0f];
    [vw addSubview:txt];
    [self.view addSubview:vw];
    increases = 0;
    increase =23;
    txt.text =@"Give me text...";
    b = [UIButton buttonWithType:UIButtonTypeCustom];
    b.frame = CGRectMake(263, 4, 53, 25);
    b.tag =1;
    [[b layer] setCornerRadius:2.0f];
    [[b layer] setMasksToBounds:YES];
    [b addTarget:self action:@selector(close:) forControlEvents:UIControlEventTouchUpInside];
    [vw addSubview:b];
    b.backgroundColor =[UIColor grayColor];
    [super viewDidLoad];

}
- (void)textViewDidEndEditing:(UITextView *)textView {
    [self setViewMovedUp:NO];
}
-(void)textViewDidChange:(UITextView *)textView {
    if(increase!=txt.contentSize.height) {
        CGRect rect = txt.frame;
        rect.size.height = txt.contentSize.height;
        textView.frame   = rect;
        CGRect bRect = b.frame;
        if(txt.frame.size.height<25){
            CGRect temp = txt.frame;
            temp.size.height =25;
            txt.frame =temp;
        }
        bRect.origin.y = txt.frame.size.height +4-25;
        b.frame = bRect;
        CGRect arect = self.view.frame;

        if(increase < txt.contentSize.height) {
            arect.origin.y -= 15;
            arect.size.height += 15;
            increases =increases+1;
        } else {
            arect.origin.y += 15;
            arect.size.height -= 15;
            increases =increases-1;
        }
        NSLog(@"%f",bRect.size.height);
        self.view.frame = arect;
        increase = txt.contentSize.height;
    }
}
-(void)setViewMovedUp:(BOOL)movedUp {
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.25];
    CGRect rect = self.view.frame;
    if (movedUp) {
        rect.origin.y -= kOFFSET_FOR_KEYBOARD;
        rect.size.height += kOFFSET_FOR_KEYBOARD;
    } else {
        rect.origin.y += kOFFSET_FOR_KEYBOARD+(increases*15);
        rect.size.height -= kOFFSET_FOR_KEYBOARD+(increases*15);
        vw.frame = CGRectMake(0, self.view.frame.size.height-34, 320, 1000);
        txt.frame = CGRectMake(4, 4, 255, 25);
        increase = 23;
        increases = 0;
        txt.text=@"";
    }
    self.view.frame = rect;
    [UIView commitAnimations];
}
- (void)textViewDidBeginEditing:(UITextView *)textView {
    if  (self.view.frame.origin.y >= 0) {
        txt.text =@"";
        [self setViewMovedUp:YES];
    }

}
-(void)close:sender {
    if(txt.isFirstResponder)
        [txt resignFirstResponder];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 100;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 60.0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"cell";
    UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    cell.textLabel.text = [NSString stringWithFormat:@"%i",indexPath.section];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if(txt.isFirstResponder)
        [txt resignFirstResponder];
}
@end   

我的testViewController.h文件

#import <UIKit/UIKit.h>
@interface testViewController :     UIViewController<UITableViewDataSource,UITableViewDelegate,UITextViewDelegate>


@end

提前致谢

1 个答案:

答案 0 :(得分:1)

你似乎有一个UITextView,你的意思是在初始化vw时有一个UIView,这导致你在UITextView中有一个UITextView。