我正在尝试以编程方式创建 uiPickerView 并将其添加到视图中,而不使用界面生成器。不要误会我的意思,我喜欢 IB,但我想这样做的原因是因为我正在尝试构建一个对象,我可以快速插入该对象以使用 UIPopoverViewController 和各种不同的子视图生成弹出菜单(例如 uiPickerView)作为弹出菜单中的菜单。我已经通过在 IB 中构建菜单并使用其 ViewController 初始化弹出窗口来完成这项工作,所以我知道这在很大程度上是如何工作的。
我在下面输入了相关代码,这是我运行它时遇到的两个错误:
- “找不到映射图像 UIPickerViewFrameRight-162-Popover.png”
- “找不到映射图像 UIPickerViewFrameLeft-162-Popover.png”
我不知道这些图像是什么,但我假设它们是选择器视图的 png。
menu = [[UIPickerView alloc]initWithFrame:CGRectMake(0,100,162,162)];
menu.delegate = self;
menu.dataSource = self;
[menu reloadAllComponents];
[menu selectRow:0 inComponent:0 animated:YES];
//Add the picker to the view
[customViewController.view addSubview:menu];
popView = [[UIPopoverController alloc] initWithContentViewController:customViewController] ;
[popView setDelegate:self];
CGRect pos = [rootView frame];
[popView presentPopoverFromRect:CGRectMake(pos.origin.x,pos.origin.y,0,pos.size.height)
inView:displayView permittedArrowDirections:arrowDir animated:YES];
现在,这段代码将使程序崩溃,除非您删除我尝试将选择器添加到视图的行,此时我只会得到空白的弹出窗口。所以我知道是选择器导致了这个问题,但我不知道如何解决它。我已经搜索了一整天,但 uipickers 上的每个在线教程都包含使用 IB。我的猜测是,这是一个非常愚蠢的错误,比如缺少导入或其他东西,但如果有人能告诉我我做错了什么,我将不胜感激。
另请注意,我遵循了有关如何为 UIPickerView 设置 dataSource 和委托方法的教程,我很确定它们没问题,但是如果您想在这里验证的话:再次感谢。
#import "PopUpMenuViewController.h"
@implementation PopUpMenuViewController
@synthesize menuType;
@synthesize data;
@synthesize popView;
@synthesize menu;
@synthesize customViewController;
#pragma mark -
#pragma mark UIPOPOVERCONTROLLER DELEGATE METHODS
#pragma mark -
- (BOOL)popoverControllerShouldDismissPopover:(UIPopoverController *)popoverController{
//Delegate this too the User of this class
return TRUE;
}
- (void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController{
//Delegate this too the User of this class
}
#pragma mark -
#pragma mark CUSTOM POPOVERVIEWCONTROLLER METHODS
#pragma mark -
-(void) initWithMenuType:(int)type{
menuType = type;
}
-(id) initWithMenuType:(int)type andData:(NSMutableArray *)dataSet fromViewItem:(id)sender
withMainView:(UIView *)mView{
[super init];
menuType = type;
data = dataSet;
rootView = sender;
displayView = mView;
arrowDir = UIPopoverArrowDirectionUp;
customViewController = [[UIViewController alloc] initWithNibName:@"PopUpMenu" bundle:nil];
return self;
}
-(void) setPopUpArrowDirection:(UIPopoverArrowDirection) arrow{
arrowDir = arrow;
}
-(void) showPopUp{
//UIPicker Menu
if (menuType==1) {
//UIPicker Setup
menu = [[UIPickerView alloc]initWithFrame:CGRectMake(0,100,162,162)];
menu.delegate = self;
menu.dataSource = self;
[menu reloadAllComponents];
[menu selectRow:0 inComponent:0 animated:YES];
//Add the picker to the view
[customViewController.view addSubview:menu];
popView = [[UIPopoverController alloc] initWithContentViewController:customViewController] ;
[popView setDelegate:self];
CGRect pos = [rootView frame];
[popView presentPopoverFromRect:CGRectMake(pos.origin.x,pos.origin.y,0,pos.size.height)
inView:displayView permittedArrowDirections:arrowDir animated:YES];
//[popView setPopoverContentSize:CGSizeMake(menu.frame.size.width+5,menu.frame.size.height+5)];
}
}
#pragma mark -
#pragma mark VIEW CONTROLLER DELEGATE METHODS
#pragma mark -
// The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
/*
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization.
}
return self;
}
*/
/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/
/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
}*/
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Overriden to allow any orientation.
return YES;
}
- (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.
}
- (void)viewDidUnload {
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[super dealloc];
[data release];
[popView release];
[menu release];
[rootView release];
[displayView release];
[customViewController release];
}
#pragma mark -
#pragma mark UIPICKERVIEW DELEGATE & DATASOURCE METHODS
#pragma mark -
#pragma mark -
#pragma mark UIPickerViewDataSource Methods
- (NSInteger) pickerView: (UIPickerView *) pickerView numberOfRowsInComponent: (NSInteger) component {
return [data count];
}
- (NSInteger) numberOfComponentsInPickerView: (UIPickerView *) pickerView {
return 1;
}
#pragma mark -
#pragma mark UIPickerViewDelegate Methods
// Row height in pixels
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component {
return 40.0f;
}
// Column width in pixels
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
return 90.0f;
}
- (NSString *) pickerView: (UIPickerView *) pickerView titleForRow: (NSInteger) row
forComponent: (NSInteger) component {
return [data objectAtIndex:row];
}
- (void) pickerView: (UIPickerView *) pickerView
didSelectRow: (NSInteger) row inComponent: (NSInteger) component {
}