How to place the morethan one pickerviews in view? - iphone

Hai,In my app i want to place the 4 pickerviews.And i place the one segment control with 3 segments.If we click on first segment then firstpicker will be display.Likewise remaining pickers are display for remaining segments.But,only first picker will be displayed and remaining pickers are not show components and rows.Only display black screen.Please provide any help.Please urgent.

To again call the delegates, call the following line
[pickerview reloadComponent];

create outlet and make use of hidden property...means if you tap on first segment make first picker as highlighted and similarly for other segments with respective pickers...
do you want to resize ??
Edit: added code from below for formatting purposes
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return ( pickerView == picker1 ? 2 : 3 );
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component { NSArray *values = ( pickerView == picker1 ? values1 : values2 );
return [values count];
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
NSArray *values = ( pickerView == picker1 ? values1 : values2 );
return [values objectAtIndex: row];
}
check like this..

Related

How to change the picker rows based on another picker's selection?

I have two pickers - country and region. I need to populate the region picker based on the row selected in the country picker. I have arrays to populate both the pickers. I need some help to change the rows of region picker based on the country picker's selection. Any help would be greatly appreciated. Thanks a lot in advance.
Ok You have two pickers lets say countryPicker and regionPicker.
in the delegate method for UIPickerView add one condition
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
NSString *pickerTitle = #"";
if (pickerView == countryPicker)
{
pickerTitle = [countryFeeds objectAtindex:row];
//assigns the country title if pickerView is countryPicker
}
else if (pickerView == regionPicker)
{
pickerTitle = [regionFeeds objectAtindex:row];
//assigns the region title if pickerView is regionPicker
}
return pickerTitle;
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
if (pickerView == countryPicker)
{
//selects the region corresponding to the selected country.
//totalRegions is a NSDictionary having country names as keys and array of their regions as values
regionFeeds = [totalRegions objectForKey:[countryFeeds objectAtindex:row]];
//Now reloading the regionPicker with new values.
[regionPicker reloadAllComponents];
}
else if (pickerView == regionPicker)
{
// your code to select a region
}
}
I hope this solves your problem :)
BR, Hari
Are they separate picker views, or one picker view with two columns?
Either way, when you change the value in one picker, just call reloadAllComponents on the other picker (or reloadComponent: if you are using a split picker) and then when it reloads the data from it's data source you can use the value from the first picker to supply the correct region list.
My suggestion is please maintain the country and region in one picker with two components(country,region).If u do like this then we can change the one component values based on another component value.
NSUInteger selectedRow= [CountryPicker selectedRowInComponent:0];
NSString *userchoice =[self pickerView:CountryPicker titleForRow:selectedRow forComponent:0];
This will give you the title for the selected row by the user in country picker
then using the arrays you have to populate names of regions
You can use
- (void)selectRow:(NSInteger)row inComponent:(NSInteger)component animated:(BOOL)animated
on the RegionPicker

instantiate UIPicker with uitextfield string

I am trying to pass my UItextfield value over to a UIPicker that I have loading in a alertview I'm just not sure what functions are available to me to get the values back into the picker?
At a guess I should be doing something in my
- (NSInteger)pickerView:(UIPickerView*)pv numberOfRowsInComponent:(NSInteger)component
{
if (pv == pickerViewA) {
return 10;
}
else
return 16;
}
but looking at the developers notes their is not much info about the UIPickerViewDataSource other than returning number of rows...
any help would be greatly appreciated.
There are other data source and delegates as-
//for title of a row in a component
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
//for width of a componenet
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
//for row height of a component
-(CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
//for number of components
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
for more info look at documentation -
http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UIPickerViewDelegate_Protocol/Reference/UIPickerViewDelegate.html
and
http://developer.apple.com/library/ios/#documentation/uikit/reference/UIPickerView_Class/Reference/UIPickerView.html
You can use either one of the following two delegates to add data content to UIPickerView
– pickerView:titleForRow:forComponent:
– pickerView:viewForRow:forComponent:reusingView:
For the first one u can return any NSString object and you cannot customize anything like font size or color for that.
But if you use the second one you can add the data content as a view to UIPickerView which can be achieved using a UILabel and here you can customize the view as you require.
Hope this might help you
Happy coding !

Fill UIPickerView with an Array Value

i am working on a project in which i have to perform following two work:
1.) Fetch value from CoreData and store it in an NSMutableArray.
2.) Take a UIPickerView and fill it with an Array value.
Problem is that size of array is dynamic and i canto fill an array value in UIPickerView. can someone help me.
in order for the UIPickerView to work correctly, you must supply the amount of components (columns) and the number of rows for each component whenever it reloads:
as mentioned, use [myPickerView reloadAllComponents]; to reload the view once the array is populated, but you MUST implement also these things after you declare the containing view controller class as <UIPickerViewDelegate> link the picker to the file owner as a delegate, and then:
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
return 1;// or the number of vertical "columns" the picker will show...
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
if (myLoadedArray!=nil) {
return [myLoadedArray count];//this will tell the picker how many rows it has - in this case, the size of your loaded array...
}
return 0;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
//you can also write code here to descide what data to return depending on the component ("column")
if (myLoadedArray!=nil) {
return [myLoadedArray objectAtIndex:row];//assuming the array contains strings..
}
return #"";//or nil, depending how protective you are
}
After updating the value (Inserting or modifying existing) in your array.
Call reloadAllComponents on your UIPickerView instance.
- (void)reloadAllComponents
Use as below
[myPickerView reloadAllComponents];

Expanding and retracting dropdown list in iPhone app

