2012-03-06 78 views
1

我一直在尝试2天来获得一个UIPickerView来显示,我很茫然。我已经实现了所有连接数据源(NSArray)的方法,并且不知道为什么我无法显示它。这个任务现在到了今晚,教授不是没用的,我放弃了问她。UIPickerView不会显示

这是包含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 

回答

2

你需要声明的是你的SpotPicker实现了UIPickerViewDelegateUIPickerViewDataSource协议:

@interface SpotPicker : UIViewController <UIPickerViewDelegate, UIPickerViewDataSource> 

,并设置你的UIPickerView的委托哟ur SpotPicker。你可以用IB或编程方式做到这一点:

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

Eureka!非常感谢: - 这就是我所需要的一切,谢谢你,谢谢你! – jamzsabb 2012-03-06 22:34:11