UIAlertView不会调用函数

时间:2012-02-27 09:47:07

标签: objective-c ios uialertview

我有一个功能,可以让你选择如何拍照(从画廊或相机)。如果没有“内容”(你还没有拍照)那么你可以自由地做。如果它确实有内容(你已经拍了照片),那么它会弹出一个对话框询问你是否确定。我已经实现了UIAlertViewDelegate,以便我可以告诉他们按哪个按钮。

我的问题出现在他们按下OK(按钮索引0)但功能不起作用(它确实被调用,我通过调试器逐步完成)。如果我再次按下按钮,它也不会让我弹出另一个对话框。

我对问题是什么感到困惑,我想不出谷歌试图找出什么。

感谢任何帮助,谢谢。

-(IBAction)galleryButtonPressed:(id)sender
{
    if (!mHasContent)
    {
        [mDelegate performSelector:@selector(galleryButtonPressed:) withObject:self];
    }
    else
    {
        UIAlertView* warning = [[UIAlertView alloc] initWithTitle:@"Section Complete" message:@"You have already taken an image. Selecting another will overwrite it, you can select to take another image above. Are you sure you wish to continue?" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: @"Cancel", nil];

        [warning setTag:GALLERY];
        [warning show];
        [warning release];
    }
}

-(IBAction)cameraButtonPressed:(id)sender
{
    if (!mHasContent)
    {
        [mDelegate performSelector:@selector(captureImageButtonPressed:) withObject:self];
    }
    else
    {
        UIAlertView* warning = [[UIAlertView alloc] initWithTitle:@"Section Complete" message:@"You have already taken an image. Selecting another will overwrite it, you can select to take another image above. Are you sure you wish to continue?" delegate:self cancelButtonTitle:nil otherButtonTitles: @"OK", @"Cancel", nil];

        [warning setTag:CAMERA];
        [warning show];
        [warning release];
    }
}

-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    // If they selected OK then resend the event to take a photo or select from the gallery
    if (buttonIndex == 0)
    {
        mHasContent = NO;

        if ([alertView tag] == GALLERY)
        {
            [self galleryButtonPressed:mGalleryButton];
        }
        else
        {
            [self cameraButtonPressed:mCameraButton];
        }
    }
}

我也尝试过使用onClick和willDismiss ....但这些都没有。

3 个答案:

答案 0 :(得分:4)

尝试这种方法......

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { 
    if (buttonIndex == 0)
    {
        mHasContent = NO;

        if ([alertView tag] == GALLERY)
        {
            [self galleryButtonPressed:mGalleryButton];
        }
        else
        {
            [self cameraButtonPressed:mCameraButton];
        }
    }
}   

答案 1 :(得分:0)

实现此委托方法

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

答案 2 :(得分:0)

而不是使用两个按钮..使用动作表。

- (IBAction)takePic :( id)发件人 {

GroceryApplicationAppDelegate *appDelegate = (GroceryApplicationAppDelegate*)[[UIApplication sharedApplication]delegate];

UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Take Photo",@"Choose Existing Photo",nil];
[sheet setDelegate:self];
[sheet showInView:appDelegate.window];
[sheet release];
}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {

if(buttonIndex == 0)
{

    if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:@"No Camera Found" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
        [alert release];

        return;
    }

    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.sourceType = UIImagePickerControllerSourceTypeCamera;

    [self presentModalViewController:picker animated:YES];
}

if (buttonIndex == 1) 
{

    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;

    [self presentModalViewController:picker animated:YES];
}

}