在我的應用程序(下面列出的代碼)中,我使用彈出窗口來顯示用戶可以選擇的一系列顏色。這些顏色用於上面正在完成的圖形的顏色。我試圖修改popover以相同的方式工作,除了這次我想要顯示圖像(圖像保存在應用程序的文檔文件夾中作爲png文件)而不是顏色塊。下面列出的是顏色選擇器彈出窗口的工作代碼。 ColorGrid是一個包含NSArray Colors的UIview,以及兩個NSUIntegers columnCount和rowCount。我試圖用UIImage替換顏色數組中的項目以及UIImageViews,但是我還沒有能夠獲得成功的結果(或可編譯的結果)。下面列出的是工作代碼。任何人都可以告訴我如何將UIColor項目更改爲圖像以在網格中顯示它們?無法調整UIPopupController以顯示圖像
- (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
}
顯示代碼,你試過放置圖像視圖,以及在編譯時會遇到什麼樣的錯誤。 – rdelmar
剛剛編輯顯示,我把圖像和imageView上面的評論行我得到的erros。當在圖像之前放置類型時(比如UIImage圖像或者UIImageView imageView - 我沒有找到類方法的警告,並且它不起作用) – njj56
由於您分配了initWithImage :,因此您顯示的最後6行不需要,所以圖片已經設置好了,現在還不清楚你的錯誤在哪裏以及它們是什麼,發佈實際的錯誤信息 – rdelmar