Search code examples
ios8uipickerviewuiactionsheetsubviews

xcode ios NSRangeException on creating subviews


how do I change the code below to fix the pickerView being depreciated?

This code has worked well before and did the job. I havent needed to compile for a while and when I was fixing another problem I bumped into this NSRangeException when it hits the subview. The code used to work with a previous version of IOS. Any thoughts would be appreciated.

Terminating app due to uncaught exception 'NSRangeException', reason: '-[__NSArrayM objectAtIndex:]: index 1 beyond bounds [0 .. 0]'

First throw call stack:... etc etc

After more reading I find that the pickerView is depreciated.

Never will understand why Apple can just change code!

-(void)willPresentActionSheet:(UIActionSheet *)actionSheet {

    switch ([actionSheet tag] ) {
        case 1://date

        {    
            NSDate *d;

            UIDatePicker *pickerView = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 50, 100, 116)];

            [pickerView setTag:100+[actionSheet  tag]];

            [pickerView setDatePickerMode:UIDatePickerModeDate];

            if( DefDate == @"" || DefDate == Nil ) DefDate = [fun0 GetCurrentDate];

            d = [fun0 GetDatefromString:DefDate];

            [pickerView setDate:d animated:YES];

            [actionSheet addSubview:pickerView];

            [pickerView release];

            NSArray *subViews = [actionSheet subviews];


**// Line below is where is where it dumps:**

            [[subViews objectAtIndex: SelectButtonIndex] setFrame:CGRectMake(20, 266, 280, 46)]; 
            [[subViews objectAtIndex:CancelButtonIndex] setFrame:CGRectMake(20, 317, 280, 46)];



        }

            break; 

    }


}




- (IBAction)btnDate:(id)sender {



    UIActionSheet *asheet = [[UIActionSheet alloc] 

                             initWithTitle:@"Pick the date of your meal" 
                             delegate:self
                             cancelButtonTitle:@""
                             destructiveButtonTitle:nil 
                             otherButtonTitles:@"Select"
                             , nil];

    [asheet setTag:1];
    [asheet showInView:[self.view superview]]; 

    [asheet setFrame:CGRectMake(0, 117, 320, 383)];
    [asheet release];

}

Solution

  • I've tested it with iOS 8.0 (and running on XCode6.1) and above, and your code and UIActionSheet both looks nice.

    It can compile, run and showing me the output.

    Here's what I get.

    enter image description here

    Update:

    From iOS 8.0 and above, UIActionSheet is depreciated. Also its stop supporting add subviews too. This means device with iOS 8.0 and above will not show you UIDatePicker as subview of it. If notice, nightmare is not the error line you've specified below the comment but its not showing data picker any more inside action sheet.

    From Documentation,

    UIActionSheet is not designed to be subclassed, nor should you add views to its hierarchy. If you need to present a sheet with more customization than provided by the UIActionSheet API, you can create your own and present it modally with presentViewController:animated:completion:.

    IMHO,

    1. You should upgrade your app and code to start supporting from iOS 8.0 only. UIActionSheet will be replace by UIAlertController. [Suggested]

    2. If you wish to support back iOS 8 then you've to use a third party action sheet which you can use in place of UIActionSheet in all iOS versions. Here's the one, https://github.com/skywinder/ActionSheetPicker-3.0 [Good Alternative]

    3. If you don't want to use third party and still wants to support all iOS versions, then you can runtime check for iOS versions and based on that you can either show UIActionSheet and UIAlertController. [Not Recommended]

    Few links which may help you.

    https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIAlertController_class/

    http://nshipster.com/uialertcontroller/

    Add UIPickerView in UIActionSheet from IOS 8 not working

    Showing a UIPickerView with UIActionSheet in iOS8 not working