2012-01-31 45 views
1

我正在使用警報視圖彈出一個彈出窗口,用戶將選擇從庫中選擇一張照片或拍攝照片以供使用。警報視圖正常,但是當我選擇一個按鈕時,我已經實現的代碼沒有運行?!?警報視圖按鈕操作不起作用

由於某種原因,- (void)picturePopup:(UIAlertView *)picturePopup clickedButtonAtIndex:(NSInteger)buttonIndex似乎甚至沒有運行!?我失去了很多教程和網站運行,但不明白爲什麼?!請幫忙!

代碼: .M

#import "LoadViewController.h" 

@implementation LoadViewController 

int imageCase; 

- (IBAction)pick:(id) sender { 

    imageCase = [sender tag]; 

    UIAlertView *picturePopup = [[UIAlertView alloc] 
    initWithTitle:@"Select Photo" message:nil delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Choose From Library", @"Take Photo", nil]; 
    [picturePopup show]; 

} 

- (void)picturePopup:(UIAlertView *)picturePopup clickedButtonAtIndex:(NSInteger)buttonIndex { 

     NSLog(@"***************getting here****************"); 

    if (buttonIndex == 1) { 

     NSLog(@"***************library****************"); 
     //Library Picker 
     UIImagePickerController *picker = [[UIImagePickerController alloc] init]; 
     picker.delegate = self; 
     picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; 
     [self presentModalViewController:picker animated:YES]; 
    } 
    if (buttonIndex == 2) { 
     NSLog(@"***************camera****************"); 
     //Camera 
     UIImagePickerController *picker = [[UIImagePickerController alloc] init]; 
     picker.delegate = self; 
     picker.sourceType = UIImagePickerControllerSourceTypeCamera; 
     [self presentModalViewController:picker animated:YES]; 
    } 



} 


- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo { 
    switch (imageCase) { 
     case 1: 
      imageView1.image = image; 
      break; 
     case 2: 
      imageView2.image = image; 
      break; 
    } 

    [picker.parentViewController dismissModalViewControllerAnimated:YES]; 

} 


- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker { 
    [picker.parentViewController dismissModalViewControllerAnimated:YES]; 
} 



@end 

.H

#import <UIKit/UIKit.h> 
#import <Foundation/Foundation.h> 

@interface LoadViewController : UIViewController <UINavigationControllerDelegate, UIImagePickerControllerDelegate, UIAlertViewDelegate> { 
    IBOutlet UIImageView *imageView1; 
    IBOutlet UIImageView *imageView2; 
} 


- (IBAction)pick:(id) sender; 

@end 

回答

1

您需要使用實際的委託方法:

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 

這是不一樣的:

- (void)picturePopup:(UIAlertView *)picturePopup clickedButtonAtIndex:(NSInteger)buttonIndex 

的方法簽名是不同的。 alertView:clickedButtonAtIndex:picturePopup:clickedButtonAtIndex:

您可以重命名變量,但您不能更改方法簽名。

+0

工作感謝您的幫助。雖然現在我有另一個問題,圖像選擇器出現,但是一旦我選擇了圖像或點擊取消圖像選擇器視圖將不會關閉!我認爲它與這段代碼有關的問題,但不知道爲什麼? '[picker.parentViewController dismissModalViewControllerAnimated:YES];' – Matt104 2012-02-01 20:03:22

+0

Picker被模態顯示。它的'parentViewController'屬性爲零。你可以使用'[picker dismiss ...'或'[self dismiss ...' – NJones 2012-02-02 04:37:11

+0

非常感謝! – Matt104 2012-02-02 08:28:30

0

這有點題外話,但我注意到你沒有合成* imageView1* imageView2。笑

+0

我不知道你可以合成一個UIImageView?!我會以同樣的方式做對象變量嗎? – Matt104 2012-02-02 08:37:20

+0

是的,就像平常一樣執行.'@implementation @synthesize imageView1,imageView2;' – AaronChapmanDev 2012-02-02 11:29:42