【问题标题】:Invalid context error on iOS 7 when adding a UIPickerView inside a UIActionSheet?在 UIActionSheet 中添加 UIPickerView 时,iOS 7 上出现无效的上下文错误?
【发布时间】:2013-10-01 09:56:15
【问题描述】:

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

Add UIPickerView & a Button in Action sheet - How?

how to add UIPickerView in UIActionSheet

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

错误也带有一个很好的消息:

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

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

【问题讨论】:

  • 是的,Apple 终于在很长一段时间内执行了他们在documentation 中所说的话。 “UIActionSheet 不是为子类而设计的,也不应将视图添加到其层次结构中。如果您需要提供比 UIActionSheet API 提供的更多自定义的表单,您可以创建自己的表单并使用 presentViewController:animated:completion 以模态方式呈现它:."
  • 每个向操作表添加视图的建议都被误导了。这从来都不是UIActionSheet 的目的。找到一种更合适的方式来显示选择器视图。
  • 感谢您确认我的怀疑。同时,我查看了这个示例,它提供了一个滑入式 UIDatePicker,应该可以轻松地重写为 UIPickerView:developer.apple.com/library/ios/samplecode/DateCell/…

标签: iphone ios ios7 uipickerview uiactionsheet


【解决方案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];
}

// 使用此代码打开日期选择器或 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 -mark 条形按钮操作

-(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 委托

 - (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];
   }

【讨论】:

    【解决方案2】:

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

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

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

    CGRect newFrame = actionSheet.frame;
    newFrame = self.view.bounds.size.height - myCustomPicker.frame.size.height;
    newFrame = myCustomPicker.frame.size.height;
    actionSheet.frame = newFrame;
    

    【讨论】:

    • 很好,谢谢!你能解释一下为什么这会删除警告吗?
    • 在带有 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;
    【解决方案3】:

    正如上面的 cmets 所指出的,UIActionSheet 不是为子类化或采用其他视图而设计的。

    更多信息请点击此处:

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

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

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

    【讨论】:

    • 你是对的。对我来说是个坏消息,因为我在 iOS 6 中使用操作表做了一些很酷的事情,但我怀疑我最好停止尝试操纵操作表的视图层次结构。谢谢。
    • 你说得有道理,虽然我的特殊情况是为 ScrollView 使用中间 contentView,但你救了我的命...
    猜你喜欢
    • 1970-01-01
    • 2014-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-30
    • 2013-09-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多