我有这个代码,当按下按钮时会加载一个新的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不工作?我希望我不会错过任何你需要的信息。
答案 0 :(得分:13)
我假设您尝试使用的视图正在使用您在执行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视图中滑动,暂停允许转换看起来很自然。