gpt4 book ai didi

objective-c - UIPickerView 不会显示

转载 作者:行者123 更新时间:2023-11-29 11:15:40 24 4
gpt4 key购买 nike

我已经尝试了 2 天来显示 UIPickerView,但我不知所措。我已经实现了连接数据源 (NSArray) 的所有方法,但不知道为什么无法显示它。作业现在今晚交,教授也不是没用,我已经放弃问她了。

这是包含 UIPickerView 的 subview 的 .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

最佳答案

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

@interface SpotPicker : UIViewController <UIPickerViewDelegate, UIPickerViewDataSource>

并将您的 UIPickerView 的委托(delegate)设置为您的 SpotPicker。您可以在 IB 中或以编程方式执行此操作:

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

关于objective-c - UIPickerView 不会显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9591431/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com