1

Showing a UIPickerView with UIActionSheet in iOS8 not showing

enter image description here

enter image description here

-(void)showPicker{

    /************************  FIXED please contact me on nfsarmento@hotmail.com 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 
Sviatoslav Yakymiv
  • 7,887
  • 2
  • 23
  • 43
Nuno Sarmento
  • 415
  • 5
  • 15

1 Answers1

1

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!

skywinder
  • 21,291
  • 15
  • 93
  • 123
VijayKumar
  • 256
  • 1
  • 3
  • 10
  • Thank you very much for your fast response, I have download the ActionSheetPicker it is a very smart solution, but I'm still a bit confuse and lost, how do I assign my views on this? Thank you – Nuno Sarmento Sep 20 '14 at 19:40
  • Please VijayKunar can you tell me how can I workout this " pickerView.dataSource = propertyDelegate; pickerView.delegate = propertyDelegate; [pickerView selectRow:[propertyDelegate index] inComponent:0 animated:NO]; " on this ActionSheetPicker – Nuno Sarmento Sep 21 '14 at 12:08
  • I have managed to add the ActionSheetPicker working on my project, what I can not understand is how to add data source on this example , can you please help me – Nuno Sarmento Sep 21 '14 at 12:18
  • Actually you dont have to do anything. Using ActionSheetStringPicker its much simpler to have the done block and cancel block executed. For specifying the datasource you would need to pass the dataArray objects of NSString to above method of yours – VijayKumar Sep 22 '14 at 06:32
  • How do I do that, please VijayKumar help me with this one – Nuno Sarmento Sep 22 '14 at 10:08
  • [ActionSheetStringPicker showPickerWithTitle:@"Select Number of Columns" rows:dataArray initialSelection:0 doneBlock:^(ActionSheetStringPicker *picker, NSInteger selectedIndex, id selectedValue) {} cancelBlock:^(ActionSheetStringPicker *picker){}origin:cell.detailTextLabel]; dataArray consists of string to be shown in the picker – VijayKumar Sep 22 '14 at 10:44
  • Thanks for fast response again, I have post the picker view which has my data, I need to fetch the data from my picker view into my ActionSheetPicker, I'm very sorry VijayKumar to ask you again for help, but please help me with this, I'm junior programmer and all this is driving me crazy. Thanks – Nuno Sarmento Sep 22 '14 at 15:51
  • Can you please look at the example project provided in the github for this library? Do let me know if you would still need help – VijayKumar Sep 24 '14 at 08:11
  • Thank you for replying, I just want to know how to pass the NSObjet array (my customer picker) to my ActionSheetPicker in a diferent view controller, that is what I'm struggling to do – Nuno Sarmento Sep 24 '14 at 09:07
  • Hi I have managed to implement the ActionSheetCustomPickerDelegate into my project and it works, I just follow the example and now Im able to have my pickers delegates parsing to my view controller, the only problem that I'm facing now is when a click on the picker to select the row the selection it doesn't place nothing on my label, can please help me with this one . thanks – Nuno Sarmento Sep 26 '14 at 09:37
  • You can add the your label in the target field and also fill the details in the doneblock. – VijayKumar Sep 27 '14 at 11:19
  • Thanks, I also believe we can add it, my problem is how to do it – Nuno Sarmento Sep 27 '14 at 20:41
  • [ActionSheetStringPicker showPickerWithTitle:@"MyTitle" rows:dataArray initialSelection:0 doneBlock:^(ActionSheetStringPicker *picker, NSInteger selectedIndex, id selectedValue) { yourTextField.text = dataArray objectAtIndex[selectedIndex]; } } cancelBlock:^(ActionSheetStringPicker *picker) { // Your code for cancel } origin:yourTextField]; – VijayKumar Sep 29 '14 at 06:21
  • This is my IBAction : - (IBAction)SelectPropertyType:(UIControl *)sender { PropertyTypeDelegate *delg = [[PropertyTypeDelegate alloc] init]; NSNumber *yass1 = @1; NSArray *initialSelections = @[yass1];} . Where I can assign the text.field ? – Nuno Sarmento Sep 29 '14 at 09:28
  • the target is one where you want to invoke your action sheet from. In my case it was textfield, in your case you should know on touch or on click of what should the ActionSheet be shown – VijayKumar Sep 29 '14 at 11:09
  • I'm completely lost with all this – Nuno Sarmento Sep 29 '14 at 11:29
  • NSArray *anotherArray = [[NSArray alloc] initWithObjects:@"0", @"2", @"3", @"8", nil]; [ActionSheetStringPicker showPickerWithTitle:actionSheet.title rows:anotherArray initialSelection:0 doneBlock:^(ActionSheetStringPicker *picker, NSInteger selectedIndex, id selectedValue) { NSIndexPath *indexPath = [NSIndexPath indexPathForRow:4 inSection:0]; if(iUnivHall == 1){ if(txtField != nil){ txtField.text = [dataArray objectAtIndex: selectedIndex]; } } } cancelBlock:^(ActionSheetStringPicker *picker) { // Nothing to be done } origin:txtField]; – VijayKumar Sep 30 '14 at 05:22
  • I give up :( , I would pay to see this done 1 week already scratching my head because of this, do you know anyone can do this job , I will pay for it – Nuno Sarmento Sep 30 '14 at 10:44
  • Whats the issue? Its quite clear here, textfield is my uitextfield from where I wish to show the actionsheet, and on click of done button in the actionsheet I am getting the value from the same array that I had given in the array to be filled back to textfield. This is done by getting value using the passed selected index in the done method. – VijayKumar Sep 30 '14 at 10:46
  • That works when it is used in the same view calling the array from the viewDidLoad. My scenario is different I'm calling my array from a NSObject Delegate . This is how I'm calling my array from my NSObject "PropertyDelegate" . - (IBAction)SelectPropertyType:(UIControl *)sender { PropertyTypeDelegate *delg = [[PropertyTypeDelegate alloc] init]; NSNumber *yass1 = @1; NSArray *initialSelections = @[yass1]; [ActionSheetCustomPicker showPickerWithTitle:@"Property Type" delegate:delg showCancelButton:YES origin:sender initialSelections:initialSelections ]; }- – Nuno Sarmento Sep 30 '14 at 22:23
  • I'm able to populate the picker what I can not do is to add value to the text.field – Nuno Sarmento Sep 30 '14 at 22:25
  • Would it not be possible for you to use ActionSheetStringPicker instead? – VijayKumar Oct 01 '14 at 06:15
  • that is what I'm trying to use – Nuno Sarmento Oct 01 '14 at 09:50
  • Please I really appreciate some help here – Nuno Sarmento Oct 06 '14 at 08:40
  • I have used ActionSheetStringPicker and I had posted you my example too, I am not sure as to what more help you might need on this. If you are fine with dropping your code file to me over email, I can have a look at it later vijaydogra@gmail.com – VijayKumar Oct 06 '14 at 12:09
  • Ok thank you very much again , I will send you the whole project without "ActionSheetPicker project in github (https://github.com/TimCinel/ActionSheetPicker)" . Please let me know how much it is, I WANT to pay you for the job. The emailwill have the Subject of " Project ActionSheet". kind regards, – Nuno Sarmento Oct 06 '14 at 12:35
  • @VijayKumar My new version of `ActionSheetPicker` https://github.com/skywinder/ActionSheetPicker-3.0 works with iOS8! Check my answer below http://stackoverflow.com/a/26343431/1698467 – skywinder Oct 13 '14 at 15:22
  • @skywinder - Will do that! Thanks – VijayKumar Oct 14 '14 at 04:45