【问题标题】:UIPickerView won't showUIPickerView 不会显示
【发布时间】:2012-03-06 20:35:44
【问题描述】:

我已经尝试了 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

【问题讨论】:

    标签: objective-c ios


    【解决方案1】:

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

    @interface SpotPicker : UIViewController <UIPickerViewDelegate, UIPickerViewDataSource>
    

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

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

    【讨论】:

    • 尤里卡!非常感谢 :-D 这就是我所需要的谢谢谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多