Reputation: 888
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
Reputation: 17906
There are three likely causes here, all related:
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.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