Force refresh on another ViewController component with swift
I think, you want to add data from oneViewController and without pressing button, you want after navigation or pressing back button, you want to reload automatically tableview to show updated results, right.
If I'm not wrong then, this solution will work for you.
Follow this below steps:
Step 1 :
Add this code in your view controller, from which you want to add data in array or in database, instead of button click.
override func viewDidLoad()
{
super.viewDidLoad()
let backItem = UIBarButtonItem(title: "Back", style: .Plain, target: self, action: "goBack")
self.navigationItem.leftBarButtonItem = backItem
}
func goBack()
{
NSNotificationCenter.defaultCenter().postNotificationName("load", object: nil)
self.navigationController?.popViewControllerAnimated(true)
}
Step 2:
Now its final step.
Now add this below code, to your result view controller, where you want to automatically update the result.
func loadList(notification: NSNotification){
self.TableView.reloadData()
}
override func viewWillAppear(animated: Bool) {
NSNotificationCenter.defaultCenter().addObserver(self, selector: "loadList:",name:"load", object: nil)
}
Now implement this code-stuff in your coding. Hope it works for you.
How to reload tableview from another view controller in swift
You can do this:
In your tableView Controller:
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(loadList), name: NSNotification.Name(rawValue: "load"), object: nil)
}
@objc func loadList(notification: NSNotification){
//load data here
self.tableView.reloadData()
}
Then in the other ViewController :
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "load"), object: nil)
How to reload a view controller when back from another view using tab bar
Follow these steps to handle you view load setup every time your view will appear:
(VC1 = First View Controller)
- Create a new function/method (named
viewLoadSetup
) in your VC1 and move all codes fromviewDidLoad()
toviewLoadSetup()
. Now, Call
viewLoadSetup()
fromviewWillAppear
class VC1: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// viewLoadSetup() you may call it from view did load also
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
viewLoadSetup()
}
func viewLoadSetup(){
// setup view did load here
}
}
If you want to call viewLoadSetup
from viewDidLoad
for one time, when your view controller is loaded, and then after every time from your viewWillAppear
then,
class VC1: UIViewController {
var isLoadingViewController = false
override func viewDidLoad() {
super.viewDidLoad()
isLoadingViewController = true
viewLoadSetup()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if isLoadingViewController {
isLoadingViewController = false
} else {
viewLoadSetup()
}
}
func viewLoadSetup(){
// setup view did load here
}
}
How to reload data in a TableView from a different ViewController in Swift
Xcode 9 • Swift 4
Create a Notification Name:
extension Notification.Name {
static let reload = Notification.Name("reload")
}
You can post a notification
NotificationCenter.default.post(name: .reload, object: nil)
And add an observer at the view controller that you want to reload the data:
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(reloadTableData), name: .reload, object: nil)
}
@objc func reloadTableData(_ notification: Notification) {
tableView.reloadData()
}
SwiftUI: Forcing an Update
Setting currentPage
, as it is a @State
, will reload the whole body.
Reloading a ViewController
You really don't need to do:
[self.view setNeedsDisplay];
Honestly, I think it's "let's hope for the best" type of solution, in this case. There are several approaches to update your UIView
s:
- KVO
- Notifications
- Delegation
Each one has is pros and cons. Depending of what you are updating and what kind of "connection" you have between your business layer (the server connectivity) and the UIViewController
, I can recommend one that would suit your needs.
How can I force my parent ViewController to reload after unwinding from a child?
If you put the refreshing code in viewDidAppear in the ViewController then it should be called any time that the ViewController is displayed. Therefore, it will be shown initially, and after the back button is pressed.
This is different than placing it in viewDidLoad, which will only be called the first time the ViewController is loaded.
Related Topics
Using Nsdate to Get Date for Easter
Add a File Generated by Run Script into The Test Target Compilation List in Xcode
Swift Video to Document Directory
Swift - Nsdate - Remove Part of Date
Parsing JSON from Url Ends Up with an Error - Swift 5
Invalid Update: Invalid Number of Rows in Section 1
Linking Pages in Swift Playgrounds [Xcode]
Applescript Email Attachment Not Working in Handler
Swift Sha256 Encryption Returns Different Encrypted String Compare to Objective C
Swiftui Navigationview Not See Image
Lldb for Swift: Access Computed Property or Perform Function Call in Type Summary Python Script
Can't Dismiss View Controller That's Embedded in a Navigation Controller
Decoding Different Type with and Without Array
Optional Protocol Requirements, I Can't Get It to Work
Dynamically Set Properties from Dictionary<String, Any> in Swift
Siblings Relationship Between Same Models in Vapor