performSegueWithIdentifier 不起作用

时间:2023-03-22
本文介绍了performSegueWithIdentifier 不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

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

I have this code that loads a new UIView (from storyboard) when a button is pressed. goPressed function is fired on button pressed and it calls selectImage function. selectImage opens a UIImagePickerController and lets user select a photo. After user has selected the photo, didFinishPickingMediaWithInfo delegate adds the selected image to a UIImageView.

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

In 'goPressed', after selectImage is performed, it should be performing a Segue at like commented as //1. But nothing happens. performSegueWithIdentifier doesn't seem to be working. And if I don't call [self selectImage] before calling performSegueWithIdentifier, it works. Here is the code:

- (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 不起作用?我希望我没有遗漏您需要的任何信息.

Please help, why performSegueWithIdentifier isn't working? I hope I am not missing any information you need.

推荐答案

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

I'm assuming that the view you are trying to segue to is using the picture you get just before you do your segue? If they cancel from the image picker do you still want to segue?

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

If it needs the picture, then maybe you should call your segue after the delegate call "did finish picking".

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

The issue with the segue not firing may be due to the animation still occurring from here:

[[picker presentingViewController] dismissModalViewControllerAnimated:YES];

你可以试试:

[[picker presentingViewController] dismissModalViewControllerAnimated:NO];

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

or if you want to maintain the animation, move the segue to the "picker did finish" method and do it this way:

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

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

or if that does not work try this approach in the pickerdidfinish method (note - this should be implemented as a delegate in the controller that calls the modal view, not the modal view itself:

//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 视图中滑动,暂停让过渡看起来很自然.

I use this transition frequently and it makes a nice drop away with the modal view then slides in the segue view and the pause allows the transition to seem natural.

这篇关于performSegueWithIdentifier 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:在 iphone 中实现 tesseract OCR 下一篇:iOS6 中未调用 shouldAutoRotate 方法

相关文章