我正在尝试以编程方式创建一个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 {
}
答案 0 :(得分:5)
如果其他人在控制台中遇到此特定警告“无法找到映射图像UIPickerViewFrameRight-162-Popover.png”,我想我已经找到了它出现的原因。
HIG松散地表示UIPickerView只应添加到iPad上的Popover中。
“在iPad上,只在弹出窗口中显示日期和时间选择器。”
在我的实验中,UIPickerView必须是popover中当前UIViewController的直接和唯一视图。如果UIPickerView出现在视图层次结构的其他位置,则会出现警告并且UIPickerView看起来很糟糕(就像它缺少视图的左右部分一样)。
在上面的代码中,您可以看到UIPickerView被添加为customController中rootView的子视图:
[customViewController.view addSubview:menu];
如果UIPickerView是customController中的根视图,这可能会有效。
您可以通过覆盖控制器的loadView方法并将UIPickerView直接分配给控制器的根视图,以编程方式使UIPickerView成为控制器的根视图:
- (void)loadView {
CGRect frame = CGRectMake(0,0, 300, 300);
UIPickerView *v = [[[UIPickerView alloc] initWithFrame:frame] autorelease];
v.delegate = self; // assuming controller adopts UIPickerViewDelegate
v.dataSource = self; // assuming controller adopts UIPickerViewDataSource
self.view = v;
}
希望这有助于某人。
答案 1 :(得分:2)
好吧我不太确定发生了什么但是我删除了这个项目并重新编写了这个代码并且瞧...没有更多的问题。
答案 2 :(得分:0)
我最近在iPad和iPhone上遇到了与我的应用相同的问题。它实际上是一个简单而又愚蠢的修复方法,它将iPad上的高度设置为180和162。上帝必须爱苹果s / w,但我不喜欢。