問題描述
我有這段代碼在按下按鈕時加載一個新的 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 不起作用的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!