2011-09-05 208 views
0

我對應用程序開發並不陌生,我正在構建一個應用程序(這將會更加複雜,但這只是我稍後擴展的基礎)。我目前所看到的是UIImageViewUIButton,代碼如下。我可以通過按鈕來從資源文件夾中設置圖像,但是我想要的是,每次按下時,按鈕都會用新圖像更新UIImageView。按一次= image1,按兩次= image2 ...等從我一直在閱讀我認爲我應該有一個數組中的圖像與一個鍵或什麼,然後我可以用++更新它我知道這是非常模糊,但正如我所說我真的很新,所以任何幫助將不勝感激!使用UIButton更新UIImageView中的圖像

@interface NextImageViewController : UIViewController { 

    IBOutlet UIImageView *imageView;  
} 

- (IBAction) nextImagePush; 

@end 

@implementation NextImageViewController 

- (IBAction) nextImagePush { 

    UIImage *img = [UIImage imageNamed:@"image1"]; 
    [imageView setImage:img];  
} 

回答

1
@interface NextImageViewController : UIViewController { 

    IBOutlet UIImageView *imageView; 
    NSInteger imageCount; 
    NSArray * imageArray; 
} 

- (IBAction) nextImagePush; 

@end 

@implementation NextImageViewController 

- (IBAction) nextImagePush { 


    UIImage *img = [UIImage imageNamed:[imageArray objectAtIndex:imageCount]]; 
    [imageView setImage:img];  
    imageCount++; 
    if (imageCount >= [imageArray count]){ 
     imageCount = 0; 
    } 

} 

- (void) viewDidLoad { 
    imageArray = [[NSArray alloc] 
     initWithObjects:@"image1", @"image2", @"image3", nil]; 
    imageCount = 0; 
} 

嘗試類似的東西。您必須設置初始圖像並根據設置的內容更改計數。

+0

Mike007非常感謝你的工作!這個網站真棒! –

0

一個非常基本的方法是設置一個整數。將其定義爲0, ,並在您第一次單擊按鈕時將其添加1。

這絕對不是工作代碼,但它只是爲了幫助你獲得開始。我強烈建議在iOS或Beginning IPhone 4 Development這本書上寫下Big Nerd Ranch的書。

-(IBAction)pic 
if variable == 0 { 
//show the first image 
variable++; 
} 
else if variable == 1 { 
//show the second image 
variable++; 
} 
+0

btw我推薦使用Saphrosit的方法。 – Sum

1

您可以簡單地創建一個NSArrayNSMutableArray並與所有你想要顯示的圖像填充它。然後,使用一個變量可見在全局範圍內(即你的界面的字段),可以使用

[imageView setImage:[arrayOfImages objectAtIndex:(index++ % [arrayOfImages count])]]; 

這段代碼也將顯示第一圖像顯示在最後一個之後。