2012-05-05 64 views
3

我有這個代碼,當按下按鈕時加載一個新的UIView(從故事板)。 goPressed函數在按下按鈕時觸發,它調用selectImage函數。 selectImage打開一個UIImagePickerController並讓用戶選擇一張照片。在用戶選擇照片後,didFinishPickingMediaWithInfo委託將所選圖像添加到UIImageView。performSegueWithIdentifier無法正常工作

在'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不起作用?我希望我不會錯過任何你需要的信息。

回答

13

我假設您試圖繼續使用的視圖正在使用您剛剛進行搜索之前得到的圖片?如果他們從圖像選擇器中取消,你還想繼續嗎?

如果它需要圖片,那麼也許你應該在代表電話「完成採摘」之後調用你的segue。

與SEGUE不觸發原因可能是動畫還是從這裏發生的問題:

[[picker presentingViewController] dismissModalViewControllerAnimated:YES]; 

你可以試試:

[[picker presentingViewController] dismissModalViewControllerAnimated:NO]; 

,或者如果你想保持動畫,移動該SEGUE到「選擇器沒有完成」的方法,做這樣說:

[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]; 

}); 

我經常使用這一過渡,它使隨着模態視圖移動,然後在賽格視圖中滑動,並且暫停可以讓過渡看起來很自然。

+0

你讓我的點,它的工作原理,當我設置dismissModalViewControllerAnimated:NO ...但如果我用你的代碼,以保持動畫的XCode給我這個錯誤: 「的UIView例如消息不聲明與選擇的方法dismissmodalviewcontrolleranimated:completion「 –

+0

好的,給新代碼一個鏡頭。它提供了一個輕微的超時時間,以允許在賽段之間完成動畫。 – CocoaEv

+0

呃,這很醜! (不是你的解決方案,但是繼續動畫會導致這個問題)。看起來最簡單和最乾淨的方法就是禁用動畫。謝謝,+1 :) – Jimmy