Iphone 在UIActionSheet中添加UIPickerView时,iOS 7上的上下文错误无效?

Iphone 在UIActionSheet中添加UIPickerView时,iOS 7上的上下文错误无效?,iphone,ios,ios7,uipickerview,uiactionsheet,Iphone,Ios,Ios7,Uipickerview,Uiactionsheet,我在UIActionSheet中有一个UIPickerView,并以SO中许多其他人建议的方式完成了这项工作: 到目前为止,在iOS 7上测试我的应用程序时,这些解决方案一直运行良好。它仍然可以工作,但我在Xcode运行时收到了很多“无效上下文0x0”警告 错误也带来了一个好消息: CGContextSetFillColorWithColor:上下文0x0无效。这是一个严重的错误。此应用程序或其使用的库正在使用无效的上下文,从而导致系统稳定性和可靠性的整体降级。此通知是出于礼貌:请解决此问题

我在UIActionSheet中有一个UIPickerView,并以SO中许多其他人建议的方式完成了这项工作:

到目前为止,在iOS 7上测试我的应用程序时,这些解决方案一直运行良好。它仍然可以工作,但我在Xcode运行时收到了很多“无效上下文0x0”警告

错误也带来了一个好消息: CGContextSetFillColorWithColor:上下文0x0无效。这是一个严重的错误。此应用程序或其使用的库正在使用无效的上下文,从而导致系统稳定性和可靠性的整体降级。此通知是出于礼貌:请解决此问题。这将成为即将进行的更新中的致命错误


可能是苹果最终想要停止这种解决方案,还是我们可以解决或修复它?

正如上面的评论所指出的,UIActionSheet并不是设计成子类或采取其他观点的

更多信息请访问苹果:

一种方法是仔细查看DateCell示例代码,并将其更改为使用UIPickerView:


此“无效上下文0x0”警告的解决方法是使用非nil标题初始化UIActionSheet(非nil按钮也可以解决此错误,但会导致视觉伪影)

然后,您需要调整操作表的框架,使其在iOS7和以前的版本中都能正确定位(请确保在showInView方法之后执行此操作)

//使用此代码打开日期选择器或UiPicker视图

-(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-标记栏按钮操作 pragma-标记PickerView委托
是的,苹果公司终于开始执行他们在过去很长一段时间里所说的话了。UIActionSheet不是为子类化而设计的,您也不应该将视图添加到其层次结构中。如果您需要提供比UIActionSheet API提供的自定义更多的工作表,您可以创建自己的工作表,并使用presentViewController:animated:completion:以模式显示它每一个将观点添加到行动表的建议都被误导了。这从来不是
UIActionSheet
的目的。找到一个更合适的方式来显示选择器视图。感谢您确认我的怀疑。同时,我看了这个例子,它提供了一个UIDatePicker中的幻灯片,应该可以毫不费力地重写为UIPickerView:你是对的。这对我来说是个坏消息,因为我在iOS 6中用actionsheet做了一些很酷的事情,但我想我最好停止操纵actionsheet的视图层次结构。谢谢。你提出了一个观点,虽然我的特殊案例是使用中间内容视图作为ScrollView,但你救了我的命……很好,谢谢!您能解释一下为什么会删除警告吗?在带有nil按钮和nil标题的UIActionSheet上调用showInView会导致错误,这与添加子视图无关。显然,没有内容的操作表毫无意义,也不会在iOS7.newFrame=self.view.bounds.size.height-myCustomPicker.frame.size.height中绘制;这给我带来了麻烦。newFrame是CGRect,myCustomPicker.frame.size.height是浮点。我是不是遗漏了什么?顺便说一句,myCustomerPicker是一个UIDatePicker。你是对的,它看起来确实错了。如何:newFrame.origin.y=self.view.bounds.size.height-myCustomPicker.frame.size.height;newFrame.size.height=myCustomPicker.frame.size.height;
CGRect newFrame = actionSheet.frame;
newFrame = self.view.bounds.size.height - myCustomPicker.frame.size.height;
newFrame = myCustomPicker.frame.size.height;
actionSheet.frame = newFrame;
-(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];
}
-(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)
 {
     
 }];
}
-(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;
 }];
 }
 - (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];
   }