3

我有这段代码在按下按钮时加载一个新的 UIView(来自情节提要)。goPressed 函数在按下按钮时触发,它调用 selectImage 函数。selectImage 打开一个 UIImagePickerController 并让用户选择一张照片。用户选择照片后,didFinishPickingMediaWithInfo 委托将所选图像添加到 UIImageView。

在“goPressed”中,执行 selectImage 后,它应该在注释为 //1 时执行 Segue。但什么也没有发生。performSegueWithIdentifier 似乎不起作用。如果我在调用 performSegueWithIdentifier 之前不调用 [self selectImage],它会起作用。这是代码:

- (IBAction)goPressed:(id)sender {
    [self selectImage];
    [self performSegueWithIdentifier:@"lastView" sender:currentSender]; //1
}
-(void)selectImage
{
    // Create image picker controller
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];

    // Set source to the camera
    imagePicker.sourceType =  UIImagePickerControllerSourceTypePhotoLibrary;

    // Delegate is self
    imagePicker.delegate = (id)self;

    // Allow editing of image ?
    imagePicker.allowsEditing=NO;


    // Show image picker
    [self presentModalViewController:imagePicker animated:YES];


}

- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    // Access the uncropped image from info dictionary
    UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];

    [[picker presentingViewController] dismissModalViewControllerAnimated:YES];


    lePreview.image= image;


}

请帮忙,为什么 performSegueWithIdentifier 不起作用?我希望我没有遗漏您需要的任何信息。

4

1 回答 1

13

我假设您尝试转场的视图是使用您在转场之前获得的图片?如果他们从图像选择器中取消,你还想继续吗?

如果它需要图片,那么也许你应该在代表调用“完成挑选”之后调用你的 segue。

segue 没有触发的问题可能是由于动画仍然从这里发生:

[[picker presentingViewController] dismissModalViewControllerAnimated:YES];

你可以试试:

[[picker presentingViewController] dismissModalViewControllerAnimated:NO];

或者如果你想保持动画,将 segue 移动到“picker did finish”方法并这样做:

[self dismissModalViewControllerAnimated:YES completion:^() {
[self performSegueWithIdentifier:@"lastView" sender:self];
}];

或者如果这不起作用,请在 pickerdidfinish 方法中尝试这种方法(注意 - 这应该作为调用模态视图的控制器中的委托来实现,而不是模态视图本身:

//maintain the animation
[self dismissModalViewControllerAnimated:YES];

//slight pause to let the modal page dismiss and then start the segue
double delayInSeconds = 0.5;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){

    //code to be executed on the main queue after delay

    [self performSegueWithIdentifier:@"lastView" sender:self];

});

我经常使用这种过渡,它在模态视图中很好地下降,然后在 segue 视图中滑动,暂停让过渡看起来很自然。

于 2012-05-05T19:06:08.187 回答