2016-04-08 16 views
1

ナビゲーションコントローラにメイン画面が表示されています。巻き戻し後にセグを実行

メイン画面は、他のビューにseguesをトリガするいくつかのボタンを有しています。 1つのボタンがテーブルビューに移動します。

このテーブルの選択は、通常、メインビュー上のボタンのいずれかによって行われるセグエをトリガーします。

私は最初にメイン画面に戻ってから、プログラムで、巻き戻しセグエーからセグをトリガする必要があると仮定していましたが、私はそれが最初にプログラムセグを実行してから巻き戻し、終了する再びメイン画面に表示されます。

このような状況に対処するための正しい方法は何ですか?私はプログラム的にsegueと呼ばれる後にテーブルビューに戻ることができないようにするために、戻るボタンはメインビューに移動する必要があります。

それはもう少し私のユースケースを説明するのに役立ちます場合:テーブルビューには、レベルのリストです。選択すると、テーブルビューで選択したレベルのゲームビューが起動されます。私のメインビューで

enter image description here

マイアンワインドセグエ:

@IBAction func backFromLevelSelectionUnwindSegue(segue:UIStoryboardSegue) { 
    performSegueWithIdentifier("playSegue", sender: self) 
} 

明らかplaySegueはゲームビューコントローラにセグエです。

同様の質問に対する回答は、ブール値フラグを設定してからviewDidAppearでセグを実行することを示唆していますが、viewDidAppearは発生した巻き戻しセグを知る必要がないようです。私が遭遇していない "正しい"解決策はありますか?あなたは、メイン画面にそれを巻き戻すことなく、ゲームビューにセグエを実行するため

答えて

2

あなたのbackFromLevelSelectionUnwindSegueでは、あなたはまだ巻き戻し可能なコンテキストにあります。以下のようなafterDelay:withObject:コンテキストがdispatch_asyncまたはperformSelectorの使用を終了した後、そうperformSegueWithIdentifierを呼び出します。

@IBAction func 
backFromLevelSelectionUnwindSegue(segue:UIStoryboardSegue) { 
    dispatch_async(dispatch_get_main_queue()) { 
     self.performSegueWithIdentifier("playSegue", sender: self) 
    } 
} 
1

それは可能でしょうか?その後、セグを巻き戻したら、テーブルビューに戻るのではなくメインビューに巻き戻しますか?私が正しく覚えていれば、提示しているView Controllerを過ぎ去らせることができます。

+0

戻るボタンだからそれだけでビュー階層のテーブルビューを残すことは珍しいことではありません。表ビューの代わりに、メインビュー – malhal

1

あなたのケースでは、あなたは単にテーブルビューとゲームの間にセグを持っていて、メインVCからtableview VCに渡す必要がある余分なデータがあるはずです。

あなたはUINavigationVCで複数viewControllersが戻って必要な場合は、私がpopToRootViewControllerを使用して見たり、それらの間のアンワインドを使用します。例えば第3のviewControllerからunwindを呼び出し、最初のviewControllerのハンドラを使用します。

+0

に行きますか?これは、viewDidLoadにsegueを実行させるよりも少しクリーンであると言わざるを得ない。 – DudeOnRock

+1

私はそれを違う方法でやってもいいと思いますが、それは心配の良い組織のようです。行をタップすると、そのVCは何をすべきかを決定する必要があります。それはそれを親に戻すべきではありません。 – PeejWeej

0

異なる場所に戻る複数の巻き戻しセグを持つことは可能です。シナリオでは、View ControllerはMain-> LevelSelect-> Game の順に配置されていなければなりません。ゲームが終了すると、2つまたは3つのボタンがあります。最初はexitToGameStartで、プレイヤーは同じレベルで再起動できます。 exitToLevelSelectは、プレイヤーが新しいレベルを選択できるようにします。そして、場合によってはexitToMainMenuが始まりに戻ります。完全な例については、Apple's UnwindSegue sampleを参照してください。具体的には、巻き戻しセグをexitToQuizStartで実行する最後のテーブルの「開始」ボタンと、巻き戻しセグを実行する「Return to The Menu Screen」を参照してください。受信メソッドのコードは以下のとおりです。

QuestionViewController.m

//! Unwinds from the ResultsViewController back to the first 
//! QuestionViewController when the user taps the 'Start Over' button. 
// 
// This is an unwind action. Note that the sender parameter is a 
// 'UIStoryboardSegue*' instead of the usual 'id'. Like all unwind actions, 
// this method is invoked early in the unwind process, before the visual 
// transition. Note that the receiver of this method is the 
// destinationViewController of the segue. Your view controller should use 
// this callback to update its UI before it is redisplayed. 
// 
- (IBAction)exitToQuizStart:(UIStoryboardSegue *)sender 
{ 
    // The user has restarted the quiz. 
    [self.currentQuiz resetQuiz]; 
} 

MainMenuViewController.m

//! Unwinds from the ResultsViewController back to the MainMenuViewController 
//! when the user taps the 'Return to the Home Screen' button. 
// 
// This is an unwind action. Note that the sender parameter is a 
// 'UIStoryboardSegue*' instead of the usual 'id'. Like all unwind actions, 
// this method is invoked early in the unwind process, before the visual 
// transition. Note that the receiver of this method is the 
// destinationViewController of the segue. Your view controller should use 
// this callback to retrieve information from the sourceViewController. Used 
// properly, this method can replace existing delegation techniques for 
// passing information from a detail view controller to a previous view 
// controller in the navigation hierarchy. 
// 
- (IBAction)exitToHomeScreen:(UIStoryboardSegue *)unwindSegue 
{ 
    // Retrieve the score from the ResultsViewController and update the high 
    // score. 
    ResultsViewController *resultVC = (ResultsViewController*)unwindSegue.sourceViewController; 
    self.highScore = MAX(resultVC.currentQuiz.percentageScore, self.highScore); 
} 

注:ストーリーボードを開き、最新のXcodeで古いプロジェクトを構築するために、ファイルインスペクタ、新しいiOSバージョンを選んで、ビルドします。

UnwindSegue Storyboard

関連する問題