iphone模式对话框,如本机"拍照,选择现有的"

时间:2011-04-21 03:54:33

标签: iphone ios4

如何创建具有可自定义按钮选项的模式对话框,就像“拍照或录像”一样iPhone上的“选择现有”对话框?这些按钮不是普通的UIButton,我确信它们不是为每个应用程序手工制作的。

1 个答案:

答案 0 :(得分:15)

听起来你想要使用UIActionSheet和U​​IImagePickerController的组合。此代码显示一个弹出窗口,允许用户选择拍照或选择现有照片,然后UIImagePickerController几乎完成其他所有操作:

- (IBAction)handleUploadPhotoTouch:(id)sender {
    mediaPicker = [[UIImagePickerController alloc] init];
    [mediaPicker setDelegate:self];
    mediaPicker.allowsEditing = YES;

    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil
                                                                 delegate:self
                                                        cancelButtonTitle:@"Cancel"
                                                   destructiveButtonTitle:nil
                                                        otherButtonTitles:@"Take photo", @"Choose Existing", nil];
        [actionSheet showInView:self.view];
    } else {
        mediaPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;     
        [self presentModalViewController:mediaPicker animated:YES];
    }
}


- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 0) {
        mediaPicker.sourceType = UIImagePickerControllerSourceTypeCamera;
    } else if (buttonIndex == 1) {
        mediaPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;     
    }

    [self presentModalViewController:mediaPicker animated:YES];
    [actionSheet release];
}

注意:这假设您有一个成员变量" mediaPicker"它保留了对UIImagePickerController的引用