如果我在Scrollview中有一个UIView(包含几个textview),如何隐藏键盘

时间:2012-03-05 09:48:22

标签: objective-c uiscrollview keyboard uitextview

我的UIVIew中有几个textview,其中包含一个scrollview。 首先,当我触摸它时,scollview不会激活,并且在完成textview编辑后想要隐藏键盘。所以我构建了一个从scrollview继承的类名subUIView。在那个课程中,我会覆盖触摸touchbegan事件。像代码一样。    但之后 textviewbeingediting 功能不再动作了。即使我想编辑textview,也是关于 touchbegan 事件的所有事情。 你能给我一些解决问题的建议吗?谢谢

//
//  **subUIView.h**
#import <UIKit/UIKit.h>

@interface subUIView :   UIScrollview
{

 }


@end


//  **subUIView.m**
#import "subUIView.h"

@implementation subUIView
- (id)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
self.delegate=self;
//return [super initWithFrame:frame];
if (self) {
    // Initialization code

}
return self;}

NSInteger a=0;


- (void) touchesBegan: (NSSet *) touches withEvent: (UIEvent *) event {   NSLog(@"touch began ");
// If not dragging, send event to next responder
if (!self.dragging)
{
      [self.nextResponder touchesBegan: touches withEvent:event]; 
 }}

在代码中我从subUIView分配scrollview并编写textviewbeginediting函数。

//
//  **ReadExperienceInfoViewController.h**

#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
#define ExperienceTableName  @"pockets"
@class subUIView;
@interface ReadExperienceInfoViewController : UIViewController <UITextFieldDelegate,UITextViewDelegate> {
subUIView *scrollView;

UIView *upView;

UITextField *bookNameTextField;
UITextView *bookExprection;
NSString *getFullDateStr;

BOOL dragging;
}

 @property (nonatomic, retain) UIView *upView;
 @property (nonatomic, retain) subUIView *scrollView;


//
// **ReadExperienceInfoViewController.m**

//- (void)loadView{

scrollView=[[subUIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
scrollView.contentSize=CGSizeMake(320, 960);
scrollView.backgroundColor=[UIColor colorWithPatternImage :[UIImage imageNamed:@"infomation.png"]];
scrollView.showsVerticalScrollIndicator=YES;
upView=[[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];

upView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 960)];
[upView setUserInteractionEnabled:YES];

UILabel *bookName = [[UILabel alloc] initWithFrame:CGRectMake(20, 20, 100, 30)];
bookName.backgroundColor = [UIColor clearColor];
bookName.text = @"Title";
bookName.font=[UIFont boldSystemFontOfSize:16];
[upView addSubview:bookName];
[bookName release];

bookNameTextField = [[UITextField alloc] initWithFrame:CGRectMake(20, 50, 280, 30)];
bookNameTextField.backgroundColor = [UIColor whiteColor];
bookNameTextField.returnKeyType=UIReturnKeyDone;
bookNameTextField.delegate=self;
[upView addSubview:bookNameTextField];

//lowerView=[[UIView alloc] initWithFrame:CGRectMake(0, 90, 320, 170)];
UILabel  *bookExprectionLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 80, 200, 30)];
bookExprectionLabel.text = @"Content";
bookExprectionLabel.font=[UIFont boldSystemFontOfSize:16];
bookExprectionLabel.backgroundColor = [UIColor clearColor];
[upView addSubview:bookExprectionLabel];
[bookExprectionLabel release];

bookExprection = [[UITextView alloc] initWithFrame:CGRectMake(20, 110, 280, 140)];
bookExprection.backgroundColor = [UIColor whiteColor];
bookExprection.font = [UIFont systemFontOfSize:14];    
bookExprection.delegate=self;
[upView addSubview:bookExprection];

self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"Save" 
                                                                           style:UIBarButtonItemStylePlain
                                                                          target:self action:@selector(updateExperience)]
                                          autorelease];

self.title= bookNameStr;       
self.view=scrollView;

}


- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.3];
upView.frame=CGRectMake(0, 0, 320, 960);
[UIView commitAnimations];
[bookExprection resignFirstResponder];
[bookNameTextField resignFirstResponder];
}


-(void)textViewDidBeginEditing:(UITextView *)textView{
NSLog(@"texview dig begin editing");
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.3];
if (textView==bookExprection) {
    upView.frame=CGRectMake(0, -80, 320, 0);          
}    
[UIView commitAnimations];  


}

1 个答案:

答案 0 :(得分:0)

您的subUIView.h文件必须实现UITextFieldDelegate协议,因此您的viewcontroller会响应文本字段的委托方法。

对于Scroll视图,您必须实现ScrollView

//subUIView.h
@interface subUIView : UIViewController <UIScrollViewDelegate, UITextFieldDelegate>{
//do stuff
}

当然,你必须编写scrollView.delegate = self;,以便viewcontroller响应滚动视图的delegate方法。

希望这有帮助