9

I have an UIPickerView inside an UIActionSheet and have done that in a way suggested by many others here at SO:

Add UIPickerView & a Button in Action sheet - How?

how to add UIPickerView in UIActionSheet

The solutions have worked fine until now when testing my app on iOS 7. It still works but I got a lot of "invalid context 0x0" warnings during runtime in Xcode.

The errors are coming with a nice message too:

CGContextSetFillColorWithColor: invalid context 0x0. This is a serious error. This application, or a library it uses, is using an invalid context and is thereby contributing to an overall degradation of system stability and reliability. This notice is a courtesy: please fix this problem. It will become a fatal error in an upcoming update.

Could it be that Apple finally want to stop this kind of solution or is it something we can work around or fix?

Community
  • 1
  • 1
Magnus
  • 735
  • 3
  • 9
  • 20
  • 3
    Yes, Apple is finally enforcing what they have said in the [documentation](https://developer.apple.com/library/ios/documentation/uikit/reference/UIActionSheet_Class/Reference/Reference.html) for a long time. "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:." – Matthias Bauch Sep 25 '13 at 17:13
  • 1
    Every suggestion to add views to an action sheet have been misguided. That has never been the purpose of `UIActionSheet`. Find a more appropriate way to display the picker view. – rmaddy Sep 25 '13 at 17:15
  • Thanks for confirming my suspicions. Meanwhile I have looked at this example which provide a slide-in UIDatePicker which should be possible to rewrite to an UIPickerView with no hassle: https://developer.apple.com/library/ios/samplecode/DateCell/Introduction/Intro.html#//apple_ref/doc/uid/DTS40008866-Intro-DontLinkElementID_2 – Magnus Sep 27 '13 at 09:35

3 Answers3

11

A workaround for this 'invalid context 0x0' warning is to init the UIActionSheet with a non nil title (non-nil buttons also resolve the error but result in visual artefacts).

UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"" 
                                                         delegate:nil
                                                cancelButtonTitle:nil
                                           destructiveButtonTitle:nil
                                                otherButtonTitles:nil];

You will then need to adjust the actionsheet's frame for it to position properly in both iOS7 and previous versions (make sure to do this after the showInView method).

CGRect newFrame = actionSheet.frame;
newFrame = self.view.bounds.size.height - myCustomPicker.frame.size.height;
newFrame = myCustomPicker.frame.size.height;
actionSheet.frame = newFrame;
alexkent
  • 1,556
  • 11
  • 25
  • Nice, thank! Are you able you explain why this removes the warning? – mts Dec 03 '13 at 02:29
  • 1
    Calling showInView on a UIActionSheet with nil buttons and nil title causes the error, it has nothing to do with adding subviews. Apparently action sheets with no contents make no sense and don't draw in iOS7. – alexkent Dec 03 '13 at 14:47
  • newFrame = self.view.bounds.size.height - myCustomPicker.frame.size.height; is giving me problem. newFrame is a CGRect and myCustomPicker.frame.size.height is a float. Am I missing something here? Btw, myCustomerPicker is a UIDatePicker – Michael Apr 04 '14 at 00:27
  • you're right, it does look wrong. how about: newFrame.origin.y = self.view.bounds.size.height - myCustomPicker.frame.size.height; newFrame.size.height = myCustomPicker.frame.size.height; – alexkent Apr 05 '14 at 07:45
2

As the comments above point out, the UIActionSheet is not designed to be subclassed or take other views.

More info here at Apple:

https://developer.apple.com/library/ios/documentation/uikit/reference/UIActionSheet_Class/Reference/Reference.html#//apple_ref/doc/uid/TP40006801-CH3-DontLinkElementID_2

One approach is to look closer at the DateCell sample code and change that to use an UIPickerView instead:

https://developer.apple.com/library/ios/samplecode/DateCell/Introduction/Intro.html#//apple_ref/doc/uid/DTS40008866-Intro-DontLinkElementID_2

Magnus
  • 735
  • 3
  • 9
  • 20
  • You're right. Bad news for me since I was doing some cool things with the action sheet in iOS 6, but I suspect I better stop trying to manipulate the actionsheet's view hierarchy. Thanks. – Kyle Clegg Oct 02 '13 at 02:22
  • You made a point, although my particular case was using an intermediate contentView for ScrollView, you saved my life... – Frederic Yesid Peña Sánchez Oct 28 '13 at 04:59
1
-(void)viewDidLoad
{
  [super viewDidLoad];
// Make Your own Action sheet

myCustomeActionSheet=[[UIView alloc] initWithFrame:CGRectMake(0,self.view.frame.size.height+1,self.view.frame.size.width,215)];
myCustomeActionSheet.backgroundColor=[UIColor whiteColor];
[self.view addSubview:myCustomeActionSheet];
}

