iOS之自訂pickerview(行駛裡程數),iospickerview

來源:互聯網
上載者:User

iOS之自訂pickerview(行駛裡程數),iospickerview

#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];

}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.