我正在使用警報視圖彈出一個彈出窗口,用戶將選擇從庫中選擇一張照片或拍攝照片以供使用。警報視圖正常,但是當我選擇一個按鈕時,我已經實現的代碼沒有運行?!?警報視圖按鈕操作不起作用
由於某種原因,- (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
工作感謝您的幫助。雖然現在我有另一個問題,圖像選擇器出現,但是一旦我選擇了圖像或點擊取消圖像選擇器視圖將不會關閉!我認爲它與這段代碼有關的問題,但不知道爲什麼? '[picker.parentViewController dismissModalViewControllerAnimated:YES];' – Matt104 2012-02-01 20:03:22
Picker被模態顯示。它的'parentViewController'屬性爲零。你可以使用'[picker dismiss ...'或'[self dismiss ...' – NJones 2012-02-02 04:37:11
非常感謝! – Matt104 2012-02-02 08:28:30