Search code examples
objective-cxcodeuipickerviewuiactionsheetuipicker

UIPickerView with UIActionSheet IOS 8 stop working


Showing a UIPickerView with UIActionSheet in iOS8 not showing

enter image description here

enter image description here

-(void)showPicker{

    /************************  FIXED please contact me on [email protected] if you need help to fix******//





   actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:nil cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
   [actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];



    CGRect pickerFrame = CGRectMake(0, 40, 0, 0);

    UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:pickerFrame];
    pickerView.showsSelectionIndicator = YES;

    switch((uint)currentDelegate){
        case 0:{
            pickerView.dataSource = propertyDelegate;
            pickerView.delegate = propertyDelegate;
            [pickerView selectRow:[propertyDelegate index] inComponent:0 animated:NO];
            break;
        }
        case 1:{
            pickerView.dataSource = regionDelegate;
            pickerView.delegate = regionDelegate;
            [pickerView selectRow:[regionDelegate index] inComponent:0 animated:NO];
            break;
        }
        case 2:{
            pickerView.dataSource = townDelegate;
            pickerView.delegate = townDelegate;
            [pickerView selectRow:[townDelegate index] inComponent:0 animated:NO];
            break;
        }
        case 3:{
            pickerView.dataSource = bedDelegate;
            pickerView.delegate = bedDelegate;
            [pickerView selectRow:[bedDelegate index] inComponent:0 animated:NO];
            break;
        }
        case 4:{
            pickerView.dataSource = bathDelegate;
            pickerView.delegate = bathDelegate;
            [pickerView selectRow:[bathDelegate index] inComponent:0 animated:NO];
            break;
        }
        case 5:{
            pickerView.dataSource = priceDelegate;
            pickerView.delegate = priceDelegate;
            [pickerView selectRow:[priceDelegate index1] inComponent:0 animated:NO];
            [pickerView selectRow:[priceDelegate index2] inComponent:1 animated:NO];
            break;
        }
        case 6:{
            pickerView.dataSource = currencyDelegate;
            pickerView.delegate = currencyDelegate;
            [pickerView selectRow:[currencyDelegate index] inComponent:0 animated:NO];
            break;
        }

    }







    [actionSheet addSubview:pickerView];

    UISegmentedControl *previousButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:NSLocalizedString(@"Previous", nil)]];
    previousButton.momentary = YES; 
    previousButton.frame = CGRectMake(5.0f, 7.0f, 70.0f, 30.0f);
    previousButton.segmentedControlStyle = UISegmentedControlStyleBar;
    previousButton.tintColor = [UIColor blackColor];

    UISegmentedControl *nextButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:NSLocalizedString(@"Next", nil)]];
    nextButton.momentary = YES; 
    nextButton.frame = CGRectMake(80.0f, 7.0f, 70.0f, 30.0f);
    nextButton.segmentedControlStyle = UISegmentedControlStyleBar;
    nextButton.tintColor = [UIColor blackColor];


    UISegmentedControl *closeButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:NSLocalizedString(@"Done", nil)]];
    closeButton.momentary = YES; 
    closeButton.frame = CGRectMake(240, 7.0f, 70.0f, 30.0f);
    closeButton.segmentedControlStyle = UISegmentedControlStyleBar;
    closeButton.tintColor = [UIColor colorWithRed:0.35f green:0.55f blue:1.5f alpha:1.0f];

    [closeButton addTarget:self action:@selector(hidePicker) forControlEvents:UIControlEventValueChanged];

    [previousButton addTarget:self action:@selector(previousPicker) forControlEvents:UIControlEventValueChanged];

    [nextButton addTarget:self action:@selector(nextPicker) forControlEvents:UIControlEventValueChanged];

    if(currentDelegate > 0)[actionSheet addSubview:previousButton];
    if(currentDelegate < 6)[actionSheet addSubview:nextButton];

    [actionSheet addSubview:closeButton];

    [actionSheet showInView:[[UIApplication sharedApplication] keyWindow]];

    [actionSheet setBounds:CGRectMake(0, 0, 320, 485)];
}

