我的 UIScrollView 没有响应触摸事件,也没有为我的 UIViewController 调用 touchesBegain 方法。(此外,我的按钮上的文字在一个上失真,而在另一个上没有显示)
在 Storyboard 中,我将 ScrollView 添加到 UIViewController 并将我的 UITextFields 和 UIButtons 添加到 ScrollView
这是代码:
#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