我剛剛加入本網站後,非常難以回答我的問題,並且你們在做什麼時看起來很神奇。我想爲iPhone構建一個簡單的介紹應用程序。我遇到的問題是我想使用UIPickerView來選擇各種選項,在pickerview中我有大約20個選項。接下來我想要做的是在單擊選項時能夠打開下一個窗口,我希望在pickerview中爲每個選項設置單獨的窗口。基本上我有一個品牌,那麼當你在pickerview上選擇品牌時選擇品牌,你就會轉向爲該品牌製造的正確配件。每個品牌都有獨立的配件,這就是爲什麼我需要每個品牌都有不同的新窗口。我已經有了我的pickerview,我只是不知道如何繼續下一步。一旦我通過點擊在選取器上選擇一個項目,我希望它移動到相應的下一個窗口。預先感謝您的幫助。如何將UIPickerView選項連接到單獨的頁面
回答
我會假設你的聲明時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
}
如果使用界面生成器,那麼你應該返回類型更改從void
到IBAction
。
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;
從UIPickerViewDelegate
Protocol Reference:
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
- 1. UIPickerView單獨頁面
- 2. 連接到單獨的頁面的Joomla快訊項目
- 3. jQuery選項卡vs單獨的頁面
- 4. 如何將HTML頁面連接到MySQL
- 5. 如何將Facebook頁面連接到Reddit頁面?
- 6. 如何將兩個單獨的頁面加載到兩個單獨的iframe?
- 7. 如何將選項添加到woo commerce編輯訂單頁面?
- 8. Qt:如何在單獨的線程中將QScriptEngineDebugger連接到QScriptEngine?
- 9. 如何將選項卡添加到dotProject的項目頁面?
- 10. 重定向到表單提交後單獨的連續頁面
- 11. 將SAP連接到.NET的選項
- 12. 如何檢查連接到php頁面
- 13. UIPickerView連接
- 14. 如何將我的Facebook頁面連接到我的wbsite/domain?
- 15. 基於datalist選項連接到其他頁面
- 16. 如何連接頁面?
- 17. 如何將面板手風琴項目與面板選項卡連接
- 18. 如何將樣式連接到Laravel的頁面?
- 19. 將數據庫連接到Drupal頁面
- 20. JQuery UI選項卡:如何直接導航到其他頁面的選項卡?
- 21. 如何將我的listview示例項目連接到下一頁
- 22. UIPickerView選擇鏈接到UITableView
- 23. VB - 如何將check_change連接到代碼後面的複選框
- 24. idTabs(將頁面鏈接到特定的選項卡)
- 25. 將單獨文檔中的頁眉預先添加到頁面
- 26. 將選項連接到按鈕
- 27. 將對象連接到javafx選項卡
- 28. 將UITableView單元連接到單獨的視圖控制器
- 29. 如何使用JSP直接寫入單獨的HTML頁面?
- 30. 如何將MainWindow.xib連接到空項目
嗨eitan27,非常感謝你對你的反應,我很新的這一點,您的文章使我明白pickerview的底層好一點。我只是對我應該在哪裏添加這些東西感到困惑。我有這些類是:SingleComponentAppDelegate.m,SingleComponentAppDelegate.h,SingleComponentPickerViewController.h,SingleComponentPickerViewController.m,我創建了一個新的觀點,如果我按一個叫AnotherView.h和AnotherView.m按鈕打開。 – ReYCangri
你寫的東西正是我想要做的,我有我的PickerView中的組件,當我選擇一個按下按鈕時,它會打開一個新頁面,如AnotherView,AnotherView1 ... to..AnotherView20等。這些視圖中的每一個都是爲我的PickerView中的每個組件製作的。下面的代碼是我想要按下的按鈕的,但我不知道如何將它鏈接到選定的行,該行將是該頁面的第0行。我試着添加你給我的代碼,但是它說navigatorController沒有定義。非常感謝你的幫助。 – ReYCangri
非常感謝Eitan,我會按照你的指示,盡我所能讓它工作。我只是想學習Objective-C,雖然我在C++方面有一些經驗,但這與我在編程課程中所做的有點不同。自從我參加這些課程已經有幾年了。我感謝你的幫助。謝謝。 – ReYCangri