2013-05-14 104 views
0

我的UIScrollView沒有響應觸摸事件,也沒有爲我的UIViewController調用touchesBegain方法。 (同樣在我的按鈕上的文本被扭曲的一個,而在另一未示出)UIScrollView對觸摸沒有響應

在故事板我添加了滾動到的UIViewController並加入我的UITextFields和UIButtons到滾動型

下面是代碼:

#import <UIKit/UIKit.h> 

@interface RefineSearchViewController : UIViewController <UITextFieldDelegate, UIScrollViewDelegate> 
{ 
    IBOutlet UIScrollView *scroller; 
} 

@property (strong, nonatomic) IBOutlet UITextField *nameField; 
@property (strong, nonatomic) IBOutlet UITextField *targetField; 
@property (strong, nonatomic) IBOutlet UITextField *vendorField; 
@property (strong, nonatomic) IBOutlet UITextField *CATField; 
@property (strong, nonatomic) IBOutlet UITextField *clonalityField; 
@property (strong, nonatomic) IBOutlet UITextField *sourceOrganismField; 
-(IBAction) textFieldReturn: (id) sender; 

#import "RefineSearchViewController.h" 
#import "DBHandler.h" 

@interface RefineSearchViewController() 

@end 

@implementation RefineSearchViewController 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
     // Custom initialization 
    } 
    return self; 
} 

- (void)viewDidLoad 
{ 
    [scroller setScrollEnabled:YES]; 

    // set the content size to be the size our our whole frame 
    //scroller.frame = CGRectMake(74, 261, 620, 354); 
    [scroller setContentSize:CGSizeMake(2000, 2000)]; 
    [scroller setCanCancelContentTouches:NO]; 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view. 
    self.targetField.delegate = self; 
    self.nameField.delegate = self; 
    self.vendorField.delegate = self; 
    self.clonalityField.delegate = self; 
    self.sourceOrganismField.delegate = self; 
    self.CATField.delegate = self; 
} 

-(void) viewWillAppear:(BOOL)animated 
{ 
    self.navigationController.navigationBarHidden = YES; 
} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{ 
    // If you are going to conduct a refine search 
    if ([[segue identifier] isEqualToString:@"Refine"]) 
    { 
     DBHandler *handler = [[DBHandler alloc]init]; 

     //Run searches on each of the parameter that aren't empty 

     NSString *nameParameter = _nameField.text; 
     if (![nameParameter isEqualToString:@""]) 
     { 
     [handler search:0 andInput:nameParameter]; 
     } 

     NSString *targetParameter = _targetField.text; 
     if(![targetParameter isEqualToString:@""]) 
     { 
      [handler search:1 andInput:targetParameter]; 
     } 

     NSString *vendorParameter = _vendorField.text; 
     if (![vendorParameter isEqualToString:@""]) 
     { 
      [handler search:2 andInput:vendorParameter]; 
     } 

     NSString *catParameter = _CATField.text; 
     if (![catParameter isEqualToString:@""]) 
     { 
      [handler search:3 andInput:catParameter]; 
     } 

     NSString *clonalityField = _clonalityField.text; 
     if (![catParameter isEqualToString:@""]) 
     { 
      [handler search:4 andInput:clonalityField]; 
     } 

     NSString *sourceField = _sourceOrganismField.text; 
     if (![sourceField isEqualToString:@""]) 
     { 
      [handler search:5 andInput:sourceField]; 
     } 

     //recursive implementation 
     for (int i = 0; i < 6 ; i++) 
     { 

     } 

     //We shouldn't clear the text fields here in my personal opinion because they apply to the search until you return to the homescreen and reset what is the 
     //current "working database" 
    } 

    //if you are going to cancel the refine search, simply go back to the previous screen 
    else if ([[segue identifier] isEqualToString:@"Cancel"]) 
    { 
     //Do Nothing 
     //but more importantly.... 
     //....clear Text Fields 
     _nameField.text = @""; 
     _targetField.text = @""; 
     _vendorField.text = @""; 
     _CATField.text = @""; 
     _clonalityField.text = @""; 
     _sourceOrganismField.text = @""; 
    } 
} 

//make that stubborn keyboard go away whenever you touch the background 
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    [_nameField resignFirstResponder]; 
    [_targetField resignFirstResponder]; 
    [_vendorField resignFirstResponder]; 
    [_CATField resignFirstResponder]; 
    [_clonalityField resignFirstResponder]; 
    [_sourceOrganismField resignFirstResponder]; 
} 

-(IBAction)textFieldReturn:(id)sender 
{ 
    [sender resignFirstResponder]; 
} 

//following code was taken and tweaked from stack overflow 
//- (void)textFieldDidBeginEditing:(UITextField *)textField 
//{ 
// [self animateTextField: textField up: YES]; 
// NSLog(@"YO"); 
//} 
// 
// 
//- (void)textFieldDidEndEditing:(UITextField *)textField 
//{ 
// [self animateTextField: textField up: NO]; 
//} 
// 
//- (void) animateTextField: (UITextField*) textField up: (BOOL) up 
//{ 
// const int movementDistance = 216; //height of the keyboard 
// const float movementDuration = 0.3f; // duration of the animation 
//  
// int movement = (up ? -movementDistance : movementDistance); 
//  
// [UIView beginAnimations: @"anim" context: nil]; 
// [UIView setAnimationBeginsFromCurrentState: YES]; 
// [UIView setAnimationDuration: movementDuration]; 
// self.view.frame = CGRectOffset(self.view.frame, 0, movement); 
// [UIView commitAnimations]; 
//} 

@end 
+0

我希望你能找到下面提到的URL有用 http://stackoverflow.com/questions/1685956/uiscrollview-touchesbegan http://stackoverflow.com/questions/2472145/how-does- UIScrollView中偷點觸摸,從-其-子視圖 – 2013-05-14 04:16:25

回答

1

確保您的scrollView的委託在storyboard中設置正確,並且scrollView的userInteractionEnabled已設置。