2012-05-05 29 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

私はあなたがセグをする直前に取得した画像を使用しようとしていると仮定していますか?彼らがイメージピッカーから取り消したら、あなたはまだセグをしたいですか?

画像が必要な場合は、デリゲートコール「ピッキングが完了しました」の後にあなたのセグを呼び出す必要があります。

セグエが原因アニメーションはまだここから発生するかもしれ発射ない問題:

[[picker presentingViewController] dismissModalViewControllerAnimated:NO]; 

をしたり、アニメーションを維持したい場合は、移動:

[[picker presentingViewController] dismissModalViewControllerAnimated:YES]; 

あなたは試すことができます"picker did finish"メソッドへのセグをこのようにする:

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

またはそれがnの場合私は頻繁にこの移行を使用して、それがAになり

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

}); 

:これはモーダルビューを呼び出して、コントローラではなく、モーダルビュー自体にデリゲートとして実装する必要があります - OT作業はpickerdidfinish法(ノートで、このアプローチを試してみてくださいモーダルビューですっきりと落としてから、セグビューでスライドさせると、一時停止によって自然に見えるようになります。

+0

あなたは、私がdismissModalViewControllerAnimatedを設定したときにそれは動作しますが、私のポイントを得た:NO ...しかし、私はアニメーションを維持するためにあなたのコードを使用している場合、XCodeのは、私は、このエラーを与える:インスタンスメッセージの 」のUIViewは、セレクタでメソッドを宣言しませんdismissmodalviewcontrolleranimated:completion " –

+0

ok、新しいコードを打ち込みましょう。これは、アニメーションがセグの間で完了できるようにわずかなタイムアウトを提供します。 – CocoaEv

+0

ああ、これは醜いです! (あなたの解決策ではなく、segueアニメーションがこの問題を引き起こす方法)。最も簡単でクリーンな方法は、単にアニメーションを無効にすることです。ありがとう、+1 :) – Jimmy