2016-12-30 8 views
3

戻ら場合、私は私が何をしたいの例をあげる再びそれらをリロードせずにビューを移動するには:どのようにそれはそう混乱はありません、私はスウィフト

例:

のは、私が地図を持っているとしましょうこれは、ユーザーが3つの注釈を動的にスクロールするたびに追加されます。今私はマップの下にボタンがあります。私はそれを押して別のviewControllerに行きます。私が望むことをして、mapを使ってviewControllerに戻ってきたら、私のマップが持っていたすべての注釈を見つけて、すべて。

私はviewControllers間を移動させ、この機能を使用するために使用:私は彼らにviewDidloadを呼び出して表示されviewcontrollerを使用する場合

let vc = self.storyboard?.instantiateViewControllerWithIdentifier("view") as? MyViewcontroller 
     self.presentViewController(vc!, animated: true, completion: nil) 

これら2:私もこれを試してみました

func move(identifier: String , viewController : UIViewController) -> Void { 
    let mstoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil) 
    let vc: UIViewController = mstoryboard.instantiateViewControllerWithIdentifier(identifier) 
    viewController.presentViewController(vc, animated: true, completion: nil) 
} 

をそのように、それは初めて登場しました。

別の例では、タブ何もリロードをナビゲートするときに(と呼ばれている唯一の機能がviewDidAppearある)

EDIT

test file

+0

もしあなたが 'presentViewController'を使っているのであれば、そのコントローラ上で' dismiss(animated:true、completion:nil) 'を実行して、前のものにリロードせずに戻ることができます – mursang

答えて

1

あなたが投稿同じプロジェクトによると、バックビュー1からビュー2から行くとき、あなたは新しいUIViewControllerをインスタンス化し、あなたのviewDidLoadが再び呼び出されると、あなたの全体のマップビューがリロードされる理由です。あなたのサンプルプロジェクトで

、代わりの

lazy var mapController2 = {() -> UIViewController in 
    let mstoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil) 
    return mstoryboard.instantiateViewController(withIdentifier: "first") 
} 

は、あなただけのボタンを押し上ごビュー2を却下すべきです。

@IBAction func butto(_ sender: AnyObject) { 
     //Your initial code 
     //PresentingController().moveToMap(self, flag: 1) 

     self.dismiss(animated: true, completion: nil) 
} 

新しいUIViewControllerを提示

は、 UIViewController古いメモリから削除されていない、それはちょうど新しい UIViewControllerの背後に隠されています。ですから、以前の状態に バック UIViewControllerに行くことを希望する時はいつでもあなたはその2番目の UIViewController上で実行いくつかのタスクを実行している場合あなたがする必要があるすべては、しかし UIViewController

近い新しいもので、維持最初のUIViewControllerに反映させたい場合は、最初にUIViewControllerを更新するようにクロージャを設定する必要があります。

+0

あなたの答えをありがとう私はそれを試してみるとすぐに結果を教えて戻ってくるだろう –

+0

ありがとうRikhその仕事私欲しい –

2

に気付いた場合に問題があるという事実によって引き起こされるtabBarViewControllerですマップコントローラは、もう一方のコントローラにナビゲートするときに割り当てが解除され、再度マップ画面に移動するときに別のコントローラが作成されます。

必要なのは、同じコントローラインスタンスを保持し、それを提示することです。提示しているコントローラに強い参照を残しておけば十分です。

class PresentingController { 
    // making the property lazy will result in the getter code 
    // being executed only when asked the first time 
    lazy var mapController = {() -> UIViewController in 
     let mstoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil) 
     return mstoryboard.instantiateViewControllerWithIdentifier("mapControllerIdentifier") 
    }() 

    func moveToMap() { 
     // simply use the mapController property 
     // the property reference will make sure the controller won't 
     // get deallocated, so every time you navigate to that screen 
     // you'll get the same controller 
     presentViewController(mapController, animated: true, completion: nil) 
    } 
} 
+0

あなたの答えと申し訳ありません私はあなたのコードを取りましたが、私は2つのエラーがあります。最初に現在のコンテキストでlazy varとclosureの戻り値の型を推論することができません。私はすぐに2を使用します –

+0

@mikevorisisクロージャの戻り値の型を指定してコードを更新しました。コードが今動作するかどうかを確認してください – Cristik

+0

今すぐテストすることはできませんが、私がすれば通知します –

関連する問題