-2

I have a UIPickerView where I have a Male and a Female option.

What I have is when I click on label, the UIPickerView is shown, and based on the selection in UIPickerView, the same text is shown on label.

All is working well, but tapping on UIPickerView is not working properly.

When I tap on UIPickerView,

  1. For iOS6, UIPickerView gets dismissed.

  2. For iOS7, UIPickerView DOESN'T get dismissed.

So what I wanted to do is to not dismiss UIPickerView when I click on it.

Any idea how to do that for iOS 6?

Dropbox link

Code

.h

#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

.m

#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

I know because of gesture recognizer, picker is getting hided in iOS6. The problem is why it is not getting hided in iOS7?

Fahim Parkar
  • 30,974
  • 45
  • 160
  • 276
  • You could always tie it with an IBAction and then call `[sender becomeFirstResponder]` – ASKASK Mar 27 '14 at 06:48
  • @ASKASK : I didnt get what you said... more details please... – Fahim Parkar Mar 27 '14 at 06:50
  • you need to just dismissed your uipickerview i hope ? –  Mar 27 '14 at 06:50
  • @mitulmarsonia : no, I don't want to dismiss the pickerview when tap on pickerview... – Fahim Parkar Mar 27 '14 at 06:51
  • First, are you familiar with IBActions? – ASKASK Mar 27 '14 at 06:51
  • @ASKASK : YES... But on UIPickerView there is no IBAAction... RIGHT? – Fahim Parkar Mar 27 '14 at 06:52
  • Hmm, let me investigate a bit and then I'll get right back to you – ASKASK Mar 27 '14 at 06:52
  • Are you using a storyboard/XIB file to design your interface or are you creating the UIPickerView solely programatically? – ASKASK Mar 27 '14 at 06:54
  • @ASKASK : STOP... WHY ARE YOU ASKING STUPID QUESTION? THERE IS NO DIFFERENCE WHETHER I AM USING XIB OR STORYBOARD... OR I AM USING UIPickerView USING IB OR PROGRAMMATICALLY.... PLEASE.... – Fahim Parkar Mar 27 '14 at 06:55
  • you put tab bar in uipickerview and take two button ok and cancel and you give the action... –  Mar 27 '14 at 06:59
  • @mitulmarsonia : I do understand, but still if I tap on UIPickerView, UIPickerView is getting dismissed in iOS6... I don't want to dismiss this in iOS6... – Fahim Parkar Mar 27 '14 at 07:00
  • You could set up a gesture recognizer that recognizes taps in the pickerView's region and if so it calls `becomeFirstResponder` to the pickerView – ASKASK Mar 27 '14 at 07:06
  • @ASKASK : Can you try with code and see first if its working on your end or not? test.. if works, let me know... – Fahim Parkar Mar 27 '14 at 07:31
  • The UIPicker being dismissed when tapping on it has nothing to do with iOS 6 or iOS 7. It has to do with the code you wrote. You're dismissing the Picker yourself, not automatically. Please post all textfield and picker view delegate methods you've implemented, also the IBActions connected to your textfield and picker view. – Lord Zsolt Mar 27 '14 at 07:44
  • @LordZsolt : I will post all code in 10 mins... – Fahim Parkar Mar 27 '14 at 08:07
  • @LordZsolt : check updated question... I have added link for the project... also add code in question – Fahim Parkar Mar 27 '14 at 08:33

1 Answers1

1

Did you write this yourself if you just copy-pasted it from somewhere and you didn't know what it does so you came to Stack Overflow with close to zero research done? By close to zero research, I mean you didn't even read the code...

What do you think these lines do?

UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideAllKeyboards)];
tapGesture.cancelsTouchesInView = NO;
[self.view addGestureRecognizer:tapGesture];

-(IBAction)hideAllKeyboards {
     NSLog(@"hideAllKeyboards");
     myPicker.hidden = YES;
}

It turns out, when a touch happens (on the pickerview) in iOS7, the view that catches the tap is of class UIPickerTableViewWrapperCell, while in iOS6, it's of class UIPickerTableView (if not tapping on a row) or UITableViewCellContentView (when tapping on a row). My guess is, the later two let the tap pass through as if the tap happened on their superview (in your case, self.view). <- The last sentence is just a guess, not for sure.

The way you can make sure the picker only gets hidden in case the tap happened on self.view is to set a delegate self as delegate to tapGesture, then implement the gestureRecognizer:shouldReceiveTouch method:

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    if (touch.view == self.view) {
        return YES;
    } else {
        return NO;
    }
}
Lord Zsolt
  • 6,492
  • 9
  • 46
  • 76
  • this will hide my pickerview... and yes, but outside touch other then mypicker, i want to hide and its hiding... – Fahim Parkar Mar 27 '14 at 09:49
  • I know because of gesture recognizer, picker is getting hided in iOS6. The problem is why it is not getting hided in iOS7? – Fahim Parkar Mar 27 '14 at 10:34
  • It's also getting hidden in iOS7, just in iOS7, the picker has a transparent background, you you're actually tapping on the picker. Try setting a background color to the picker and tap outside of it. – Lord Zsolt Mar 27 '14 at 10:56
  • if I tap outside in iOS7, it gets hided and same happens for iOS 6 too... I had applied gesture on view, so why touch is getting called on uipicker in ios6? anyway i solved my problem in another way by not calling touch event on view... i am showing button when picker is shown and when i tap button, button n picker gets hided... – Fahim Parkar Mar 27 '14 at 11:15
  • I don't understand what you mean... When I run it (both iOS6 and iOS7), if I tap on the view, the picker gets hidden, if I tap on the picker, nothing happens. – Lord Zsolt Mar 27 '14 at 11:33
  • for me (in my sample code), when I tap on picker in iOS6, picker gets hided... I don't know what is going on? – Fahim Parkar Mar 27 '14 at 11:40
  • Thanks for your answer.. using this even if I tap on UIPickerView, it is not getting dismissed... – Fahim Parkar Mar 30 '14 at 12:48