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...)
Related
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.
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.
I have a tableView with the custom cell (see image below.)
Taking a three row table as an example, if a user changes segment controller in row 0 to "Yes," can I automatically change the segment controllers in rows 1 & 2 to "No?"
I am using the following to detect a segment change:
- (void)seg_changed:(id) sender {
cell=(switchCell*) [[sender superview] superview];
UITableView *table=(UITableView*) [cell superview];
NSIndexPath *path=[table indexPathForCell:cell];
NSLog(#"been pressed %d si %d",path.section, path.row);
}
Much appreciated.
In this method you call just tell those other segment controllers to set their values to "NO". The hard part is figuring out where those two other controls are. You have to do the hard work of tracking them.
If your design ensures there are always two more cell with segmented controls you can just access the correct cells by incrementing the path.row value.
This change can tell your data model that a value has changed, the model object then updates the associated values, and notifies the cells displaying those other values.
You can add an array to this cell class that keeps track of what other cells should be modified with this change.
Edit: (to respond to a comment) To change the setting displayed on the segmented control just set the property selectedSegmentIndex of the UISegmentedControl to the appropriate value. "Yes" should be 0, and "No" should be 1.
I am having a problem! When I use addSubview: and add a view to a UIScrollView, it adds a completely new view.
I want to either:
toggle between 3 predefined views, or
remove the last view from the UIScrollView before adding another one.
(All 3 views have some images so I'd imagine that the second option would be more efficient, memory-wise. However, perhaps the first option would be better on battery life? This is a 'subquestion'.)
How does one accomplish this?
You could accomplish your plan B by using removeFromSuperview function of your UIView.
Use it as below.
[mySecondImageView removeFromSuperview];
[myScrollView addSubview:myThirdImageView];
EDITED:
When you create UIImageView assign a tag value to each and also use and integer iVar to hold the tag value of your current view lets say it's currentViewTag ;
myFirstImageView.tag = 1;
mySecondImageView.tag = 2;
myThirdImageView.tag = 3;
currentViewTag = 1;
[myScrollView addSubview:myFirstImageView];
Now use as below
[[myScrollView viewWithTag:currentViewTag] removeFromSuperview];
[myScrollView addSubview:myThirdImageView];
currentViewTag = 3;
Sounds to me that you are trying to implement an infinite scrolling uiscrollview like the photos app.
If so, you don't need to remove any subviews and add them, you just need to create a UIScrollView that is as wide as three of your images and then when the scroll view is scrolled, change the image in either position 1 or 3 depending on which way the scroll is performed and then reset the position of the scroll view. If this is what you are trying to do, I can give more information if you need it.
I have my view that contains the UIPickerView...
I tried to make this code:
[self.picker selectRow:rowSelected inComponent:0 animated:true];
to select the row with index rowSelected, but this doesn't work...
I need to put this in viewdidload??
You do not call this method yourself. It is called by the system when a user selects a row in the pickerview. You should override the implementation of that method to do whatever you wish should happen when the user selects that row in that component.