Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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()
        }
    }
}
}

In the above program the viewDidAppear is not call at the time of execution.Can anyone explain why this happen?

like image 209
Sathish Kumar Gurunathan Avatar asked May 11 '17 05:05

Sathish Kumar Gurunathan


People also ask

Can viewDidAppear be called multiple times?

viewWillAppear(_:) is called every time the view appears. In this simple app, that means it is only called once. However, imagine your app has several views, and you can move between them. viewWillAppear(_:) is called every time your view is displayed.

When viewWillAppear is called?

viewwillappear method is called as and when the view controller's view is added to the window. ( if the view is already in the window and is hidden by another view, this method is called when the view is once again revealed). The method is a notification to the view controller that the view is about to become visible.

Why does viewWillAppear not get called when an app comes back from the background?

The Simple Answer The technical reason for when viewWillAppear gets called is simple. Notifies the view controller that its view is about to be added to a view hierarchy. It can't be any view hierarchy — it has to be the one with a UIWindow at the root (not necessarily the visible window).

What happens between viewWillAppear and viewDidAppear?

There is a noticeable pause after row selection and before the new view is pushed. Some logging indicates that all of my code is reasonably quick, from row selection until the pushed controller's viewWillAppear . But then the time between viewWillAppear and viewDidAppear is logged at around 0.7 seconds.


Video Answer


1 Answers

You should call super.viewWillAppear and 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
}

Also

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

}

EDIT

It seems you've subclassed UITabBarController. In your TabBarController, you need to call super.viewWillAppear() and super.viewDidAppear

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

  override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
  }
}
like image 168
saurabh Avatar answered Nov 15 '22 21:11

saurabh