警报视图按钮操作无效

时间:2012-01-31 22:53:23

标签: objective-c xcode uialertview

我正在使用警报视图弹出一个弹出窗口,用户可以选择从库中选择照片或拍照使用。警报视图很好,但是当我选择一个按钮时,我实现的代码没有运行?!?

由于某种原因,- (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

2 个答案:

答案 0 :(得分:1)

您需要使用实际的委托方法:

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

这与以下内容不同:

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

方法签名不同。 alertView:clickedButtonAtIndex:picturePopup:clickedButtonAtIndex:

您可以重命名变量,但无法更改方法签名。

答案 1 :(得分:0)

这有点偏离主题,但我注意到你没有合成 * imageView1 * imageView2 。洛尔