1

当我的 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];
}
4

1 回答 1

2

您当前的问题似乎是您希望pickImage在视图首次出现时被调用,而不是在它由于弹出窗口关闭而重新出现时被调用。

一种可能是将pickImage调用从viewDidAppear回调移动到viewDidLoad回调中。

但是,如果调用得太快,另一种选择是添加一个您签入的布尔变量,viewDidAppear以确保pickImage它只被调用一次。

- (void) viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    if (firstAppearance) {
        firstAppearance = NO;
        [self pickImage];
    }
}

并在viewDidLoad回调中将该布尔值设置为 true。

- (void) viewDidLoad {
    [super viewDidLoad];
    firstAppearance = YES;
}

显然,您还需要在头文件中的某处声明 bool。

BOOL firstAppearance;
于 2013-07-15T18:37:12.470 回答