I just joined this site after looking very hard for an answer to my question and you guys seem to be amazing at what you do. I want to build a simple intro app for the iPhone. The problem I ran into is that I want to use a UIPickerView for choosing a variety of options, I have about 20 options in the pickerview. Next thing I want to do is to be able to open the next window when you click an option, I want to have individual windows for each of my options in the pickerview. Essentially I have a brand, then when you pick the brand by selecting it on the pickerview, you move on to the right accessories made for that brand. There are individual accessories made for each brand which is why I need a different new window for each. I already have my pickerview in place I just dont know how to proceed to the next step. Once I select an item on the pickerview by tapping, I want it to move to the corresponding next window. Thank you in advance for your help.
Share
I’m going to assume that when declaring your UIPickerView, you’ve set the delegate to current view either in Interface Builder or done so in the code i.e.
myPicker.delegate = self;. Also in your .h file, you have set up the controller such that it is analogous tomyViewController: UIViewController<UIPickerViewDelegate>.You’ll implement the following method and use the
rowargument to decide which view to show next. This method gets fired after a row is selected (and was wired to do so the moment you specified which class is the delegate of the picker view). Assuming you’re using a navigationController, it could look something like:Note that if you declare a new variable (such as one of your view controllers) inside a case statement, you need braces
{ }inside the case statement in order to do so.Alternatively, if you don’t want the event to fire automatically after a row is selected (for example, in case of the user accidentally selecting a row), you can create a button that the user presses to confirm the selection/proceed. Upon pressing the button, the button’s target method could act according to the selected row in the picker. You can find the selected row with the following method call
[myPicker selectedRowInComponent:0](assuming your picker only has one column/”component”).Good luck!
Edits (after reading your follow-up questions):
Assuming that SingleComponentPickerViewController.h is the view where you created your UIPickerView.
SingleComponentPickerViewController.h:
SingleComponentPickerViewController.m:
In your viewDidLoad, (or wherever you created the pickerview), set the delegate:
To access your myPickerView variable in the above method, it will mean that your UIPickerView object has class scope or is a property.
Also, have the action that is fired after the button press in this file.
If you are using Interface Builder, then you should change the return type from
voidtoIBAction.SingleComponentAppDelegate.m
Right now I’m assuming your App Delegate’s
didFinishLaunchingWithOptionslooks something like this near the end:You want to modify it the window’s rootViewController property to be set to a navigation controller, with your custom view controller inside of it. This way, you can easily push and pop views onto the screen.