2012-03-05 76 views
0

我有我的UIVIew中包含滾動視圖中的幾個textview。 首先scollview不會在我觸摸它時激活,並且想要在完成文本視圖編輯後隱藏鍵盤。所以我建立了一個從scrollview繼承的類名subUIView。在那個類中,我覆蓋觸摸touchbegan事件。像代碼打擊。 但之後textviewininiting的函數不再行動。這完全是關於touchbegan即使我想編輯textview時也會調用事件。 你能提供給我一些建議來解決這個問題?謝謝如何隱藏鍵盤,如果我有滾動視圖中的UIView(包含幾個文本視圖)

// 
// **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]; 
}} 
代碼打擊

我的Alloc從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]; 


} 

回答

0

subUIView.h文件必須實現UITextFieldDelegate協議,讓您的ViewController響應委託方法文本字段。

同爲滾動視圖,你必須實現滾動型

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

那麼當然你必須寫scrollView.delegate = self;從而使視圖 - 控制響應delegatemethods滾動視圖。

希望這有助於

+0

正如我實現UITextViewDelegate協議和UIScrollViewDelegate在我的UIViewController。但是你可能知道subUIview是一個ScrollView類,如果不拖動或滾動它將調用隱藏鍵盤功能。在我這樣做後,它會捕獲所有的事件,所以UIViewcontroller的textviewbeginediting和其他觸摸事件函數將永遠不會被調用。非常感謝您的建議。 – MuddySky 2012-03-05 11:06:19

相關問題