objective-c - performSegueWithIdentifier 不工作

标签 objective-c ios5 delegates segue

我有这段代码可以在按下按钮时加载一个新的 UIView(来自 Storyboard )。按下按钮时触发 goPressed 函数,并调用 selectImage 函数。 selectImage 打开一个 UIImagePickerController 并让用户选择一张照片。用户选择照片后,didFinishPickingMediaWithInfo 委托(delegate)将所选图像添加到 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 不起作用?希望我没有遗漏您需要的任何信息。

最佳答案

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

如果它需要图片,那么也许你应该在委托(delegate)调用“完成选择”之后调用你的 segue。

segue 未触发的问题可能是由于动画仍在此处发生:

[[picker presentingViewController] dismissModalViewControllerAnimated:YES];

你可以试试:

[[picker presentingViewController] dismissModalViewControllerAnimated:NO];

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

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

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

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

关于objective-c - performSegueWithIdentifier 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10464638/

相关文章:

objective-c - Cocoa - 暂停一个方法

iphone - 即使在ARC中也可能存在对象泄漏?

iphone - 在 UITableViewCell 中动态分配 UIButton 事件

ios5 - 为什么 -[[UIButton 外观] setBackgroundImage] 会影响 UIBarItem 对象的初始外观以及如何纠正它?

ios - 永远不会调用委托(delegate)方法

c# - 返回值未显示

iphone - 如何获取存储在设备中的所有图像并在 iPhone sdk 中将它们显示为图库

iphone - 在 View 中设置 searchBar.text 确实加载了

ios - 如何使用快速枚举修改 NSArray 的字符串元素?

c# - 为什么编译器不能在这种重载解析情况下告诉更好的转换目标? (协方差)