Invalid context error on iOS 7 when adding a UIPickerView inside a UIActionSheet?

前端 未结 3 576
悲哀的现实
悲哀的现实 2021-01-05 00:14

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

3条回答
  •  天命终不由人
    2021-01-05 00:30

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

提交回复
热议问题