問(wèn)題描述
我有這段代碼在按下按鈕時(shí)加載一個(gè)新的 UIView(來(lái)自情節(jié)提要).goPressed 函數(shù)在按下按鈕時(shí)觸發(fā),它調(diào)用 selectImage 函數(shù).selectImage 打開一個(gè) 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' 中,執(zhí)行 selectImage 后,它應(yīng)該在注釋為//1 處執(zhí)行 Segue.但什么也沒(méi)有發(fā)生.performSegueWithIdentifier 似乎不起作用.如果我在調(diào)用 performSegueWithIdentifier 之前不調(diào)用 [self selectImage],它會(huì)起作用.代碼如下:
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;
}
請(qǐng)幫忙,為什么 performSegueWithIdentifier 不起作用?我希望我沒(méi)有遺漏您需要的任何信息.
Please help, why performSegueWithIdentifier isn't working? I hope I am not missing any information you need.
推薦答案
我假設(shè)您嘗試轉(zhuǎn)場(chǎng)的視圖是使用您在轉(zhuǎn)場(chǎng)之前獲得的圖片?如果他們從圖像選擇器中取消,你還想繼續(xù)嗎?
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?
如果它需要圖片,那么也許你應(yīng)該在代表調(diào)用確實(shí)完成挑選"之后調(diào)用你的 segue.
If it needs the picture, then maybe you should call your segue after the delegate call "did finish picking".
segue 沒(méi)有觸發(fā)的問(wèn)題可能是由于動(dòng)畫仍然從這里發(fā)生:
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];
或者如果你想保持動(dòng)畫,將segue移動(dòng)到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];
}];
或者如果這不起作用,請(qǐng)?jiān)?pickerdidfinish 方法中嘗試這種方法(注意 - 這應(yīng)該作為調(diào)用模態(tài)視圖的控制器中的委托來(lái)實(shí)現(xiàn),而不是模態(tài)視圖本身:
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];
});
我經(jīng)常使用這種過(guò)渡,它會(huì)在模態(tài)視圖中很好地下降,然后在 segue 視圖中滑動(dòng),暫停讓過(guò)渡看起來(lái)很自然.
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.
這篇關(guān)于performSegueWithIdentifier 不起作用的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!