How select value initally when picker view appear? - iphone

I have set a picker view appearance on text field begin editing function and hide keyboard. Now problem is that when picker view appear then it show selection indicator on first value but that value not show in text field. I want that when user click on text filed then picker view appear and value from picker will show in text field where selection indicator place. How do that? Suppose i have four value in same order 0,1,2,3. Now when picker view appear then selection indicator is at on '0' value. But to get that value we have to scroll picker view and come back to '0' then it show that value in text field. Why it is occurs? And how get value automatically?
Thanks in advances...

Well, there is no connection between your picker view and the text field - you have to make it yourself.
There are two things to consider:
1) Your text field already contains a value, let's say "1". When you open the picker, you can set it to this value already with the function
[yourPicker selectRow:1 inComponent:0 animated:false]
, assuming your data array for the picker is [0, 1, 2, 3]. In your case, to make the text field contain the value 0, just call
[textField setText:#"0"]
2) When you are finished with the picker, it calls the delegate function
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
So you have to make your view implement the UIPickerViewDelegate protocol, implement the above delegate function in it and set the view as the picker view's delegate.
In the delegate function you can get the value the user picked (using row to access your data array values) and set this value in your textfield.

Related

Data Pass to UIPickerView inside of UITableViewCell

I have UITableView and there is a picker view inside every cell. User can change value of every picker separately.
I need to know that which cell's pickerView is changed. For example, when user move a picker I will know that, this is cell 3.
Is there any way to do that? It's not necessary to get instant changed values. We can store it on array than send when Go button clicked that is not related with tableview.
I would suggest to store an array of pointers to your picker views. So index 0 of your array would point to the picker view of table row 0, etcetera. Then, make sure your view controller is the delegate of all picker views. Implement the pickerView:didSelectRow:inComponent: method declared by the UIPickerViewDelegate protocol. That method has an instance of UIPickerView as parameter: that's the one which value was changed. To obtain the row of that picker view, you can send your array the indexOfObject: method.
Create a subclass of a UITableViewCell. That subclass should get a reference to whatever you want to update, and then it can simply create (or IBOutlet link) to the picker and be a delegate of the picker.
So Cell should have:
- (void) passStuff:(NSMutableDictionary *)datamodel key:(NSString *)key values:(NSArray *)values {
_privateWeakRefDataModel = datamodel;
_privateCopyKey = key;
_privateValues = values;
[picker reloadData];
}
#pragma mark - Picker Delegate Code here
This way the cells will handle updating your data model directly. Keeps your code much cleaner.

How to display the textfield value in picker selected row in iPhone?

I have an application where I have a pickerview. In that picker I am displaying values from 1 to 100. When I select a value in picker say 2 that value is displayed in a textfield. I want that if my textfield contains value say 5 that value particular value should get displayed in my picker.
Now problem is when in textfield 5 is displayed and I go to pickerview it shows by default every time 1 selected irrespective of the value in the texfield. I want the value which is present in the textfield should get displayed on the pickerview also.
Use below code it will help you
[picker selectRow:[yourTxtObj.text intValue]-1 inComponent:0 animated:YES];
[yourPickerViewName selectRow:[textfield.text intValue] inComponent:yourComponent animated:YES]
This will work. It is used for select a row in picker.

iOS 5 Picker View update based on selection?

I am working on a project where I want to have a picker view with 3 columns.
In the first column I want the user to select a letter.
Once the letter is selected I want the second column to update with a list of last names that start with that letter. When a last name is selected I want the third column to update with a list of first names who have the selected last name.
Can this even be done? Help!?
Get the letter that they selected using this pickerview delegate method:
- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
You could load the next picker at this point but it would be better to populate the next picker when the done button on each picker is selected:
This is a nice SO post that describes how to use picker views very well: Dismissing UIPickerView with Done button on UIToolBar

How do you pass data from one text field to another when using Storyboards in iOS5?

I am using storyboards for the 1st time, and I cant figure out what I am doing wrong here ... I have a button that transitions from one view controller to another using StoryBoards (the 2nd view is presented modally).
I am trying to use the "prepare for segue" in order to pass the value of a text field from view 1 to view 2, but it is not working. Can somebody tell me what I have wrong here ... ?
View 1:
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if ([[segue identifier] isEqualToString:#"saveGame"]) {
statsViewController *svc = (statsViewController *)[segue destinationViewController];
[svc setStatsTextField:gameTextField];
}
}
If there is other code I can post to clarify please let me know.
(for the record there are no errors, the text field on view 2 just doesn't update.)
You cannot just assign a text field in one view controller to a property in another one. This achieves nothing for the text field that is actually in the second view controller's view. Instead, you have to assign a value to the text field's text property. (And ideally, this value should not come directly from another text field because you shouldn't use views to store your app's data. Whenever a text field updates, you should store the updated value in a variable in your view controller or model.)
Also, the statsTextField does not yet exist at the time this code is executed because the destination view controller's view is not yet loaded. You should declare a separate string property in statsViewController (class names should begin with a capital letter btw) and then assign the text field's value in viewDidLoad.
The text field is probably nil at that point since the view hasn't been loaded. You can force this (no problem doing so since its about to happen anyway!) by wrapping your code in an if statement:
if (svc.view)
svc.textfield.text = #"Hello";
Accessing the view property forces the view controller to load the view, if it is not already present.
I notice you seem to be passing a whole textfield object instead of a string to the text property - that doesn't seem like a good idea. It should be more like my example above.

iphone UIPicker default position

Is there a way to set a default position for a UIPicker? I mean, if the user has the option of selecting any number from 1 to 100 in the UIPicker, can I set it to automatically start at 50?
You can use selectRow:inComponent:animated: like this:
[somePickerView selectRow:49 inComponent:0 animated:NO]; // Zero-indexed, so row 49 is the row that says 50
You can fire it in the viewDidLoad method for the UIViewController managing the view which contains the UIPickerView.
(It could also be that you can specify the default value in Interface Builder, but I'm not sure and I didn't check...)