为什么 viewDidAppear 没有调用?
why the viewDidAppear is not calling?
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 在 execution.Can 时没有被调用,谁能解释为什么会这样?
你应该调用 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", style: .plain, target: self, 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)
}
}
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 在 execution.Can 时没有被调用,谁能解释为什么会这样?
你应该调用 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", style: .plain, target: self, 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)
}
}