开发者

iphone modal dialog like native "take picture, choose existing"

How do I create a modal dialog with customisable button choices just like the "take picture or video" | "choose existing" dialog on the iPhone? Those buttons aren't normal UIButton's and I'm sure they aren't hand 开发者_C百科crafted for every app out there.


It sounds like you want to use a combination of UIActionSheet and UIImagePickerController. This code shows a popup that lets the user choose to take a photo or choose an existing one, then the UIImagePickerController pretty much does everything else:

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

NOTE: This assumes you have a member variable "mediaPicker" which keeps a reference to the UIImagePickerController

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