I'm starting off developing an iPhone app. I need to present a drop down box for the customer to pick a value which will essentially be sent downstream to a database upon submit. I'm clearly using a UIPickerView for the drop down list, built from IB.
Once my view loads, the Drop down list is enabled, with all values showing.
Question is:
Can I only expand the drop down list once it's clicked?
Can I retract the list once the user selects a value?
I'm thinking very web-centric in terms of drop-downs, but I could have this all wrong.
Your view controller needs to implement the UIPickerViewDataSource as well as the UIPickerViewDelegate. In the XIB of the UIPickerView you need to link the File's Owner with the UIPickerView's delegate and datasource.
Like in the UITabelDataSource you need to provide the number of components (table: sections) inside [numberOfComponentsInPickerView]. Then you need to provide the number of rows for each component in [numberOfRowsInComponent]. Finally you need to provide the title for the each entry inside [titleForRow].
Now to bring everything to life you can use [didSelectRow] to load the next component if the previous one was selected.
Attention: UIPickerView has a little bug not to call [didSelectRow] when the component is filled / changed. In order to make it work more smoothly I added a "None" entry as the first entry which is a non-selection and does not cause the next component to be loaded.
This is a rudimentary code:
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 3; // Number of Components aka Columns
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
if( component == 0 ) {
return [self.firstColumnEntries count] + 1;
} else if( component == 1 ) {
return [self.secondColumnEntries count] + 1;
} else if( component == 2 ) {
return [self.thirdColumnEntries count] + 1;
}
return 0;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
NSLog( #"titleForRow(), component: %d, row: %d", component, row );
NSDictionary *item = nil;
if( row == 0 ) {
return #" --- ";
} else {
int correctedRow = row - 1;
if( component == 0 ) {
item = [self.firstColumnEntries objectAtIndex:correctedRow];
} else if( component == 1 ) {
item = [self.secondColumnEntries objectAtIndex:correctedRow];
} else if( component == 2 ) {
item = [self.thirdColumnEntries objectAtIndex:correctedRow];
}
return [item objectForKey:#"name"]; // My objects are NSDictionarys
}
}
- (void) pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
DLog( #"SSVC.didSelectRow(), component: %d, row: %d", component, row );
ActionRequest *action = nil;
if( row == 0 ) {
if( component == 0 ) {
[self refreshFirstColumn:self.firstColumnEntries];
} else if( component == 1 ) {
...
}
} else {
// Last Column is selected. Now selection is complete
...
}
}
Because the UIPickerView takes away a lot of space and you cannot change it size I would recommend to place it on an additional UIView (make the rest transparent) and display it when the user selected the field that is assigned to the value (UITextField). When the selection is done (either when last value is selected or when the user enter a button) you let the View disappear and set the value onto the UITextField. Make sure that the UITextField cannot be edited and that entering it make the View with the Picker View appear. When done make sure that you also move to the next field.
You have a couple options:
Probably easiest for a beginner: push a new view (either as a modal view or onto the nav stack) that presents the list either as a table view where they click the item they want or as a pickerView where it scrolls.
Review the twitter app 'my profile' view. While I haven't done this personally, I think it's simply using the didSelectRowAtIndex to determine which section the user clicked and then filling an array of values for that section before calling [tableView reloadData]

UIPickerView problems

I have a very simple application that use a 2 component UIPickerView that causes me a crash every time I click over it. I dragged it into my view by IB, then hooked up dataSource and delegate to File's Owner. In the .h file:
#interface SettingsViewController : UIViewController <UIPickerViewDataSource, UIPickerViewDelegate> {
While in .m
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView {
return 2;
}
- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component {
NSInteger value;
if (component == 0) {
value = [tipiDado count];
} else {
value = [numeroDadi count];
}
return value;
}
- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
if (component == 0) {
return [tipiDado objectAtIndex:row];
} else {
return [numeroDadi objectAtIndex:row];
}
}
- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
NSLog(#"Selected Dice: %#. Number of Dice: %#", [tipiDado objectAtIndex:row], [numeroDadi objectAtIndex:row]);
}
I dunno why it continues to give me SIGBART or EXC_BAD_ACCESS... I don't know where I'm doing wrong.
Suggestions?
Thanks folks.
It's difficult to answer properly without seeing some more code. When it crashes, you should be able to see exactly what line is causing the crash (look at the call stack on the left). My guess is that either one of your arrays (tipiDado or numeroDadi) are not retained properly or else that the objects stored in them are not of type NSString.
If you update the question with the code you use to initialize them, it will be easier to point out the exact problem.
It's probably because your arrays (tipiDado and numeroDadi) are no longer valid. Maybe they are set up as autoreleased objects?
What you can do, is start in debug mode (debug configuration and with debugger attached) and it should stop right at the line where it crashes.
Try using Allocations with zombo detections / reference counter to see which object is the problem.
In didSelectRow, you are using the same row value to access both arrays. If the arrays are not the same size, you could be accessing an out-of-range item.
You should either check one array only based on the component parameter or to show both selections you can do this instead:
NSInteger tipiDadoRow = [thePickerView selectedRowInComponent:0];
NSInteger numeroDadiRow = [thePickerView selectedRowInComponent:1];
NSLog(#"Selected Dice: %#. Number of Dice: %#",
[tipiDado objectAtIndex:tipiDadoRow], [numeroDadi objectAtIndex:numeroDadiRow]);
I used property/synthesize to initialize them, so when I filled them up with [NSArray arrayWithObjects:...] I haven't added retain but I forget to use self. notation!!
Writing down:
self.tipiDado = [NSArray arrayWithObjects:#"D4",...];
fixed up he problem.