2012-02-27 55 views
0

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

当我们按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 ....并没有这些工作。

回答

0

实现这个委托方法

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

对不起,我应该说。我已经尝试过,并且还有“willDissWithButtonIndex”,而且他们都不工作。 – Luke 2012-02-27 09:52:02

4

尝试此方法...

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

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

“Gallary”和“CAMERA”中的值是什么?它是整数或字符串 – Rupesh 2012-02-27 10:00:38

+0

它是: #define GALLERY 1 #define CAMERA 0 – Luke 2012-02-27 10:57:29

+0

做一件事..获取本地int temp = alertview.tag中的alertview.tag的值并将该值与CAMERA/GALLERY进行比较 – Rupesh 2012-02-27 11:00:30

0

而不是使用两个按钮..使用actionsheet的。

- (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]; 
} 

- (无效)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]; 
} 

}

+0

您可以修改此代码并根据您的项目使用它。 – Rupesh 2012-02-27 13:53:44

+0

如果您有任何疑问,请问我 – Rupesh 2012-02-27 13:55:15