ivesingh
ivesingh

Reputation: 888

UIPickerView is not showing anything

I implemented a simple label and UIPickerView in Xcode 5 for iOS 7. I used the code from the following website. Everything seems to be correct. Not sure why it's not working.

http://www.youtube.com/watch?v=CIhqiuG8p1k

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController {
    IBOutlet UILabel *label;
    IBOutlet UIPickerView *Picker;
    NSArray *PickerData;
}

@property (retain, nonatomic) IBOutlet UIPickerView *Picker;
@property (retain, nonatomic) NSArray *PickerData;
@end


#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
@synthesize Picker,PickerData;

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    NSArray *array = [[NSArray alloc] initWithObjects:@"One",@"Two",@"Three",@"Four", nil];
    self.PickerData = array;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 1;
}

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:    (NSInteger)component {
    return [PickerData count];
}

-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row  forComponent:(NSInteger)component {
    return [self.PickerData objectAtIndex:row];
}

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:  (NSInteger)component {
    int select = row;
    if (select == 0) {
        label.text = @"One Is Selected";
    } else if (select == 1) {
        label.text = @"Two Is Selected";
    } else if (select == 2) {
        label.text = @"Three Is Selected";
    } else if (select == 3) {
        label.text = @"Four Is Selected";
    }
}

@end

Upvotes: 0

Views: 1641

Answers (1)

Seamus Campbell
Seamus Campbell

Reputation: 17906

There are three likely causes here, all related:

  1. Does the picker view actually exist? Have you added it to the xib file that defines the interface for this class?
  2. Is the picker view in your xib connected to the outlet for your picker view ivar? It's always a good idea when debugging issues like this to check if self.PickerView is actually pointing to an object — if it's nil, you probably haven't connected it and your code will just silently throw away the messages you send to the empty ivar.
  3. Are the datasource and delegate properties of the picker view connected to your view controller? That's how the picker view knows who to ask for its contents.

Also, as an aside on Objective-C style: variables, properties, and ivars are, by convention, have names that begin with lowercase letters, while classes and structures start with uppercase letters. You don't have to follow this convention, but it will make your code easier to read for others, and it will help you read other people's code, in turn.

Upvotes: 2

Related Questions