ToolBar at the top of UIPIckerView in xcode?

后端 未结 6 1129
清歌不尽
清歌不尽 2020-12-20 20:08

I need to add a toolbar with done button on the top of UIPickerView. I don\'t want to use actionSheet because I want the rest of the v

6条回答
  •  情深已故
    2020-12-20 20:15

    You can getPicker using this

    -(void)getValuePicker
    {
        ViewForValuePicker = [[UIView alloc]initWithFrame:CGRectMake(0, 219, 320, 266)];
    
        UIToolbar *toolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
        toolBar.barStyle = UIBarStyleBlackOpaque;
    
        UIBarButtonItem *btn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneBtnPressToGetValue)];
    
        [toolBar setItems:[NSArray arrayWithObject:btn]];
        [ViewForValuePicker addSubview:toolBar];
    
    
        valuePicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 44, 320, 216)];
        valuePicker.delegate=self;
        valuePicker.dataSource=self;
        valuePicker.showsSelectionIndicator=YES;
    
        [ViewForValuePicker addSubview:valuePicker];
    
        [appDelegate.window addSubview:ViewForValuePicker];
    } 
    

    And its Delegete Method

    -(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
    {
        return 1;
    }
    
    - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component;
    {
        return [pickerValueAry count];
    }
    
    - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;
    {
        NSMutableArray *ary = [[NSMutableArray alloc] initWithArray:pickerValueAry];
        id str=[ary objectAtIndex:row];
        return str;
    }
    
    - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
    {    
        NSLog(@"selectedRowInPicker >> %d",row);
    }  
    

    You can follow my answer for more Link

提交回复
热议问题