// Use this code To Open Date Picker Or UiPicker View

-(void)OpenActionSheet:(Boolean)isDatePickere
{
SelectedString=Nil;
if (datepickerView) {
    [datepickerView removeFromSuperview];
    datepickerView=Nil;
}
if (picker) {
    [picker removeFromSuperview];
    picker=Nil;
}
if (isDatePickere)
{
    // Add the  Datepicker
    datepickerView= [[UIDatePicker alloc] init];
    datepickerView.datePickerMode = UIDatePickerModeDateAndTime;
    datepickerView.minimumDate=[NSDate date];
    [myCustomeActionSheet addSubview:datepickerView];
}
else
{
    // Add Picker View
    picker = [[UIPickerView alloc] initWithFrame:CGRectMake(0,40, 320, 216)];
    picker.showsSelectionIndicator=YES;
    picker.dataSource = self;
    picker.delegate = self;
    [myCustomeActionSheet addSubview:picker];
}

UIToolbar *tools=[[UIToolbar alloc]initWithFrame:CGRectMake(0, 0,320,40)];
tools.barStyle=UIBarStyleBlackOpaque;
[myCustomeActionSheet addSubview:tools];

UIBarButtonItem *doneButton=[[UIBarButtonItem alloc]initWithTitle:@"Done" style:UIBarButtonItemStyleBordered target:self action:@selector(btnActinDoneClicked)];
doneButton.imageInsets=UIEdgeInsetsMake(200, 6, 50, 25);
UIBarButtonItem *CancelButton=[[UIBarButtonItem alloc]initWithTitle:@"Cancel" style:UIBarButtonItemStyleBordered target:self action:@selector(btnActinCancelClicked)];

UIBarButtonItem *flexSpace= [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];

NSArray *array = [[NSArray alloc]initWithObjects:CancelButton,flexSpace,flexSpace,doneButton,nil];

[tools setItems:array];

//picker title
UILabel *lblPickerTitle=[[UILabel alloc]initWithFrame:CGRectMake(60,8, 200, 25)];
lblPickerTitle.text=@"Select";
lblPickerTitle.backgroundColor=[UIColor clearColor];
lblPickerTitle.textColor=[UIColor whiteColor];
lblPickerTitle.textAlignment=NSTextAlignmentCenter;
lblPickerTitle.font=[UIFont boldSystemFontOfSize:15];
[tools addSubview:lblPickerTitle];

[UIView animateWithDuration:0.5 animations:^{
    myCustomeActionSheet.frame=CGRectMake(0,self.view.frame.size.height-myCustomeActionSheet.frame.size.height,myCustomeActionSheet.frame.size.width,myCustomeActionSheet.frame.size.height);
} completion:^(BOOL finished)
 {
     
 }];
}

pragma -mark Bar Button Action

-(void)btnActinDoneClicked
{
if (SelectedString==Nil)
{
    SelectedString=[pickerArrayList objectAtIndex:0];
}

if (datepickerView)
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    
    NSLocale *posix = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
    [dateFormatter setLocale:posix];
    [dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
    [dateFormatter setDateFormat:@"DD/MM/yy hh:mm a"];
    
    [selectedButton setTitle:[dateFormatter stringFromDate:datepickerView.date] forState:UIControlStateNormal];
}
else
    [selectedButton setTitle:SelectedString forState:UIControlStateNormal];

    [self performSelector:@selector(btnActinCancelClicked) withObject:nil afterDelay:0.10];

 }
-(void)btnActinCancelClicked
{
[UIView animateWithDuration:0.5 animations:^{
    
    viewActiobSheetView.frame=CGRectMake(0,self.view.frame.size.height+1,self.view.frame.size.width,viewActiobSheetView.frame.size.height);
    
}
                 completion:^(BOOL finished)
 {
     [datepickerView removeFromSuperview];
     [picker removeFromSuperview];
     datepickerView=Nil;
     picker=Nil;
 }];
 }

pragma -mark PickerView Delegate

 - (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:  (NSInteger)component reusingView:(UIView *)view
{
  UILabel *label=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 40)];
  label.text=[pickerArrayList objectAtIndex:row];
  label.textAlignment=NSTextAlignmentCenter;
  label.textColor=[UIColor blackColor];
  return label;
 }
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;
  {
    return 1;
  }
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
  {
    return [pickerArrayList count];
  }
 -(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
   {
    SelectedString=[pickerArrayList objectAtIndex:row];
   }
Community
  • 1
  • 1
Jignesh Mayani
  • 6,937
  • 1
  • 20
  • 36