如何解雇iPhone 5 iOS 6中的popover?

时间:2013-07-12 19:34:37

标签: ios6 uiviewcontroller modalviewcontroller presentmodalviewcontroller uipopover

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

并且View Controller消失了。现在我想做的就是让视图控制器最初显示,这样用户就不必通过向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];
}

1 个答案:

答案 0 :(得分: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;