Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reload and not reload if press back from different view controllers. 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.

enter image description here

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
like image 206
Pak Ho Cheung Avatar asked Jan 24 '16 04:01

Pak Ho Cheung


3 Answers

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)
}
like image 112
Eendje Avatar answered Sep 29 '22 21:09

Eendje


If displaying vc2 is performed by vc1 and is always sure to invalidate the data in vc1, you could do the following:

  • add a needsReload boolean instance variable to vc1
  • set it to true whenever you display vc2 (and when instanciating vc1 eg in awakeFromNib if coming from a storyboard)
  • only perform the content of loadTable if needsReload is true (maybe refactor this logic into a loadTableIfNeeded)
  • don't forget to set needsReload to false in the end of loadTableIfNeeded

This invalidation pattern is found throughout UIKit, see for example UIView setNeedsLayout/layoutIfNeeded. The advantage is that even if several events cause the data to invalidate, it will only actually get refreshed when you need it.

In your situation it has the additional advantage of keeping the logic contained in vc1 and not creating unnecessary coupling between your VCs, which is always good.

---UPDATE: sample implementation (ObjC but you'll get the idea)

You only need to handle this in VC1, forget about all the back button stuff in VC2. This implementation will mark VC1 for reload as soon as VC2 is presented, but will actually reload only on viewWillAppear, when VC2 is dismissed.

---UPDATE 2: Added a conditional reload based on a delegate callback

Note that _needsReload is now set in the delegate callback, not when VC2 is first presented. Instead we set VC1 as the delegate of VC2. (_needsReload logic is actually unnecessary using this method, kept it for reference)

//VC2: add a delegate to the interface

@class VC2;

@protocol VC2Delegate
- (void) viewController:(VC2*)myVC2 didFinishEditingWithChanges:(BOOL)hasChanges;
@end

@interface VC2
@property (nonatomic, weak) id<VC2Delegate> delegate
@end

@implementation VC2 

- (IBAction) finishWithChanges
{
   [self.delegate viewController:self didFinishEditingWithChanges:YES];
}

- (IBAction) finishWithoutChanges
{
   [self.delegate viewController:self didFinishEditingWithChanges:NO];
}

@end

//VC1: implement the VC2Delegate protocol

@interface VC1 () <VC2Delegate>
@end

@implementation VC1
{
   BOOL _needsReload
}

- (void) awakeFromNib 
{
   //adding this for completeness but the way you did it in Swift (at init) is correct
   [super awakeFromNib];
   _needsReload = YES;
}

- (void) viewWillAppear:(BOOL)animated
{
   [super viewWillAppear:animated];
   [self reloadTableIfNeeded];
}

- (IBAction) displayVC2
{
   VC2* myVC2 = ... //instanciate your VC2 here
   myVC2.delegate = self; //set as the delegate of VC2
   [self presentViewController:myVC2 animated:YES completion:nil];
}

- (void) viewController:(VC2*)myVC2 didFinishEditingWithChanges:(BOOL)hasChanges
{
   _needsReload = hasChanges;
   [self reloadTableIfNeeded];
}

- (void) reloadTableIfNeeded
{
   if (_needsReload) {
      [self.tableView reloadData];
      _needsReload = NO;
   }
}

@end
like image 22
amadour Avatar answered Sep 29 '22 21:09

amadour


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)])
like image 23
rushisangani Avatar answered Sep 29 '22 20:09

rushisangani