![](/img/trans.png)
[英]how to make viewDidLoad of SwrevealViewController call only once
[英]How to make viewDidLoad method of ViewController call only once?
我在應用程序中創建了一個容器視圖 。 當用戶將UITableviewCell上的用戶選項卡和其他UIViewController加載到容器視圖中時,容器視圖將加載其他UIViewController 。 我嘗試通過在其他ViewController類上插入日志消息來進行測試,並發現每次調用viewDidLoad和ViewDidAppear方法時都會指出,這些類每次在單元格選項卡上都被實例化。
但是在研究關於蘋果文檔的容器視圖時, https://developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/CreatingCustomContainerViewControllers/CreatingCustomContainerViewControllers.html
我發現UITabBarController也實現ContainerView並在數組中管理其UIViewController 。 我嘗試檢查日志消息,發現viewDidLoad方法僅被調用一次,而當我們第二次訪問該選項卡時,僅被調用viewDidAppear 。 所以我真的很想知道這背后的背后是什么。 他們在使用UnWind segue嗎(我猜不是)。
但是我按照以下教程以以下方式創建了容器視圖控制器: https : //github.com/codepath/ios_guides/wiki/Container-View-Controllers-Quickstart as
class ViewController: UIViewController {
@IBOutlet weak var contentView: UIView!
var activeViewController : UIViewController? {
didSet{
removeInactiveViewController(oldValue)
updateActiveViewController()
}
}
var cellTxtArrs = ["FIRST","SECOND","THIRD"]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
var tblView : UITableView = UITableView()
tblView.frame = CGRectMake(0, 20, 300 ,100)
tblView.separatorColor = UIColor.clearColor()
tblView.scrollEnabled = false
tblView.rowHeight = (30)
self.view.addSubview(tblView)
tblView.delegate = self
tblView.dataSource = self
tblView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "myCell")
}
func removeInactiveViewController(inactiveViewController:UIViewController?){
if let inActiveVC = inactiveViewController{
inActiveVC.willMoveToParentViewController(nil)
inActiveVC.view.removeFromSuperview()
inActiveVC.removeFromParentViewController()
}
}
func updateActiveViewController(){
if let activeVC = activeViewController{
//not necessary
addChildViewController(activeVC)
activeVC.view.frame = contentView.bounds
contentView.addSubview(activeVC.view)
activeVC.didMoveToParentViewController(self)
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
extension ViewController : UITableViewDataSource , UITableViewDelegate{
//MARK: table view data source methods
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
// cell.textLabel?.font = UIFont(name: label.font.fontName, size: 22)
var cell : UITableViewCell = tableView.dequeueReusableCellWithIdentifier("myCell") as! UITableViewCell
cell.backgroundColor = UIColor(red: 0.000, green: 0.400, blue: 0.404, alpha: 1.00)
cell.selectionStyle = UITableViewCellSelectionStyle.None
cell.textLabel?.text = self.cellTxtArrs[indexPath.row]
cell.textLabel?.textAlignment = NSTextAlignment.Left
cell.textLabel?.textColor = UIColor.whiteColor()
return cell
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.cellTxtArrs.count
}
//MARK: - table view delegate methods
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
var row = indexPath.row
switch row {
case 0:
let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let firstVC = storyboard.instantiateViewControllerWithIdentifier("firstVC") as! FirstViewController
activeViewController = firstVC
case 1:
var storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let secondVC = storyboard.instantiateViewControllerWithIdentifier("secondVC") as! SecondViewController
activeViewController = secondVC
case 2:
let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let thirdVC = storyboard.instantiateViewControllerWithIdentifier("thirdVC") as! ThirdViewController
activeViewController = thirdVC
default:
println("no index")
}
}
}
標簽欄控制器具有一系列子控制器,這些子控制器在它們之間切換時會保留。
基本上,您需要做的是:
我在很多地方都使用了此過程。
請注意,這對於在內容視圖中具有導航控制器也很有效,但是導航堆棧也將持久存在,這可能不是期望的結果。 例如,如果您有3個選項卡,並且第一個選項卡導航到第二個視圖控制器,即使切換到第三個選項卡並返回第一個選項卡,它也將保留在第二個視圖控制器上。
分配了非活動視圖控制器后,將它們緩存在一個數組中,您將只調用它們的viewDidLoad
方法一次。
現在之所以調用viewDidLoad是因為您每次都在分配一個新的視圖控制器。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.