Reload and not reload if press back from different view controllers. Swift

reload view controller swift
check if back button is pressed swift
reload tableview from another viewcontroller swift
pass data back to previous view controller swift
reload tab bar controller swift
add back button to view controller swift
swift reload view after dismiss
reload in swift

The top three answers can solve my questions. It is hard to pick which one is the best. So, I just pick the one who is the first to answer my question. Sorry for amateur and iOSEnthusiatic. Thank you for your help. I appreciate it.

ViewController 1 has a table view.

My question is how to reload the table view only if I click back from view controller 2, and not reload the table view if I click back from view controller 3.

Right now, my code for back button is

@IBAction func backButtonTapped(sender: AnyObject) {
    self.dismissViewControllerAnimated(true, completion: nil)
}

In view controller 1. I know that the table view would be reloaded from either view controller 2 or 3

override func viewDidAppear(animated: Bool) {
    loadTable()
}

I tried to put loadTable() in viewDidLoad and try to write the below code for back button in view controller 2. But, it doesn't work.

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let controller = storyboard.instantiateViewControllerWithIdentifier("UserHomePageViewController") as! UserHomePageViewController
controller.viewDidLoad()

Any suggestion what I should do? Thank you for your help.

EDIT:

I think this is an easier way to do it, but it still does not work as I thought. I guess it is because the viewDidAppear is executed before the call of reloadTableBool. Correct? Is there any way to fix it? Thank you. You help would be appreciated.

class 2ViewController
@IBAction func backButtonTapped(sender: AnyObject) {
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let controller = storyboard.instantiateViewControllerWithIdentifier("1ViewController") as! 1ViewController
        print("viewcontroller2 before call: \(controller.reloadTableBool)")
        controller.reloadTableBool = false
        print("viewcontroller2 after call: \(controller.reloadTableBool)")
        self.dismissViewControllerAnimated(true, completion: nil)
    }
class 1ViewController
var reloadTableBool = true
override func viewDidAppear(animated: Bool) {
    print("viewcontroller1: \(reloadTableBool)")
    if reloadTableBool == true {
        loadTable()
    }
}

When I click back on view controller 2, it prints

viewcontroller2 before call: true
viewcontroller2 after call: false
viewcontroller1: true

How to refresh a viewController wh…, Reload and not reload if press back from different view controllers. Swift - ios. It is not advised to call ViewDidLoad or ViewWillAppear by yourself. In the ViewDidLoad include a loadData() function to prepare the data. This is executed in the initial run. When you want to reload, call loadData() again to get the data from model. In a tableView call reloadData() or in a regular view setNeedsDisplay().

Here is a link to a question I answered a couple days ago. Use the navigation controller delegate to handle the back button. In your second view controller, set the delegate to self and reload the tableview when you press the back button.

override func viewDidLoad() {
    super.viewDidLoad()

    navigationController?.delegate = self
}

func navigationController(navigationController: UINavigationController, willShowViewController viewController: UIViewController, animated: Bool) {
    if let controller = viewController as? FirstViewController {
        controller.tableView.reloadData()
    }
}

NOTE: I'm assuming you're using the back button of the navigation controller here.

EDIT: Another example using your manually added back button:

@IBAction func backButtonTapped(sender: AnyObject) {

    if let viewControllers = app.window?.rootViewController?.childViewControllers {
        viewControllers.forEach { ($0 as? FirstViewController)?.tableView.reloadData() }
    }

    self.dismissViewControllerAnimated(true, completion: nil)
}

Seeing as you are using a navigation controller:

@IBAction func backButtonTapped(sender: AnyObject) {

    navigationController?.viewControllers.forEach { ($0 as? FirstViewController)?.tableView.reloadData() }

    self.dismissViewControllerAnimated(true, completion: nil)
}

Reload data in ViewController???, I have 5 tab bars, when I press one of them I want the viewController's data to be is there another way to refresh the data? https://stackoverflow.com/questions/​45204146/swift-selected-tab-bar-index-not-triggering-scroll-to-top I would suggest doing your reload in viewWillAppear of your collection view's view controller. Let’s go back to storyboards vs. view controllers. In the previous section, we’ve discussed how view controllers affect the navigation and interactions in your app. Most apps will need to use container view controllers, such as UINavigationController, to facilitate navigation and flow of the app.

