2012-06-28 26 views
0

我剛剛加入本網站後,非常難以回答我的問題,並且你們在做什麼時看起來很神奇。我想爲iPhone構建一個簡單的介紹應用程序。我遇到的問題是我想使用UIPickerView來選擇各種選項,在pickerview中我有大約20個選項。接下來我想要做的是在單擊選項時能夠打開下一個窗口,我希望在pickerview中爲每個選項設置單獨的窗口。基本上我有一個品牌,那麼當你在pickerview上選擇品牌時選擇品牌,你就會轉向爲該品牌製造的正確配件。每個品牌都有獨立的配件,這就是爲什麼我需要每個品牌都有不同的新窗口。我已經有了我的pickerview,我只是不知道如何繼續下一步。一旦我通過點擊在選取器上選擇一個項目,我希望它移動到相應的下一個窗口。預先感謝您的幫助。如何將UIPickerView選項連接到單獨的頁面

回答

2

我會假設你的聲明時UIPickerView,你已經設置委託給當前視圖無論是在界面生成器或代碼即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: 
     //... 
    } 
} 

請注意,如果您聲明一個新的變量(如您的視圖控制器之一)的情況下語句中,你需要的情況下,在大括號{ }聲明爲了這樣做。

或者,如果您不希望事件在選中某行後自動觸發(例如,如果用戶意外地選擇了一行),您可以創建一個按鈕以供用戶按下以確認選擇/繼續。按下按鈕後,按鈕的目標方法可以根據選擇器中選定的行進行操作。您可以通過以下方法調用[myPicker selectedRowInComponent:0](假定您的選擇器只有一列/「組件」)來查找選定的行。

祝你好運!

編輯(讀你的後續問題後):

假設SingleComponentPickerViewController.h就是您創建UIPickerView的看法。

SingleComponentPickerViewController.h:

@interface SingleComponentPickerViewController : UIViewController <UIPickerViewDelegate, UIPickerViewDataSource> 

SingleComponentPickerViewController.m:

在您的viewDidLoad,(或你創建的pickerview),設置委託:

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

訪問abov中的myPickerView變量e方法,這將意味着您的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 
} 

如果使用界面生成器,那麼你應該返回類型更改從voidIBAction

SingleComponentAppDelegate.m

現在我假設你的應用程序委託的didFinishLaunchingWithOptions看起來像這樣接近尾聲:

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

你想修改窗口的RootViewController的屬性設置到一個導航控制器,其中包含您的自定義視圖控制器。這樣,您可以輕鬆地將視圖推送到屏幕上並將其彈出。

//... 
//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; 
+0

嗨eitan27,非常感謝你對你的反應,我很新的這一點,您的文章使我明白pickerview的底層好一點。我只是對我應該在哪裏添加這些東西感到困惑。我有這些類是:SingleComponentAppDelegate.m,SingleComponentAppDelegate.h,SingleComponentPickerViewController.h,SingleComponentPickerViewController.m,我創建了一個新的觀點,如果我按一個叫AnotherView.h和AnotherView.m按鈕打開。 – ReYCangri

+0

你寫的東西正是我想要做的,我有我的PickerView中的組件,當我選擇一個按下按鈕時,它會打開一個新頁面,如AnotherView,AnotherView1 ... to..AnotherView20等。這些視圖中的每一個都是爲我的PickerView中的每個組件製作的。下面的代碼是我想要按下的按鈕的,但我不知道如何將它鏈接到選定的行,該行將是該頁面的第0行。我試着添加你給我的代碼,但是它說navigatorController沒有定義。非常感謝你的幫助。 – ReYCangri

+1

非常感謝Eitan,我會按照你的指示,盡我所能讓它工作。我只是想學習Objective-C,雖然我在C++方面有一些經驗,但這與我在編程課程中所做的有點不同。自從我參加這些課程已經有幾年了。我感謝你的幫助。謝謝。 – ReYCangri

相關問題