UIPickerView not displaying - iphone

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

Related

The PickView is not visible when I run iPhone simulator

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.

UIPickerView crashes app when scrolling past beginning or end of list

I'm new to iOS dev, so this is probably easy to fix. I have a custom view controller in which I'm adopting the protocols to control a UIPickerView in a nib. Everything works fine unless, in the iPad simulator, I scroll the picker beyond the first item in the list or the last item in the list and release. It kicks the following error:
Thread 1: Program received signal: "EXC_BAD_ACCESS"
on this line of my main.m class:
int retVal = UIApplicationMain(argc, argv, nil, nil);
Relevant code follows:
ViewController.h
#interface BirdColorViewController : UIViewController <UIPickerViewDelegate, UIPickerViewDataSource> {
IBOutlet UIPickerView *birdColorPicker;
NSArray *birdColors;
}
#property (nonatomic,retain) IBOutlet UIPickerView *birdColorPicker;
Viewcontroller.m
- (void)dealloc
{
[birdColorPicker release];
[super dealloc];
}
...
- (void)viewDidLoad
{
[super viewDidLoad];
birdColors = [NSArray arrayWithObjects:#"Blue",#"Yellow",#"Red",nil];
birdColorPicker.delegate = self;
birdColorPicker.dataSource = self;
}
...
#pragma mark - UIPickerViewDataSource methods
//(UIPickerView *)thePickerView
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return [birdColors count];
}
#pragma mark - UIPickerViewDelegate methods
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return [birdColors objectAtIndex:row];
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
// Set value in prefs/model
}
Try:
birdColors = [[NSArray alloc] initWithObjects:#"Blue",#"Yellow",#"Red",nil];
instead of birdColors = [NSArray arrayWithObjects:#"Blue",#"Yellow",#"Red",nil];
Make birdColors a property also (nonatomic, retain) like you do with the pickerView.
Your array is not being retained, so you're accessing zombie memory.
Set NSZombieEnabled=YES in the properties/General panel of your Executable. That will tell you exactly what is being accessed.

Two pickers in a view -iphone app

I am using two UIPickers in a view.
first picker has 3 components and second has one.
but when I select items, it shows correct items from first picker but always return first item from second picker regardless of selected row.
Please help.
here is the code I am using.
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
if (pickerView == triplePicker)
return 3;
else {
return 1;
}
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
if (pickerView == triplePicker) {
if (component == kColorComponent)
return[colorList count];
if (component == kClarityComponent)
return[clarityList count];
return[shapeList count];
}
else{
return [listPickerItems count];
}
}
-(NSString *)pickerView:(UIPickerView *)pickerView
titleForRow:(NSInteger)row
forComponent:(NSInteger)component
{
if (pickerView == triplePicker) {
if (component == kColorComponent)
return [colorList objectAtIndex:row];
if (component == kClarityComponent)
return [clarityList objectAtIndex:row];
return [shapeList objectAtIndex:row];
}
else{
return [listPickerItems objectAtIndex:row];
}
}
in buttonpressed event I have following for second picker to return the item selected:
NSInteger pickrow = [listPicker selectedRowInComponent:0];
NSString *picked = [listPickerItems objectAtIndex:pickrow];
I would definitely suggest using two separate delegates to handle two pickers.
Aside from that, I'm going to guess that you're using Interface Builder to set up your view. If that's the case check if you have properly linked your listPicker with your File's Owner. If listPicker would be nil, then selectedRowInComponent: would always return null (0 for NSInteger), hence would always select the first item in your array.
EDIT: Some sample code for separate delegates:
You need to create a second class to be your delegate, like this:
FirstPickerDelegate.h
#import <Foundation/Foundation.h>
#class UntitledViewController;
#interface FirstPickerViewDelegate : NSObject <UIPickerViewDelegate, UIPickerViewDataSource> {
NSArray* values;
IBOutlet UntitledViewController* viewController;
}
-(void) loadData;
#property (nonatomic, retain) NSArray* values;
#property (nonatomic, assign) IBOutlet UntitledViewController* viewController;
#end
FirstPickerViewDelegate.m
#import "FirstPickerViewDelegate.h"
#implementation FirstPickerViewDelegate
#synthesize values;
#synthesize viewController;
-(id) init
{
if ( self = [super init] )
{
[self loadData];
}
return self;
}
-(void) loadData
{
NSArray* array = [[NSArray alloc] initWithObjects:#"first", #"second", #"third", nil];
self.values = array;
[array release];
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return [values count];
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return [values objectAtIndex:row];
}
-(void) dealloc
{
self.values = nil;
[super dealloc];
}
#end
Your ViewController (UntitledViewController here, sorry about the name):
#import <UIKit/UIKit.h>
#class FirstPickerViewDelegate;
#interface UntitledViewController : UIViewController {
IBOutlet UIPickerView* firstPicker;
IBOutlet FirstPickerViewDelegate* firstDelegate;
}
#property (nonatomic, retain) IBOutlet UIPickerView* firstPicker;
#property (nonatomic, retain) IBOutlet FirstPickerViewDelegate* firstDelegate;
#end
Basically you need to drop an NSObject on your object list, and change it's class to FirstPickerViewDelegate, then make connections like this:
I feel I overelaborated this time, but I'm in a good mood today so whatever :P
About the main question: double check that listPicker is not nil at the time of pressing the button, if it is not, try to use
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
to track down the error.

iPhone UIPickerView in UIViewController

I have a xib in which I have added a UIViewController named delta. The view under delta is controlled by the delta viewcontroller, not the file owner. On the delta view, I have a UIViewPicker. My issue is that I am programming in the UIPickerView in the deltaviewcontroller and I'm issuing deltaviewcontroller as the delegate and data source for the UIPickerView. Everything should work, but when I load the deltaviewcontroller's view, the application crashes. If I carry out everything the same way under the file's owner view, it works fine. I'm wondering if there is really a way to make the UIPickerView work with a UIViewController and no necessarily a file's owner.
For references, the code:
Header
#interface DeltaViewController : UIViewController <UIPickerViewDelegate, UIPickerViewDataSource> {
NSMutableArray *arrayNo;
IBOutlet UIPickerView *pickerView;
}
#property (nonatomic, retain) UIPickerView *pickerView;
#property (nonatomic, retain) NSMutableArray *arrayNo;
#end
Implementation
#import "DeltaViewController.h"
#implementation DeltaViewController
#synthesize pickerView;
#synthesize arrayNo;
- (void)viewDidLoad
{
NSMutableArray *dollarsArray = [[NSMutableArray alloc] init];
for (int i = 0; i < 100; i++)
{
NSString *item = [[NSString alloc] initWithFormat:#"%i", i];
[dollarsArray addObject:item];
}
self.arrayNo = dollarsArray;
[dollarsArray release];
}
#pragma mark -
#pragma mark Picker Data Source Methods
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return [arrayNo count];
}
#pragma mark Picker Delegate Methods
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return [arrayNo objectAtIndex:row];
}
- (void)dealloc {
[arrayNo release];
[pickerView release];
[super dealloc];
}
#end
Please point out if I'm doing anything wrong. Help is much appreciated.
If you search how to add a UIPickerView in a UIView, just check out the Apple sample code for UIPickerView. I think it's one of the best ways learning to use the different classes correctly. There are five project which includes an UIPickerView. Here's the link
UIPickerView class reference and sample code
Try doing
[self.view addSubview:pickerView];
in your viewDidLoad method.

Very simple iPhone question. How to make UIPickerView with two columns?

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]);
}