Re: [問題] dismissViewController

作者: DLMC (永保安康)   2014-02-26 21:50:26
※ 引述《y03131003 (Jimmy)》之銘言:
: 我有A, B, C, D 四個viewController
: A 是 rootViewController(TabBarViewController)
: B 是 用AVFoundation寫的相機
: A 用 modal 的方式叫出了 B
: B 用 modal 的方式叫出了 C
: C 用 modal 的方式叫出了 D
: 我想在D裡按一個按鈕回到A
: 一開始是在storyboard裡直接拉一個segue到A
: 然後performSegueWithIdentifier
: 不過這樣似乎有memory leak的問題,多重複幾次這個步驟記憶體就直線上升
: 最後導致當機
: 後來查了一下發現要回到 A 只要在 D裡取得rootViewController
: 然後rootViewController呼叫 dismissViewController 就行了
: 如下
: [[[[UIApplication sharedApplication] keyWindow] rootViewController]
: dismissViewControllerAnimated:NO completion:nil];
: 這樣的確可以成功,可是按下按鈕後大概要等2秒才會回到A..
: 請問有辦法按下的瞬間就回到A嗎?
: 用performSegue的方法可以瞬間,但是多重複幾次就當機了...
前三次用 modal 的方式叫 view controller 出來,階層是:
A > B(相機) > C > D
接下來在 D 用了 performSegueWithIdentifier:sender: 這個方法叫了 A,
並不是一路從 D 回到 A,
反而是 A > B(相機) > C > D > A
所以只要多跑幾次,當然會掛掉(尤其 B 是相機)。
參考 UIViewController 的官方文件,http://bit.ly/1iu7EOn
在 dismissViewControllerAnimated:completion: 有提到:
If you present several view controllers in succession, thus building a stack
of presented view controllers, calling this method on a view controller lower
in the stack dismisses its immediate child view controller and all view
controllers above that child on the stack.
你用的方法正是文件中所提,只是要確保你找到的 rootViewController 就是A。
(如果D被用在別處,那這個 rootViewController 或許就不是A了)
有一個繞路的方法,乾脆將它們的嵌在 naivgation controller 裡面,
然後將 navigation bar 重頭到尾都藏起來(如果你不想要它的話)。
如此一來,想回到最前面,只要使用 self.navigationController 的
popToRootViewControllerAnimated: 方法就好。
補充:po完文後想了一下,用 navigation controller 的話,轉換 view controller 的
動畫效果可能就不是原po想要的,需要自訂轉場動畫了
另外,就如推文的網友們所提,B這個用到相機的 view controller 相當吃資源,
可以的話,用完就先關掉會比較好。
作者: y03131003 (Jimmy)   2014-02-27 00:51:00
我試試看用navigation 謝謝!!

Links booklink

Contact Us: admin [ a t ] ucptt.com