2012-03-06 83 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