0

私は自分の質問への答えを一生懸命探した後、このサイトに参加しました。iPhone 用のシンプルなイントロ アプリを作成したいと考えています。私が遭遇した問題は、さまざまなオプションを選択するために UIPickerView を使用したいということです。ピッカービューには約 20 のオプションがあります。次にやりたいことは、オプションをクリックしたときに次のウィンドウを開くことができるようにすることです。ピッカービューのオプションごとに個別のウィンドウを作成したいと考えています。基本的に私はブランドを持っています。次に、ピッカービューでブランドを選択して選択すると、そのブランド用に作られた適切なアクセサリーに移動します. ブランドごとに個別のアクセサリーが作られているため、それぞれに別の新しいウィンドウが必要です。次のステップに進む方法がわかりません。ピッカービューでタップしてアイテムを選択したら、対応する次のウィンドウに移動したい。よろしくお願いいたします。

4

2 に答える 2

2

UIPickerViewを宣言するときに、Interface Builderで、またはコードでデリゲートを現在のビューに設定したと仮定しますmyPicker.delegate = self;。また、.hファイルでは、に類似するようにコントローラーを設定していますmyViewController: UIViewController<UIPickerViewDelegate>

次のメソッドを実装し、row引数を使用して次に表示するビューを決定します。このメソッドは、行が選択された後に起動されます(そして、どのクラスがピッカービューのデリゲートであるかを指定した瞬間に起動されます)。NavigationControllerを使用しているとすると、次のようになります。

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    //For example, you can do a switch based on the row
    switch (row) {
         case 0:
             [self.navigationController pushViewController:myFirstView animated:YES];
             break;
         case 1:
             [self.navigationController pushViewController:mySecondView  animated:YES];
             break;
         // ..
         // ..
         case 19:
             break;
         default:
        //...
    }
}

caseステートメント内で新しい変数(ビューコントローラーの1つなど)を宣言する場合、{ }そのためにはcaseステートメント内に中括弧が必要であることに注意してください。

または、行が選択された後にイベントが自動的に発生しないようにする場合(たとえば、ユーザーが誤って行を選択した場合)、ユーザーが選択/続行を確認するために押すボタンを作成できます。ボタンを押すと、ボタンのターゲットメソッドはピッカーで選択された行に従って動作する可能性があります。選択した行は、次のメソッド呼び出しで見つけることができます[myPicker selectedRowInComponent:0](ピッカーに1つの列/「コンポーネント」しかない場合)。

幸運を!

編集(フォローアップの質問を読んだ後):

SingleComponentPickerViewController.hが、UIPickerViewを作成したビューであると想定します。

SingleComponentPickerViewController.h:

@interface SingleComponentPickerViewController : UIViewController <UIPickerViewDelegate, UIPickerViewDataSource>

SingleComponentPickerViewController.m:

viewDidLoad(またはピッカービューを作成した場所)で、デリゲートを設定します。

-(void) viewDidLoad {
    //...
    myPickerView.delegate = self;
    //...
    [super viewDidLoad];
}

上記のメソッドでmyPickerView変数にアクセスするには、UIPickerViewオブジェクトがクラススコープを持っているか、プロパティであることを意味します。

また、このファイルでボタンを押した後に実行されるアクションがあります。

-(void)onButtonPress:(id)sender {
    //This gets the currently selected row from the picker
    //Again, assuming you have your picker as a property or class variable
    int row = [myPickerView selectedRowInComponent:0];

    //In your final version you need to use a condition to decide which view to show.
    if (row == 0) {
        AnotherView *anotherViewInstance = [[AnotherView alloc] init];

        //Show "anotherView"
        [self.navigationController pushViewController:anotherViewInstance animated:YES];
    }
    else if (row == 1) {
        AnotherView2 *anotherView2 = [[AnotherView2 alloc] init];

        //Show "anotherView"
        [self.navigationController pushViewController:anotherView2 animated:YES];
    }
    else if (row == 2) {
        AnotherView3 *anotherView3 = [[AnotherView3 alloc] init];

        //Show "anotherView"
        [self.navigationController pushViewController:anotherView3 animated:YES];
    }
    //...continue with the rest of your conditions here
}

Interface Builderを使用している場合は、リターンタイプをからvoidに変更する必要がありますIBAction

SingleComponentAppDelegate.m

今のところ、あなたのAppDelegatedidFinishLaunchingWithOptionsは終わり近くに次のようになっていると思います。

//....
self.viewController = [[SingleComponentPickerViewController alloc] initWithNibName...];
self.window.rootViewController = self.viewController;     
[self.window makeKeyAndVisible];
return YES;

ウィンドウのrootViewControllerプロパティを変更して、カスタムViewControllerを内部に持つナビゲーションコントローラーに設定します。このようにして、ビューを画面に簡単にプッシュおよびポップできます。

//...
//Create your base view, similar (or even identically) to what was done earlier
SingleComponentPickerViewController *myView = [[SingleComponentPickerViewController alloc] initWithNibName...]; 

//Stick it inside a UINavigationController so you can push and pop views on top of it.
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:myView];

//Assign that to the window's rootViewController, show it, and return
self.window.rootViewController = nav;     
[self.window makeKeyAndVisible];
return YES;
于 2012-06-28T15:35:30.047 に答える
0

UIPickerViewDelegateプロトコルリファレンスから:

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
于 2012-06-28T15:06:03.743 に答える