我在UIActionSheet中有一个UIPickerView,并且在SO的许多其他人建议的方式做到了这一点:
Add UIPickerView & a Button in Action sheet - How?
how to add UIPickerView in UIActionSheet
直到现在,在iOS 7上测试我的应用程序时,解决方案运行良好。它仍然有效,但在Xcode的运行时期间我收到了很多“无效的上下文0x0”警告。
错误也带来了一个很好的消息:
CGContextSetFillColorWithColor:无效的上下文0x0。这是一个严重的错误。该应用程序或其使用的库正在使用无效的上下文,从而导致系统稳定性和可靠性的整体降低。此通知是礼貌的:请解决此问题。在即将到来的更新中,它将成为一个致命的错误。
可能是Apple最终想要停止这种解决方案,还是我们可以解决或解决这个问题?
答案 0 :(得分:11)
此“无效上下文0x0”警告的解决方法是使用非零标题初始化UIActionSheet(非零按钮也可以解决错误,但会导致视觉伪影)。
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;
答案 1 :(得分:2)
正如上面的评论所指出的那样,UIActionSheet并非设计为子类或其他视图。
Apple的更多信息:
一种方法是仔细查看DateCell示例代码并将其更改为使用UIPickerView:
答案 2 :(得分: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)
{
}];
}
-(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];
}