你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> iOS學習筆記—— UIPickerView 控件的簡單使用

iOS學習筆記—— UIPickerView 控件的簡單使用

編輯:IOS開發綜合

UIPickerView 是iOS常用的控件之一,它通過輪轉界面提供一系列多值選項,它向用戶展示信息,也能收集用戶輸入。下面是一個普通的UIPickerView控件。

\

使用UIPickerView控件需要遵守兩種協議,一種是UIPickerViewDelegate,另一種是UIPickerViewDataSource

UIPickerViewDelegate協議的方法有:

1. -(NSString *) pickerView: (UIPickerView * )pickerView

titleForRow: (NSInteger) row<喎?/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA+ICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgZm9yQ29tcG9uZW50OiAoTlNJbnRlcmdlcikgY29tcG9uZW50PC9wPgo8cD4gICAgICC4w7e9t6i4+b7d1ri2qLXE0NC6xbe1u9i4w9DQtcSx6szioaM8L3A+CjxwPjwvcD4KPHA+CiAgICAgICAyLiAgLSh2b2lkKSBwaWNrZXJWaWV3OiAoVUlQaWNrZXJWaWV3ICopcGlja2VyVmlldyA8L3A+CjxwPgogICAgICAgICAgICAgICAgICAgZGlkU2VsZWN0Um93OiAoTlNJbnRlZ2VyKXJvdzwvcD4KPHA+CiAgICAgICAgICAgICAgICAgICBpbkNvbXBvbmVudDogKE5TSW50ZWdlciljb21wb25lbnQ8L3A+CjxwPgogICAgICAgICAgICC1sdPDu6fRodTxxLO49nJvd8qxLHBpY2tlciB2aWV3tffTw7TLuq/K/TwvcD4KPHA+CiAgICAgICAzLiAgLSAoQ0dGbG9hdCkgcGlja2VyVmlldzooVUlQaWNrZXJWaWV3ICopcGlja2VyVmlldyA8L3A+CjxwPgogICAgICAgICByb3dIZWlnaHRGb3JDb21wb25lbnQ6IChOU0ludGVnZXIpIGNvbXBvbmVudDwvcD4KPHA+CiAgICAgICAgICAgINPJcGlja2VyIHZpZXe199PDLLWxxuTU2rvm1sZyb3fE2sjdLNDo0qpyb3e1xLjftsjKsTwvcD4KPHA+CiAgICAgICA0LiAgLSAoQ0dGbG9hdClwaWNrZXJWaWV3OiAoVUlQaWNrZXJWaWV3ICopcGlja2VyVmlldyA8L3A+CjxwPgogICAgICAgICAgICAgIHdpZHRoRm9yQ29tcG9uZW50OihOU0ludGVnZXIpIGNvbXBvbmVudDwvcD4KPHA+CiAgICAgICAgICAgILWxcGlja2VyIHZpZXcg0OjSqnJvd7XEv+22yMqxLLX308O0y7qvyv08L3A+CjxwPgogICAgICAgNS4gIC0gKFVJVmlldyAqKXBpY2tlclZpZXc6IChVSVBpY2tlclZpZXcgKilwaWNrZXJWaWV3PC9wPgo8cD4KICAgICAgICAgICAgICAgICAgICAgICAgICAgdmlld0ZvclJvdzooTlNJbnRlZ2VyKSByb3cgPC9wPgo8cD4KICAgICAgICAgICAgICAgICAgICAgICAgZm9yQ29tcG9uZW50OihOU0ludGVnZXIpIGNvbXBvbmVudDwvcD4KPHA+CiAgICAgICAgICAgICAgICAgICAgICAgICAgIHJldXNpbmdWaWV3OihVSVZpZXcgKikgdmlldzwvcD4KPHA+CiAgICAgICAgICAgILWxcGlja2VyIHZpZXfQ6NKquPjWuLaotcRjb21wb25lbnQucm931ri2qHZpZXfKsSy199PDtMu6r8r9Lre1u9gmIzIwNTQwO86q08PX93Jvd8TayN21xHZpZXc8L3A+CjxwPiAgICBVSVBpY2tlclZpZXdEYXRhU291cmNl0K3S6bXEt723qNPQo7o8L3A+CjxwPiAgICAgIDEuICAtKE5TSW50ZWdlcikgbnVtYmVyT2ZDb21wb25lbnRzbG5QaWNrZXJWaWV3OiAoVUlQaWNrZXJWaWV3ICopIHBpY2tlclZpZXc8L3A+CjxwPiAgICAgICAgICAguMO3vbeot7W72FVJUGlja2VyVmlld9Do0qq24MnZuPbX6bz+o6jT0LbgydnB0KOpoaM8L3A+CjxwPiAgICAgIDIuICAtKE5TSW50ZWdlcikgcGlja2VyVmlldzogKFVJUGlja2VyVmlldyAqKSBwaWNrZXJWaWV3PC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sZWN0Um93OiAoTlNJbnRlZ2VyKXJvdyA8L3A+CjxwPgogICAgICAgICAgICAgICAgIGluQ29tcG9uZW50OiAoTlNJbnRlZ2VyKWNvbXBvbmVudDwvcD4KPHA+CiAgICAgICAgICAgICAgICAgICAgICBhbmltYXRlZDogKEJPT0wpYW5pbWF0ZWQ8L3A+CjxwPgogICAgICAgICAg1Nq0+sLr1ri2qNKq0aHU8bXExLNjb21wb25lbnS1xMSzcm93PC9wPgo8cD4KICAgICAgICAgILLOyv1hbmltYXRlZCDI9M6qWUVTLNeqtq9zcGlutb3E49Gh1PG1xNDCJiMyMDU0MDs7yPTOqk5PLNaxvdPP1Mq+xOPRodTxtcQmIzIwNTQwOzwvcD4KPHA+CiAgICAgNy4gIC0oVUlWaWV3ICopIHZpZXdGb3JSb3c6IChOU0ludGVnZXIpcm93IDwvcD4KPHA+CiAgICAgICAgICAgICAgICAgICAgICAgICBmb3JDb21wb25lbnQ6IChOU0ludGVnZXIpY29tcG9uZW50PC9wPgo8cD4KICAgICAgICAgILe1u9jTyc6vzdC3vbeocGlja2VyVmlldzp2aWV3Rm9yUm93OmZvckNvbXBvbmVudHJldXNpbmdWaWV3Ota4tqi1xHZpZXcuyOe5+86vzdC3vbeozrTKtc/WLLvy1d/LtdXiuPZ2aWV3sqKyu8rHv8m8+7XELNTyt7W72G5pbDwvcD4KPGJyPgo8cD48YnI+CjwvcD4KPHA+yr7A/bT6wuujujwvcD4KPHA+dGV4dC5oPC9wPgo8cD48L3A+CjxwcmUgY2xhc3M9"brush:java;">@interface TBLViewController : UIViewController { NSArray *firstTypes; NSArray *secondTypes; } @property (strong, nonatomic) IBOutlet UIPickerView *pickerView; - (IBAction)Sure:(id)sender; @end


