爱程序网

iOS之自定义pickerview(行驶里程数)

来源: 阅读:

#pragma mark -- 里程数按钮的点击事件

- (void)mileageBtnClicked:(UIButton *)sender {

    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"里程数/km\n\n\n\n\n\n\n\n" message:nil preferredStyle:UIAlertControllerStyleAlert];

    UIPickerView *mileage = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 40, 270, 150)];

    //指定Delegate

    mileage.delegate = self;

    //显示选中框

    mileage.showsSelectionIndicator = YES;

    [alert.view addSubview:mileage];

    

    UIAlertAction *ok = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {

        NSInteger row = [mileage selectedRowInComponent:0];

        NSString *mileageNum = [pickerData objectAtIndex:row];

        mileageLabel.text = [NSString stringWithFormat:@"%@km", mileageNum];

    }];

    

    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {

    }];

    

    [alert addAction:ok];

    [alert addAction:cancel];

    [self presentViewController:alert animated:YES completion:^{ }];

    

    pickerData = [[NSArray alloc] initWithObjects:@"3000",@"3500",@"8000",@"8500",@"13000",@"13500",@"18000",@"18500",@"23000",@"23500",@"28500",@"33000",@"33500",@"38000",@"38500",@"43000",@"43500",@"48000",@"48500",@"53000",@"53500",@"58000",@"58500", nil];

}

 #pragma mark -- UIPickerViewDataSource

//返回显示的列数

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {

    return 1;

}

//返回显示的行数

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {

    return pickerData.count;

}

#pragma mark -- UIPickerViewDelegate

//返回当前行的内容

- (nullable NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component __TVOS_PROHIBITED {

    return [pickerData objectAtIndex:row];

}

 

关于爱程序网 - 联系我们 - 广告服务 - 友情链接 - 网站地图 - 版权声明 - 人才招聘 - 帮助