我在我的代码中使用UIImagePickerView
,其中有三个按钮。一个拍照,第二个选择照片,第三个取消。代码如下:
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Select the operation to proceed?"
delegate:self
cancelButtonTitle:@"Cancel"
destructiveButtonTitle:nil
otherButtonTitles:@"Take Photo", @"Select Photo", nil];
[actionSheet showInView:self.view];
当我点击取消按钮时,它不起作用。
我正在使用UIImagePickerViewDelegate
方法,如下所示。
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
@try {
//set selected image in imageview by imagepickerview
UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
imgProfilePic.image = chosenImage;
[picker dismissViewControllerAnimated:YES completion:nil];
}
@catch (NSException *exception) {
NSLog(@"%@",exception.description);
}
}
//- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
//{
// NSLog(@"@@@@");
// [picker dismissViewControllerAnimated:YES completion:NULL];
//}
请检查我的UIImagePickerView
代码,并为我提供正确代码的指导。
#pragma mark - Actionssheet delegate method for Image
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
@try {
if(buttonIndex != 3)
{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
if(buttonIndex == 0)
{
NSLog(@"Choose Photo from Camera");
//simulator has no camera so app will crash, below call just provide the alert that device has no camera.
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:@"Error"
message:@"Device has no camera"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles: nil];
[myAlertView show];
}
else
{
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
}
}
if(buttonIndex == 1)
{
NSLog(@"Choose Photo from Gallary");
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
}
// if (buttonIndex == 3)
// {
// NSLog(@"###");
// }
//
// else
// {
// NSLog(@"Cancel the tab");
// [picker dismissViewControllerAnimated:YES completion:NULL];
//
// }
[self presentViewController:picker animated:YES completion:nil];
}
}
@catch (NSException *exception) {
NSLog(@"%@",exception.description);
}
}
答案 0 :(得分:1)
我认为你是stackOverflow和iOS应用程序开发的新手。 UIActionsheet
和UIImagePickerController
都是不同的东西。所以,如果你要创建一个如下的actiohsheet:
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Select the operation to proceed?"delegate:self
cancelButtonTitle:@"Cancel"
destructiveButtonTitle:nil otherButtonTitles:@"Take Photo", @"Select Photo", nil];
[actionSheet showInView:self.view];
现在您可以调用其按钮方法,如下所示:
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex==0)
{
// take photo selected
}
else if (buttonIndex==1)
{
// select photo selected
}
else
{
// cancel button selected
}
}
您还可以使用UIAlertController
取代iOS8中的UIAlert
和UIActionSheet
,以便您可以使用以下内容:
UIAlertController* AlertSheet = [UIAlertController alertControllerWithTitle:@"New ActionSheet" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"Action" style:UIAlertActionStyleDefault //
handler:^(UIAlertAction * action) {
NSLog(@"Action");
}];
[AlertSheet addAction:defaultAction];
UIAlertAction *cancleAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
}];
[AlertSheet addAction:cancleAction];
[self presentViewController:AlertSheet animated:YES completion:nil];
答案 1 :(得分:0)
您可以尝试将此代码用于我的工作
UIAlertController* alert = [UIAlertController
alertControllerWithTitle:nil // Must be "nil", otherwise a blank title area will appear above our two buttons
message:nil
preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction* button0 = [UIAlertAction
actionWithTitle:@"Cancel"
style:UIAlertActionStyleCancel
handler:^(UIAlertAction * action)
{
// UIAlertController will automatically dismiss the view
}];
UIAlertAction* button1 = [UIAlertAction
actionWithTitle:@"Take photo"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
// The user tapped on "Take a photo"
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:picker animated:YES completion:NULL]; }];
UIAlertAction* button2 = [UIAlertAction
actionWithTitle:@"Choose Existing"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
// The user tapped on "Choose existing"
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:picker animated:YES completion:NULL];
}];
[alert addAction:button0];
[alert addAction:button1];
[alert addAction:button2];
[self presentViewController:alert animated:YES completion:nil];
答案 2 :(得分:0)
UIActionSheet因iSO 8而被弃用。您应该使用UIAlertController
UIAlertController* alert = [UIAlertController
alertControllerWithTitle:nil // Must be "nil", otherwise a blank title area will appear above our two buttons
message:nil
preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction* button0 = [UIAlertAction
actionWithTitle:@"Cancel"
style:UIAlertActionStyleCancel
handler:^(UIAlertAction * action)
{
// UIAlertController will automatically dismiss the view
}];
UIAlertAction* button1 = [UIAlertAction
actionWithTitle:@"Take photo"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
// The user tapped on "Take a photo"
UIImagePickerController *imagePickerController= [[UIImagePickerController alloc] init];
imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePickerController.delegate = self;
[self presentViewController:imagePickerController animated:YES completion:^{}];
}];
UIAlertAction* button2 = [UIAlertAction
actionWithTitle:@"Select Photo"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
// The user tapped on "Select Photo"
UIImagePickerController *imagePickerController= [[UIImagePickerController alloc] init];
imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imagePickerController.delegate = self;
[self presentViewController:imagePickerController animated:YES completion:^{}];
}];
[alert addAction:button0];
[alert addAction:button1];
[alert addAction:button2];
[self presentViewController:alert animated:YES completion:nil];