我有Xcode 4.2,我正嘗試創建一個下拉菜單。我希望下拉菜單下拉單詞或數字。然後,當我從列表中選擇一些內容時,我希望它出現在不同頁面的表格中。我的問題是:我會如何寫這個? 謝謝UIPickerview下拉菜單
回答
首先通過File> New> New File並按照提示創建一個帶有nib文件的UIViewController子類。
現在打開筆尖(MyViewController.xib)。
你應該拖動一個UIPickerView和一個UITableView到視圖,並且按照自己喜歡安排他們(沒關係)。
下一頁打開您的UIViewController子類的頭文件(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;
然後就回到你的筆尖文件和的UITableView和UIPickerView連接到這些變量。
現在在你的源文件(MyViewController.m),你需要合成你引用。因此,僅低於@implementation MyViewController
現在添加@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
的- (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];
有一些更多的委託方法,你可以在規範的類找到,但這些的應該現在就足夠了。
您好null0pointer感謝這哥們將這項工作在一個標籤式應用程序或不是因爲我需要它的標籤:/ – 2012-02-18 17:12:54
和所有的,所以我已經嘗試的代碼之後我失去了我很新的Xcode和似乎無法找到所有的東西,它只是混淆了我可以上傳的uipickerview等圖像,並告訴我哪個代碼是哪裏.h - .m等我真的很感激它。 – 2012-02-18 17:31:25
所以我用幾張圖片更新了帖子,以幫助您找到東西。我還修復了我在代碼中遇到的一些錯誤。希望有所幫助。 – null0pointer 2012-02-19 16:20:20
- 1. 下拉菜單
- 2. 下拉菜單?
- 3. 下拉菜單
- 4. 下拉菜單
- 5. 下拉菜單
- 6. 下拉菜單拉到右
- 7. PHP下拉菜單
- 8. CSS - 下拉菜單
- 9. 下拉菜單3
- 10. PySide下拉菜單
- 11. CSS下拉菜單
- 12. Dhtmlx下拉菜單
- 13. DHTML下拉菜單
- 14. 帶下拉菜單
- 15. 下拉菜單popper.js
- 16. jquery下拉菜單
- 17. 下拉菜單javascript
- 18. Javascript下拉菜單
- 19. HTML下拉菜單
- 20. HTML下拉菜單
- 21. jquery下拉菜單
- 22. PHP下拉菜單
- 23. Bootstrap下拉菜單
- 24. CSS下拉菜單
- 25. Dreamweaver下拉菜單
- 26. CSS下拉菜單
- 27. silverlight下拉菜單
- 28. 動態UIPickerView菜單
- 29. 下拉菜單不下降
- 30. CSS - 單層下拉菜單
我會很感激 – 2012-02-18 15:10:53