无法调整 UIPopupController 显示图像
Posted
技术标签:
【中文标题】无法调整 UIPopupController 显示图像【英文标题】:Can not adjust UIPopupController to display images 【发布时间】:2012-10-05 17:20:10 【问题描述】:在我的应用程序(下面列出的代码)中,我使用一个弹出框来显示一系列用户可以选择的颜色。这些颜色用于他们在上面完成的绘图的颜色。我正在尝试修改弹出框以相同的方式工作,除了这次我想显示图像(图像作为 png 文件保存在应用程序的文档文件夹中)而不是颜色块。下面列出的是颜色选择器弹出框的工作代码。 ColorGrid 是一个 UIview,它包含一个 NSArray 颜色,以及两个 NSUIntegers columnCount 和 rowCount。我试图用 png 文件的 UIImages 以及 UIImageViews 替换颜色数组中的项目,但我无法获得成功的结果(或可编译的结果)。下面列出的是工作代码。谁能告诉我如何将 UIColor 项目更改为图像以在网格中显示它们?
- (IBAction)popoverStrokeColor:(id)sender
StrokeColorController *scc = [[[StrokeColorController alloc] initWithNibName:@"SelectColorController" bundle:nil] autorelease];
scc.selectedColor = self.strokeColor;
[self doPopoverSelectColorController:scc sender:sender];
- (void)doPopoverSelectColorController:(SelectColorController*)scc sender:(id)sender
[self setupNewPopoverControllerForViewController:scc];
scc.container = self.currentPopover;
self.currentPopover.popoverContentSize = scc.view.frame.size;
scc.colorGrid.columnCount = 2;
scc.colorGrid.rowCount = 3;
scc.colorGrid.colors = [NSArray arrayWithObjects:
//put the following below back in after testing
[UIColor blackColor],
[UIColor blueColor],
[UIColor redColor],
[UIColor greenColor],
[UIColor yellowColor],
[UIColor orangeColor],
//[UIColor purpleColor],
// [UIColor brownColor],
// [UIColor whiteColor],
// [UIColor lightGrayColor],
//[UIColor cyanColor],
//[UIColor magentaColor],
nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(colorSelectionDone:) name:ColorSelectionDone object:scc];
[self.currentPopover presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES]; //displays the popover and anchors it to the button
感谢您的帮助。我是 Objective-c 的新手。
编辑 - 这是我尝试插入图像而不是颜色的功能
- (void)doPopoverSelectColorController:(SelectColorController*)scc sender:(id)sender
[self setupNewPopoverControllerForViewController:scc];
scc.container = self.currentPopover;
self.currentPopover.popoverContentSize = scc.view.frame.size;
// these have to be set after the view is already loaded (which happened
// a couple of lines ago, thanks to scc.view...
scc.colorGrid.columnCount = 2;
scc.colorGrid.rowCount = 3;
//here we need to get the UIImage items to try to put in the array.
NSArray *pathforsave = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = [pathforsave objectAtIndex:0];
//here we need to add the file extension onto the file name before we add the name to the path
//[fileName appendString:@".hmat"];
NSString *strFile = [documentDirectory stringByAppendingPathComponent:@"test.png"];
NSString *strFile1 = [documentDirectory stringByAppendingPathComponent:@"test1.png"];
NSString *strFile2 = [documentDirectory stringByAppendingPathComponent:@"test2.png"];
NSString *strFile3 = [documentDirectory stringByAppendingPathComponent:@"test3.png"];
NSString *strFile4 = [documentDirectory stringByAppendingPathComponent:@"test4.png"];
NSString *strFile5 = [documentDirectory stringByAppendingPathComponent:@"test5.png"];
//now for the Images
UIImage *image = [ UIImage imageWithContentsOfFile: strFile];
UIImage *image1 = [ UIImage imageWithContentsOfFile: strFile1];
UIImage *image2 = [ UIImage imageWithContentsOfFile: strFile2];
UIImage *image3 = [ UIImage imageWithContentsOfFile: strFile3];
UIImage *image4 = [ UIImage imageWithContentsOfFile: strFile4];
UIImage *image5 = [ UIImage imageWithContentsOfFile: strFile5];
UIImageView *imageview = [[[UIImageView alloc] initWithImage:image] autorelease];
[self.view addSubview:imageview];
UIImageView *imageview1 = [[[UIImageView alloc] initWithImage:image1] autorelease];
[self.view addSubview:imageview1];
UIImageView *imageview2 = [[[UIImageView alloc] initWithImage:image2] autorelease];
[self.view addSubview:imageview2];
UIImageView *imageview3 = [[[UIImageView alloc] initWithImage:image3] autorelease];
[self.view addSubview:imageview3];
UIImageView *imageview4 = [[[UIImageView alloc] initWithImage:image4] autorelease];
[self.view addSubview:imageview4];
UIImageView *imageview5 = [[[UIImageView alloc] initWithImage:image5] autorelease];
[self.view addSubview:imageview5];
imageview.image = image;
imageview1.image = image1;
imageview2.image = image2;
imageview3.image = image3;
imageview4.image = image4;
imageview5.image = image5;
scc.colorGrid.colors = [NSArray arrayWithObjects:
// When attempting to add the images like this - get the error identified expected
// after the e in image, at the end bracket. Putting a * does nothing to change the error
[image],
// When adding one of the Imageviews, i get the same error as above
//below is how I attempted to add it
[imageView],
//
nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(colorSelectionDone:) name:ColorSelectionDone object:scc];
[self.currentPopover presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES]; //displays the popover and anchors it to the button
【问题讨论】:
显示您尝试放入图像视图的代码,以及编译时遇到的错误类型。 刚刚编辑显示,我在图像上方的注释行中添加了图像查看我得到的错误。将类型放在图像之前时(如 UIImage 图像或 UIImageView 图像视图 - 我收到未找到类方法的警告。它不起作用。 由于您分配了 initWithImage:,因此不需要您显示的最后 6 行,因此图像已经设置。目前尚不清楚您的错误在哪里以及它们是什么。发布实际的错误消息。 所有错误都是预期的标识符。快速帮助有这个,但我无法成功地完成它的建议 - 描述:用作单元格内容的图像。可用性 ios(2.0 及更高版本)已弃用:改为使用 imageView 属性获取 UIImageView 对象,然后获取或设置封装的图像。 我可能刚刚发现了问题 - 看起来像是我的 drawRect 函数 - 如果需要,将继续工作并重新发布 【参考方案1】:删除 image 和/或 imageView 周围的方括号:
scc.colorGrid.colors = [NSArray arrayWithObjects:
// Not : [image] but
image,
// or
imageView,
// Not : [imageView],
nil];
【讨论】:
谢谢,这可以将图像添加到数组中,我仍在使用 drawRect 函数以正确显示项目以上是关于无法调整 UIPopupController 显示图像的主要内容,如果未能解决你的问题,请参考以下文章
Monotouch - UIActionSheet 无法在 iPad 上正确显示
除非调整大小,否则框架不显示组件 (JAVA AWT)。无法修复