Xcode 4.2があり、ドロップダウンメニューを作成しようとしています。私は単語や数字でドロップダウンメニューをドロップダウンメニューにしたいと思います。リストから何かを選択すると、別のページの表に表示されます。私の質問は:私はこれをどのように書きますか? ありがとうUIPickerviewドロップダウンメニューの場合
答えて
最初に、ファイル>新規>新規ファイルに進み、プロンプトに続いてnibファイルでUIViewControllerサブクラスを作成します。
は今ペン先(MyViewController.xib)を開きます。
あなたは、ビューにUIPickerViewとのUITableViewをドラッグして、しかし、あなたは(それは問題ではない)のようにそれらを配置する必要があります。
次あなたのUIViewControllerサブクラスのヘッダファイル(MyViewController.h)を開きます。
@interface MyViewController : UIViewController {
行の終わりに
<UITableViewDelegate, UITableViewDataSource, UIPickerViewDelegate, UIPickerViewDataSource>
を追加すると、それはまた、あなたのテーブルビューとピッカーとへの参照を追加する必要があります。この
@interface MyViewController : UIViewController <UITableViewDelegate, UITableViewDataSource, UIPickerViewDelegate, UIPickerViewDataSource>
次のようになります。 2つの値の配列。ただ、@end
の上に続いて、次の行
@property (strong, nonatomic) IBOutlet UITableView* tableView;
@property (strong, nonatomic) IBOutlet UIPickerView* pickerView;
@property (strong, nonatomic) NSMutableArray* tableData;
@property (strong, nonatomic) NSMutableArray* pickerData;
を追加するだけで戻ってあなたのnibファイルに移動して、これらの変数にのUITableViewとUIPickerViewを接続します。
今すぐあなたのソースファイル(MyViewController.m)にあなたの参照を合成する必要があります。だから、デリゲートメソッド、それらのかなりの数があるでしょうを追加するだけで今@implementation MyViewController
の下のファイルに@synthesize tableView, pickerView, tableData, pickerData
を追加し、彼らはかなり自明です。UITableViewため
デリゲート方法はUIPickerViewため
- (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;
デリゲートメソッドが
- (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];
}
[OK]を、あなたがしなければならない最後の事は、デリゲートを設定し、データを初期化されます。 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];
あなたはクラスの仕様に見つけることができますが、これらのものは今のところ十分なはずですさらにいくつかのデリゲートメソッドがあります
を追加します。こんにちは、こんにちは、こんにちは、このタブディーのアプリケーションでこの仕事をするかどうかは、タブ付き:/ –
とすべて私はコードと私の失われた私は非常にだXcodeの新機能で、どこにあるのか分からないようで、uipickerviewなどの画像をアップロードして、どのコードがどこにあるのか教えてください。本当に感謝しています。 –
だから私は物事を見つけるのに役立ついくつかの画像で投稿を更新しました。私はコード内にあったいくつかのバグを修正しました。希望が役立ちます。 – null0pointer
- 1. ページローロード時にドロップダウンメニューを開いた場合
- 2. ドロップダウンメニューのタイトルをドロップダウンメニューに合わせる
- 3. ドロップダウンメニューを使用した場合のjQueryの表示値
- 4. UIPickerViewの幅をビューに合わせる
- 5. UIPickerViewに日付が設定されている場合のローカル通知?
- 6. jqueryダイアログが開いている場合、ドロップダウンメニュー
- 7. image_tagがclass = "center-block"の場合、ブートストラップのドロップダウンメニューのアライメントが失敗する
- 8. ドロップダウンメニューは、親がアクティブな場合にのみ利用可能です
- 9. JavaScriptのドロップダウンメニューとフラッシュの競合
- 10. 複数のUIPickerView
- 11. iphoneのUIPickerview
- 12. UIPickerviewの使用
- 13. UIPickerViewのselectRow:inComponent:animated:call pickerView:didSelectRow:inComponent:?
- 14. UIPickerViewでのトラブル
- 15. iphoneのUIPickerview
- 16. UIPickerviewのカスタマイズ
- 17. iphoneのUIPickerView
- 18. UIPickerViewのカスタムルックアンドフィール。
- 19. UIPickerView上のUIButton
- 20. UIPIckerViewの問題
- 21. UIPickerViewのカスタムレンダラー
- 22. UIPickerViewの編集
- 23. UIPickerViewゴーストイメージ
- 24. UIPickerViewアニメーショングリッチ
- 25. UIPickerViewジッタ
- 26. リロードUIPickerView
- 27. UIPickerView&ActionSheet
- 28. UIPickerViewサウンド
- 29. 円UIPickerView
- 30. UIPIckerViewが希望の場所に表示されません
私は本当に感謝します –