I drag a PickerView and a button to the View in my xib file.
But, when I build my code, only my button shows in the simulator. It seems that the PickerView has been hidden because I have got the data from the PickerView.
What is the reason for this problem? How can I fix it?
You must implement at least 2 methods from the UIPickerViewDataSource and 1 from the UIPickerViewDelegate to have the picker view show up. The following 3 methods create a picker view that just displays the word "text" in 3 columns and 12 rows:
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
return #"Test";
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 3;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
return 12;
}
And do not forget to declare your UIViewController class as delegate of UIPickerViewDataSource and UIPickerViewDelegate:
#interface YourCustomClassViewController : UIViewController <UIPickerViewDataSource, UIPickerViewDelegate>
In YourCustomClassViewController.h file,
#interface YourCustomClassViewController : UIViewController <UIPickerViewDataSource, UIPickerViewDelegate>{
IBOutlet UIPickerView *pickerView;
}
In YourCustomClassViewController.m file, You can add following methods
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
return #"YourName";
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 2;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
return 5;
}
In your xib file(YourCustomClassViewController.xib), connect Picker to File's Owner. Like following way...
pickerView --->Picker
dataSource --->Picker
delegate ---->Picker
I hope it will be helpful to you surely. If you have any doubt ask to me.
Related
I've set up a UIPickerView successfully, but I'm wondering about the best way to handle the case when the data is initially empty. For my current iteration I use the following code for the pickerdelegate
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
if ([flash_cards count])
{
return [[flash_cards allKeys] objectAtIndex:row];
}
return #"";
}
where
flash_cards = NSMutableDictionary *
Whenever I try to scroll it creates an error of indexoutofbounds which is only logical. How do I handle the case of an empty array?
EDIT:
Also as part of the code I implemented the following
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponen: (NSInteger)component
{
return [flash_cards count];
}
#pragma mark Picker Data Source Methods
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
To solve this problem you should return 1 instead 0 in the method numberOfRowsInComponent from the UIPickerViewDataSource when the array is empty, like the example below:
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return [self.myArray count] == 0 ? 1 : [self.myArray count];
}
You should also implement the UIPickerViewDataSource methods:
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
You can still use flash_cards (or [flash_cards count]) to derive the return values of these methods; if your data is empty, you can return 0, then later call -reloadAllComponents on your picker view when you get data loaded.
I got a string as mild,medium,hot.I split the string with comma as separator.I need to print it in a pickerView too.
I used the following code and got List count as 3 successfully.
NSString *spList=[mdict objectForKey:#"spicinesstype"];
NSArray *list = [spList componentsSeparatedByString:#","];
NSLog(#"List count:%d",[list count]);
return [list count];
But how could I display all 3 items in pickerview
You should set your class as the delegate of the picker view, Then implement these 3 delegate methods for your picker
- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component {
return [array count];
}
- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
return [array objectAtIndex:row];
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView {
return 1;
}
First you need to implement UIPickerViewDelegate and UIPickerViewDataSource by putting them on the end of the #interface line of the header file of your view controller.
Like this: #interface MyViewController : UIViewController <UIPickerViewDelegate, UIPickerViewDataSource>
Next you need to set your view controller as the delegate and data source of you picker view. You can either do this in the - (void)viewDidLoad method of your view controller by adding the lines:
myPickerView.delegate = self;
myPickerView.dataSource = self;
Or you can link it up in Interface Builder if you are using that.
Then you need to implement these delegate methods in your View Controller source file.
(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;
{
return 1;
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
// whatever you want to happen when a row is selected.
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component;
{
return [list count];
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;
{
return [list objectAtIndex:row];
}
I made a simple picker that displays the information chose in a label on the same page. The picker is just showing ? ? ?, but when i scroll over the ? it shows the correct things chosen in the label. How do I get it to also show the words in the pickerwheel itself?
Without seeing your code, it is difficult to track down. How are you setting the information in the Label and the Picker?
If you have an array of strings which are also displayed in the Picker, then this is easy:
In the .h file, use:
#interface ViewController : UIViewController <UIPickerViewDataSource, UIPickerViewDelegate>{
NSArray * arrayOfString;
UILabel * theLabel;
UIPicker * thePicker;
}
In the .m file, use:
#pragma mark -
#pragma mark View
- (void)viewDidLoad;{
[super viewDidLoad];
arrayOfStrings = [[NSArray alloc] initWithObjects:#"1", #"2", #"etc", nil];
}
#pragma mark -
#pragma mark Picker Delegate Methods
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView; {
return [arrayOfStrings count];
}
- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component; {
return 1;
}
- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component; {
return [arrayOfStrings objectAtIndex:row];
}
- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component; {
theLabel.text = [arrayOfStrings objectAtIndex:row];
}
You then hook everything up in the nib, and this should fix your problem. Hope this helps!
I have a UIPickerView on a UIView. I've implemented its protocol in the .h and delegates in the .m files:
<UIPickerViewDataSource, UIPickerViewDelegate>
In IB, I've connected the above to the picker, which I also have an IBoutlet for. The methods look like this:
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView {
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component {
return [self.arr count];
}
- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
return #"test";
}
- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
//do something
}
Any ideas which piece I'm missing to get the picker working?
If the delegate methods are not getting called, then in IB double check that the picker view's delegate and datasource outlets are connected to File's Owner.
In the .h file, the picker view outlet should be declared like this:
#property (nonatomic, retain) IBOutlet UIPickerView *pickerView;
you have not assigned the delegate to the UIPickerView thats the reason none of the delegate methods are called.
UIPickerView *pickerView;
pickerView.delegate = self;//write this line in view did load method.
Hope this helps and let me know if this answers your question..:)
sometimes it's due to the pickerview not being connected to the file Owner's datasource and delegate in the nib
I have read other questions about creating a UIPickerView with two or more columns but cannot find the exact solution.
How is this done for the iPhone programmatically? How do you add static data? Thanks!
Make your controller (or whatever is controlling the behavior of the PickerView) support the UIPickerViewDelegate protocol. Then, implement:
- (int) numberOfColumnsInPickerView:(UIPickerView*)picker
to return the number of columns you want, and
- (int) pickerView:(UIPickerView*)picker numberOfRowsInColumn:(int)col
to return the number of rows for each column, and finally:
- (UIPickerTableCell*) pickerView:(UIPickerView*)picker tableCellForRow:(int)row inColumn:(int)col
to setup each cell.
See the reference for UIPickerView and UIPickerViewDelegate.
There is an excellent tutorial on this subject here.
Assuming you have a dictionary or a couple of arrays holding your static data. For simplicity I will go with a very simple array.
You have to modify your view controllers interface definition to tell the program that your view controller can provide data and delegation to a picker view.
#interface NVHomeViewController : UIViewController <UIPickerViewDelegate,UIPickerViewDataSource>
Than just implementing a couple of methods will make it work however documentation should be checked for other methods that are optional but provides more customization and control.
#interface NVHomeViewController : UIViewController <UIPickerViewDelegate,UIPickerViewDataSource>
NSArray *options;
- (void)viewDidLoad
{
[super viewDidLoad];
options = #[#"a",#"b",#"c",#"d"];
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
return [options count];
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
return [options objectAtIndex:row];
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
NSLog(#"%# selected.",[options objectAtIndex:row]);
}