You can use notification approach easily for this.

Add observer in your 1st ViewController in viewDidLoad method.

NSNotificationCenter.defaultCenter().addObserver(self, selector: "reloadTable:", name: "reloadTable", object: nil)

func reloadTable(notification : NSNotification){


let isReload : NSNumber = notification.userInfo!["isReload"] as! NSNumber

    if (isReload.boolValue) {
        self.tableView.reloadData()
    }
}

Then post notification like this from your 2nd and 3rd ViewController respectively when you call dismissViewController.

// From 2nd viewcontroller

NSNotificationCenter.defaultCenter().postNotificationName("reloadTable", object: nil, userInfo: ["isReload" : NSNumber(bool: false)])

// From 3rd viewcontroller
NSNotificationCenter.defaultCenter().postNotificationName("reloadTable", object: nil, userInfo: ["isReload" : NSNumber(bool: true)])

How to reload second view controller in a tab view. : swift, to exit back to previusly Storyboard and this works grate, but how to I reload it's data? I have written stuff to memory that I want to reload. Swift. Asked 3 years ago It depends on what view the view controller is using. UITableView No UITableView just an UIView that I want to retrive UserDefaults.standard values. Posted 3  If you have a value in one view controller and want to pass it to another, there are two approaches: for passing data forward you should communicate using properties, and for passing data backwards you can either use a delegate or a block.

Reload tableview data from different viewController swift file : swift, I have a tabbar setup with two different view controllers. change to the data, then navigates back to VC2 from the tab bar, it does NOT reload with the new data  You can use this method to refresh the custom input view or input accessory view associated with the current object when it is the first responder. The views are replaced immediately—that is, without animating them into place. If the current object is not the first responder, this method has no effect.

How to reload the original view controller / view ? · Issue #56 , I have this code which reloads on start and also have the same code when in item is view i have is a popover so i can't have it reload when the tableview is back in Suppose you've got view controller a (of class A), which is the one with the attachment for one particular shader, but I don't want to use it at other times. [Converted to Swift 2.0 -- SJL 9/17/15 ] Navigation controllers are the workhorse of organizing view controllers. I've covered much of their use in other posts about MVC, segues and delegates. In this chapter, We’ll go through some of the Swift code for the Navigation controller. The View Controller Stack Navigation view controllers are stack…

php with out reload page Code Example, GitHub is home to over 50 million developers working together to host and Expected to see the original view controller layout after the end of the various Alternatively I put the Dispatch call into a button press (button Swift version: 4.1 to recover original state, when the content has not been modified  An easy way of presenting view controllers in different storyboards. Introduction. Storyboards present a convenient and easy way of designing view controllers, but they have their caveats also. As an app grows in complexity and requires more views, it becomes increasingly difficult to hold them all in a single storyboard.

Comments
  • Are you using the back button you get from the navigation controller or did you created one and added it manually?
  • I drag a Bar Button Item to navigation bar and create a function for that button as "back". I guess I create it manually.
  • In your screenshots, it shows you're using a navigation controller. Why don't you use the build in back button?
  • Um. Not sure how to use the build in one. LOL
  • Eh, it's kinda automatic :p Either way, I've updated my answer.
  • I did that too but it seems viewDidAppear is executed before the call of reloadTableBool. Would you mind to take a look on my update?
  • I got it. Work. Thank you for your help:)
  • Good news. Would have been even better news if you had accepted my answer, but still :-)
  • One more question. If I want to assign a value from display2 in order to tell display1 whether the table view should reload like what I try to code in EDIT, is it possible?
  • Yes it is possible, for example using the delegate pattern. See my update. Hope you won't have too much trouble Swifting it.
  • Hi. Would you mind to see my edit? I think it is easier way to do it as I can just use a Bool, but it seems viewDidAppear is executed before the call of reloadTableBool?
  • Actually my approach should be the easiest and arguably cleanest compared to having have to keep in mind to send a Bool every time you navigate back to your first view. Did any of my suggestions worked? I'll look at your edit now.