2012-02-18 192 views
2

我有Xcode 4.2,我正嘗試創建一個下拉菜單。我希望下拉菜單下拉單詞或數字。然後,當我從列表中選擇一些內容時,我希望它出現在不同頁面的表格中。我的問題是:我會如何寫這個? 謝謝UIPickerview下拉菜單

+0

我會很感激 – 2012-02-18 15:10:53

回答

7

首先通過File> New> New File並按照提示創建一個帶有nib文件的UIViewController子類。

Select UIViewController subclass

Name your class

現在打開筆尖(MyViewController.xib)。

MyViewController.xib

你應該拖動一個UIPickerView和一個UITableView到視圖,並且按照自己喜歡安排他們(沒關係)。

UITableView

UIPickerView

Arrange the interface elements

下一頁打開您的UIViewController子類的頭文件(MyViewController.h)。

MyViewController.h

@interface MyViewController : UIViewController { 

行的末尾添加<UITableViewDelegate, UITableViewDataSource, UIPickerViewDelegate, UIPickerViewDataSource>,所以它看起來像這樣

@interface MyViewController : UIViewController <UITableViewDelegate, UITableViewDataSource, UIPickerViewDelegate, UIPickerViewDataSource> 

接下來,您將需要將引用添加到您的tableview和選擇器,也兩個值的數組。正上方@end添加以下代碼行

@property (strong, nonatomic) IBOutlet UITableView* tableView; 
@property (strong, nonatomic) IBOutlet UIPickerView* pickerView; 
@property (strong, nonatomic) NSMutableArray* tableData; 
@property (strong, nonatomic) NSMutableArray* pickerData; 

MyViewController.h should look like this

然後就回到你的筆尖文件和的UITableView和UIPickerView連接到這些變量。

Connect your components

現在在你的源文件(MyViewController.m),你需要合成你引用。因此,僅低於@implementation MyViewController

MyViewController.m

現在添加@synthesize tableView, pickerView, tableData, pickerData將文件添加委託方法,會有相當多的人,但他們非常自我解釋。

爲UITableView的委託方法是

- (NSInteger)numberOfSectionsInTableView:(UITableView*)tableView; 

- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section; 

- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath; 

- (void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath; 

爲UIPickerView委託方法是

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

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component; 

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

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component; 

他們應該被添加到源文件,並使用如下

的UITableView:

- (NSInteger)numberOfSectionsInTableView:(UITableView*)tableView { 
    // The number of sections in the UITableView 
    return 1; 
} 

- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section { 
    // The number of rows in the UITableView 
    return [tableData count]; 
} 

- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; 
    } 

    // Set the table cell text to the appropriate value in tableData 
    cell.textLabel.text = [tableData objectAtIndex:indexPath.row]; 

    return cell; 
} 

- (void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    // Whatever happens when you select a table view row. 
} 

UIPickerView:

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView; 
{ 
    // The number of sections in the UIPickerView 
    return 1; 
} 

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component; 
{ 
    // The number of rows in the UIPickerView 
    return [pickerData count]; 
} 

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component; 
{ 
    // The data for each row in the UIPickerView 
    return [pickerData objectAtIndex:row]; 
} 

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component 
{ 
    // whatever you want to happen when a row is selected. 

    // here I am assuming you want to remove from the picker and add to the table on selection 
    [tableData addObject:[pickerData objectAtIndex:row]]; 
    [pickerData removeObjectAtIndex:row]; 

    [tableView reloadData]; 
    [pickerView reloadAllComponents]; 
} 

MyViewController should have these methods.

好吧,你必須做的最後一件事是設置代表和初始化數據。在MyViewController- (void)viewDidLoad方法中添加以下行

tableView.delegate = self; 
tableView.dataSource = self; 
pickerView.delegate = self; 
pickerView.dataSource = self; 

tableData = [[NSMutableArray alloc] init]; // table starts empty 
pickerData = [[NSMutableArray alloc] initWithObjects:@"1", @"2", @"3", @"4", @"5", nil]; // picker starts with values 1, 2, 3, 4, 5 

[tableView reloadData]; 
[pickerView reloadAllComponents]; 

- (void)viewDidLoad should look like this.

有一些更多的委託方法,你可以在規範的類找到,但這些的應該現在就足夠了。

+0

您好null0pointer感謝這哥們將這項工作在一個標籤式應用程序或不是因爲我需要它的標籤:/ – 2012-02-18 17:12:54

+0

和所有的,所以我已經嘗試的代碼之後我失去了我很新的Xcode和似乎無法找到所有的東西,它只是混淆了我可以上傳的uipickerview等圖像,並告訴我哪個代碼是哪裏.h - .m等我真的很感激它。 – 2012-02-18 17:31:25

+0

所以我用幾張圖片更新了帖子,以幫助您找到東西。我還修復了我在代碼中遇到的一些錯誤。希望有所幫助。 – null0pointer 2012-02-19 16:20:20