【问题标题】:How to hide the keyboard if I have a UIView(contains several textview) in Scrollview如果我在 Scrollview 中有 UIView(包含多个 textview),如何隐藏键盘
【发布时间】:2012-03-05 09:48:21
【问题描述】:

我的 UIVIew 中有几个文本视图,它们包含在滚动视图中。 首先,当我触摸 scollview 并希望在完成 textview 编辑后隐藏键盘时,scollview 没有激活。所以我建立了一个继承自scrollview的类名subUIView。在该课程中,我覆盖了 touch touchbegan 事件。就像代码打击一样。 但在那之后 textviewbeingediting 功能不再起作用。这都是关于 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 分配滚动视图并编写 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];  


}

【问题讨论】:

    标签: objective-c uiscrollview keyboard uitextview


    【解决方案1】:

    您的subUIView.h 文件必须实现UITextFieldDelegate 协议,因此您的视图控制器响应文本字段的委托方法。

    滚动视图也一样,你必须实现滚动视图

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

    那么当然你必须写scrollView.delegate = self;,以便视图控制器响应滚动视图的委托方法。

    希望对你有帮助

    【讨论】:

    • 当我在我的 UIViewcontroller 中实现 UITextViewDelegate 协议和 UIScrollViewDelegate 时。但是您可能知道 subUIview 是一个 Scrollview 类,如果不拖动或滚动它将调用隐藏键盘功能。在我这样做之后,它将捕获所有事件,因此 UIViewcontroller 的 textviewbeginediting 和其他触摸事件函数将永远不会被调用。非常感谢您的建议。
    猜你喜欢
    • 2016-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-03
    相关资源
    最近更新 更多