objective-c – performSegueWithIdentifier无法正常工作

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

在’goPressed’中,在执行selectImage之后,它应该执行一个Segue,如注释为// 1.但没有任何反应. 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不工作?我希望我不会错过任何你需要的信息.

我假设你想要的视图是使用你在做segue之前得到的图片吗?如果他们从图像选择器中取消,你还想要塞住吗?

如果它需要图片,那么也许你应该在委托电话“完成选择”后调用你的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视图中滑动,并且暂停允许过渡看起来很自然.

相关文章
相关标签/搜索