text.m

- (void)viewDidLoad				// 初始化
{
    [super viewDidLoad];

    firstTypes = [[NSArray alloc] initWithObjects:@"hello", @"hi", @"how are u", nil];
    secondTypes = [[NSArray alloc] initWithObjects:@"Zhao", @"Qian", @"Sun", @"Li", @"Zhou", @"Wu", @"Zheng", @"Wang", nil];
    pickerView.delegate = self;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

- (IBAction)Sure:(id)sender {			// 顯示選中信息
    NSInteger first = [pickerView selectedRowInComponent:0];		// 選中的行數
    NSInteger second = [pickerView selectedRowInComponent:1];
    
    NSString* firstString = [firstTypes objectAtIndex:first];		// 選中行的信息
    NSString* secondString = [secondTypes objectAtIndex:second];
    
    NSString* message = [NSString stringWithFormat:@"Your chooise is %@ && %@", firstString, secondString];
    
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Chooise Information" message:message delegate:nil cancelButtonTitle:@"ok" otherButtonTitles:nil];
    [alert show];
}

#pragma mark -
#pragma mark Picker Data Source Methed
- (NSInteger) numberOfComponentsInPickerView:(UIPickerView *)pickerView {	// 返回列數
    return 2;
}
- (NSInteger) pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{		// 返回該列行數
    if (component == 0) {
        return [firstTypes count];
    }
    else{
        return [secondTypes count];
    }
}

#pragma mark Picker Delegate Methods
- (NSString *) pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{	// 返回該行標題
    if (component == 0) {
        return [firstTypes objectAtIndex:row];
    }
    else{
        return [secondTypes objectAtIndex:row];
    }
}

@end


  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved