ios – 为什么viewDidAppear没有调用?

import UIKit

import SwiftyDropBox

class NotesViewController: UIViewController,UITableViewDataSource,UITableViewDelegate,NotesviewDelegate {

var userText:[String] = []

@IBOutlet weak var tableView: UITableView!

override func viewDidLoad() {
    super.viewDidLoad()

    print("View Did Load Notes view controller")
}

override func viewWillAppear(_ animated: Bool) {
    print("View will Appear")
    self.tabBarController?.navigationItem.title = "Notes"

    let sendButton = UIBarButtonItem(title: "New",style: .plain,target: self,action: #selector(goToNoteEditorViewController))

    self.tabBarController?.navigationItem.rightBarButtonItem = sendButton
    self.automaticallyAdjustsScrollViewInsets = false
}

override func viewDidAppear(_ animated: Bool) {

    print("View Did appear Notes view controller")
    didDoneButtonpressed()

}

func goToNoteEditorViewController() {

    let storyboard = UIStoryboard(name: "Main",bundle: nil)

    guard let vc = storyboard.instantiateViewController(withIdentifier:"NoteEditorViewID") as? NoteEditorViewController else{
        return
    }
    vc.delegate = self
    self.navigationController?.present(vc,animated: true,completion: nil)

}


public func tableView(_ tableView: UITableView,numberOfRowsInSection section: Int) -> Int{

    return userText.count
}


public func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell{

    let cell = UITableViewCell(style: UITableViewCellStyle.default,reuseIdentifier: "cell")
    cell.textLabel?.text = userText[indexPath.row]
    print("The cell is \(cell)")
    return (cell)
}

public func tableView(_ tableView: UITableView,didSelectRowAt indexPath: IndexPath){

    let controller = UIStoryboard(name: "Main",bundle: nil)

    guard let viewController = controller.instantiateViewController(withIdentifier:"TextdisplayID") as? TextdisplayViewController else{
        return
    }

    let indexPath = tableView.indexPathForSelectedRow

    let currentCell = tableView.cellForRow(at: indexPath!)!

    let text = currentCell.textLabel!.text ?? ""
    print(currentCell.textLabel!.text ?? "")

    viewController.textTitle = text
    navigationController?.pushViewController(viewController,animated: true)
}

func didDoneButtonpressed() {

    let userDefaults = UserDefaults()

    if let dictionary = userDefaults.dictionary(forKey: "UserNotes"){
        if let dictionaryTexts = dictionary as? [String : String] {
            userText = [String](dictionaryTexts.keys)

            print("array values: \(userText)")
            self.tableView.reloadData()
        }
    }
}
}

在上面的程序中,viewDidAppear在执行时没有调用.任何人都可以解释为什么会发生这种情况?

解决方法

你应该调用super.viewWillAppear和super.viewDidAppear()
override func viewWillAppear(_ animated: Bool) {
  super.viewWillAppear(animated: animated)
  print("View will Appear")
  self.tabBarController?.navigationItem.title = "Notes"

  let sendButton = UIBarButtonItem(title: "New",action: #selector(goToNoteEditorViewController))

  self.tabBarController?.navigationItem.rightBarButtonItem = sendButton
  self.automaticallyAdjustsScrollViewInsets = false
}

override func viewDidAppear(_ animated: Bool) {
  super.viewDidAppear(animated: animated)
  print("View Did appear Notes view controller")
  didDoneButtonpressed()

}

编辑

看来你已经将UITabBarController子类化了.在你的TabBarController中,你需要调用super.viewWillAppear()和super.viewDidAppear

class YourCustomTabBarController : UITabBarController {
   override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
  }

  override func viewWilldisappear(_ animated: Bool) {
    super.viewWilldisappear(animated)
  }
}

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...