我有一个UIPickerView,我有一个男性和女性选项。
当我点击标签时,会显示UIPickerView,并根据UIPickerView中的选择,标签上会显示相同的文字。
一切运行良好,但点击UIPickerView无法正常工作。
当我点按UIPickerView时,
对于iOS6,UIPickerView被解雇。
对于iOS7,UIPickerView不会被解雇。
所以我想做的是点击它时不要忽略UIPickerView。
知道如何为iOS 6做到这一点吗?
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController<UIPickerViewDelegate, UIPickerViewDataSource>
@property (retain, nonatomic) IBOutlet UIPickerView *myPicker;
@property (retain, nonatomic) IBOutlet UILabel *myLabel;
@property (retain, nonatomic) IBOutlet NSMutableArray *arrayGender;
@end
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize myPicker, myLabel, arrayGender;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
arrayGender = [[NSMutableArray alloc] init];
[arrayGender addObject:@"Male"];
[arrayGender addObject:@"Female"];
myLabel.text = @"Choose gender...";
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideAllKeyboards)];
tapGesture.cancelsTouchesInView = NO;
[self.view addGestureRecognizer:tapGesture];
myLabel.userInteractionEnabled = YES;
UITapGestureRecognizer *tapGestureRecognizergenderLabel = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(genderLabelTapped)];
tapGestureRecognizergenderLabel.numberOfTapsRequired = 1;
[myLabel addGestureRecognizer:tapGestureRecognizergenderLabel];
[tapGestureRecognizergenderLabel release];
myPicker.hidden = NO;
}
-(void) genderLabelTapped {
NSLog(@"genderLabelTapped");
[myPicker reloadAllComponents];
myPicker.hidden = NO;
}
-(IBAction)hideAllKeyboards {
NSLog(@"hideAllKeyboards");
myPicker.hidden = YES;
}
- (void)viewWillAppear:(BOOL)animated
{
NSLog(@"viewWillAppear");
[super viewWillAppear:animated];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void) dealloc {
[myPicker release];
[myLabel release];
[super dealloc];
}
- (IBAction)takeMeBack:(id)sender {
[self.navigationController popViewControllerAnimated:YES];
}
#pragma mark -
#pragma mark PickerView DataSource
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
NSLog(@"custom data..");
if (IS_DEVICE_RUNNING_IOS_7_AND_ABOVE()) {
// NSLog(@"changing font...");
UILabel *label;
// = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 233, 44)]; // your frame, so picker gets "colored"
label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 233, 44)];
label.textColor = [UIColor whiteColor];
label.font = [UIFont fontWithName:@"Trebuchet MS" size:14];
label.textAlignment = NSTextAlignmentCenter;
label.text = [arrayGender objectAtIndex:row];
return label;
} else {
// NSLog(@"changing font...");
UILabel *label;
// = [[UILabel alloc] initWithFrame:CGRectMake(40, 0, 193, 44)];
label = [[UILabel alloc] initWithFrame:CGRectMake(40, 0, 193, 44)];
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor blackColor];
label.font = [UIFont fontWithName:@"HelveticaNeue-Bold" size:18];
label.font = [UIFont fontWithName:@"HelveticaNeue-Bold" size:14];
label.text = [arrayGender objectAtIndex:row];
return label;
}
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return [arrayGender count];
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return [arrayGender objectAtIndex:row];
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row
inComponent:(NSInteger)component
{
myLabel.text = [NSString stringWithFormat:@"%@", [arrayGender objectAtIndex:row]];
myPicker.accessibilityValue = [NSString stringWithFormat:@"%@", [arrayGender objectAtIndex:row]];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
- (BOOL)shouldAutorotate
{
return NO;
}
@end
我知道因为手势识别器,选择器被隐藏在iOS6中。问题是为什么它不会被隐藏在iOS7中?
答案 0 :(得分:1)
如果你只是从某个地方复制粘贴它并且你不知道它做了什么,你自己写了这个吗?所以你来到Stack Overflow,接近于零的研究完成了吗?通过接近于零的研究,我的意思是你甚至没有读过代码......
您认为这些线条有什么作用?
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideAllKeyboards)];
tapGesture.cancelsTouchesInView = NO;
[self.view addGestureRecognizer:tapGesture];
-(IBAction)hideAllKeyboards {
NSLog(@"hideAllKeyboards");
myPicker.hidden = YES;
}
事实证明,当iOS7中的触摸发生时(在挑选者视图上),捕获水龙头的视图属于类UIPickerTableViewWrapperCell,而在iOS6中,它是类UIPickerTableView(如果没有点击一行)或UITableViewCellContentView(当点击一行时)。我的猜测是,后两个让水龙头通过,好像在他们的超级视图上发生了敲击(在你的情况下,self.view)。 &lt; - 最后一句话只是猜测,不确定。
你可以确保只有在self.view上点击发生时才会隐藏选择器的方法是将委托自己设置为tapGesture的委托,然后实现gestureRecognizer:shouldReceiveTouch方法:
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
if (touch.view == self.view) {
return YES;
} else {
return NO;
}
}