UIPickerView不会显示

时间:2012-03-06 20:35:44

标签: objective-c ios

我已经尝试了2天才能显示UIPickerView并且我不知所措。我已经实现了连接数据源(NSArray)的所有方法,并且不知道为什么我无法显示它。这项任务现在到了今晚,而教授也没有用,我放弃了问她。

这是包含UIPickerView的子视图的.m文件。我只需要弄清楚为什么选择器不会显示!帮助表示我很生气......

#import "SpotPicker.h"

@implementation SpotPicker
@synthesize spotPicker;

NSArray *pickerLocations;
NSString *selectedLocation;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
    // Custom initialization
}
return self;
}

- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];

// Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
self.modalTransitionStyle=UIModalTransitionStyleFlipHorizontal;

pickerLocations = [[NSArray alloc]initWithObjects:@"Grand Canyon", @"Mt Rushmore", @"Statue of Liberty", @"Empire State Building", @"Hollywood Sign", @"Lincoln Memorial", @"Space Needle", nil];
}
- (void)viewDidUnload
{
[self setSpotPicker:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (IBAction)dismissPicker:(id)sender {
[self dismissModalViewControllerAnimated:YES];
}

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

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

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

- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
//NSLog(@"Selected Color: %@. Index of selected color: %i", [pickerLocations objectAtIndex:row], row);
selectedLocation = [pickerLocations objectAtIndex:row];
}

-(NSString *) getLocation {
return selectedLocation;
}

@end

这是头文件,只是为了确保我没有声明错误或愚蠢的东西

#import <UIKit/UIKit.h>

@interface SpotPicker : UIViewController

- (IBAction)dismissPicker:(id)sender;
-(NSString *) getLocation;

@property (weak, nonatomic) IBOutlet UIPickerView *spotPicker;

@end

1 个答案:

答案 0 :(得分:2)

您需要声明SpotPicker实施UIPickerViewDelegateUIPickerViewDataSource协议:

@interface SpotPicker : UIViewController <UIPickerViewDelegate, UIPickerViewDataSource>

并将UIPickerView的委托设为您的SpotPicker。您可以在IB中执行此操作,也可以以编程方式执行此操作:

- (void)viewDidLoad
{
    ...
    self.spotPicker.delegate = self;
    self.spotPicker.dataSource = self;
}