当我的 iPhone 5 应用程序启动时,我没有显示视图控制器,原因如下:
- (void) viewDidAppear:(BOOL)animated {
}
用户要做的第一件事是触摸初始屏幕上的一个按钮,该按钮打开一个菜单,用于从用户的照片库、相机或奥巴马的照片中检索照片。用户触摸该按钮时调用的方法是:
- (IBAction) pickImage {
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Image Source"
delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil
otherButtonTitles:@"Photo Library", @"Camera", @"Obama", nil];
[actionSheet showInView:scrollView];
[actionSheet release];
}
现在 actionSheet 最终调用:
[self presentViewController:imagePicker animated:TRUE completion:nil];
对于 iPhone 5,一切正常,用户可以使用 presentViewController 获得 3 个选项。用户选择图像后,我调用:
[[picker presentingViewController] dismissViewControllerAnimated:YES completion:nil];
并且视图控制器消失了。现在我想做的是让视图控制器最初出现,这样用户就不必通过向 viewDidAppear 方法添加一行来触摸连接到 pickImage 的按钮,如下所示:
- (void) viewDidAppear:(BOOL)animated {
[self pickImage]; // makes the image picker pop up when app intializes
}
但是,当我对此进行测试时,imagePicker 会像我预期的那样自动出现,但是在我完成选择图像并将其关闭后,它会在我选择图像后重新出现。我怎样才能解决这个问题???
以下是我正在谈论的类: MainViewController.h 文件:http ://db.tt/Vgm3w0gs MainViewController.m文件: http ://db.tt/uN8YdNGN
或者你可以只看下面的相关方法:
- (void) actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex {
switch (buttonIndex) {
case 0: { //photo library
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:imagePicker animated:TRUE completion:nil];
} else {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Photo library is empty or unavailable" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alertView show];
}
break;
}
case 1: //camera option has it's view controller dismissed after image is taken. The rest of ActionSheet doesn't really matter...
HERE 是关闭 ViewController 的方法,除非从 viewDidAppear 中调用 imagePicker。问题:当从 viewDidAppear 中调用 imagePicker 时,如何让 viewController 关闭?
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
// loads image into main screen
[self loadImage:[info objectForKey:UIImagePickerControllerOriginalImage]];
[[picker presentingViewController] dismissViewControllerAnimated:YES completion:nil];
} [picker release];
}