-(void) nextPicker{

    [self hidePicker];

    currentDelegate++;

    [self showPicker];
}

-(void) previousPicker{

    [self hidePicker];
    currentDelegate--;

    [self showPicker];
}

-(void)hidePicker{


    switch ((uint)currentDelegate) {
        case 0:{
            [tf_type setText: [propertyDelegate.values objectAtIndex: [propertyDelegate index]]];
            _appDelegate.int_typeV = [propertyDelegate index];
            break;
        }
        case 1:{
            [tf_region setText: [regionDelegate.values objectAtIndex: [regionDelegate index]]];
            _appDelegate.int_regionV = [regionDelegate index];

            //Reset Town dropdown when a region is picked
            _appDelegate.int_townV = 0;
            townDelegate = [[TownDelegate alloc] init];
            [self.tf_town setText: [townDelegate.values objectAtIndex: [_appDelegate int_townV]]];

            break;
        }
        case 2:{
            [tf_town setText: [townDelegate.values objectAtIndex: [townDelegate index]]];
            _appDelegate.int_townV = [townDelegate index];
            break;
        }
        case 3:{
            [tf_numBed setText: [bedDelegate.values objectAtIndex: [bedDelegate index]]];
            _appDelegate.int_numBedV = [bedDelegate index];
            break;
        }
        case 4:{
            [tf_numBath setText: [bathDelegate.values objectAtIndex: [bathDelegate index]]];
            _appDelegate.int_numBathV = [bathDelegate index];
            break;
        }
        case 5:{
            NSString * priceString = [[NSString alloc] initWithFormat:@"%@ - %@", 
                                      [priceDelegate.values objectAtIndex: [priceDelegate index1]], 
                                      [priceDelegate.values2 objectAtIndex: [priceDelegate index2]]];
            [tf_price setText:priceString];

            _appDelegate.int_minPriceV = [priceDelegate index1];
            _appDelegate.int_maxPriceV = [priceDelegate index2];
            break;
        }
        case 6:{
            [tf_currency setText: [currencyDelegate.values objectAtIndex: [currencyDelegate index]]];
            _appDelegate.int_currencyV = [currencyDelegate index];
            break;
        }
    }

    [popoverController dismissPopoverAnimated:YES];
    popoverController = nil;

    [self dismissActionSheet];
}

My picker view

#import "PropertyTypeDelegate.h"

@implementation PropertyTypeDelegate
@synthesize values;


-(id)init{

    self = [super init];
    [self loadData];

    return self;
}

-(int)index{ return index;}
-(void) loadData{



    NSArray* array = [[NSArray alloc] initWithObjects: 
                      NSLocalizedString(@"No Preference", nil),
                      NSLocalizedString(@"Villa", nil),
                      NSLocalizedString(@"Town House", nil),
                      NSLocalizedString(@"Apartment", nil),
                      NSLocalizedString(@"Retail", nil),
                      NSLocalizedString(@"Labour Camp", nil),
                      NSLocalizedString(@"Office", nil),
                       NSLocalizedString(@"Warehouse", nil),
                      NSLocalizedString(@"Land Residential", nil),
                      NSLocalizedString(@"Hotel apartment", nil),
                      NSLocalizedString(@"Residential Building", nil),

                      nil];

    self.values = array;

    index = 0;
}





    -(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 1;
     }

    -  (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    return [values count];
}




    -(NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent: (NSInteger)component{ return [values objectAtIndex:row];
}

    -(void)pickerView:(UIPickerView *) thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component { 
    index  = (int) (NSInteger)row;
}



    @end 

Solution

  • The link provided above actually refers to Apple Doc where it has removed adding subview to UIActionSheet. In your code, you are doing similar thing by adding UIPickerView into UIActionSheet. So in iOS8 onwards, even if the view is added to UIActionSheet, the view returned is actually nil while displaying.

    For this purpose you can use ActionSheetPicker-3.0. Actually, it's not UIActionSheet anymore. But looks exactly the same, and that's why it works on iOS8. Do let me know if this answers your query!