Reboot and do not reboot if you press back from different controllers. swift

The top three answers can solve my questions. It's hard to choose which one is the best. So, I just choose the one who answers my question first. Sorry for the amateur and enthusiastic iOS. Thanks for the 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 the return button is

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

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

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

loadTable() viewDidLoad "" 2. .

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

, ? .

EDIT:

, , , . , , viewDidAppear reloadTableBool. ? ? . .

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

2,

viewcontroller2 before call: true
viewcontroller2 after call: false
viewcontroller1: true
+6
3

vc2 vc1 vc1, :

  • needsReload boolean vc1
  • true, vc2 ( instanciating vc1, , awakeFromNib, )
  • loadTable, requireReload (, loadTableIfNeeded)
  • needsReload false loadTableIfNeeded

UIKit, ., , UIView setNeedsLayout/layoutIfNeeded. , , , .

, vc1, VC, .

--- UPDATE: (ObjC, )

VC1, back VC2. VC1 , VC2 , viewWillAppear, VC2 .

--- UPDATE 2: ​​

, _needsReload , VC2 . VC1 VC2. (_needsReload , )

//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
+2

, . "". , "".

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

: , .

EDIT: :

@IBAction func backButtonTapped(sender: AnyObject) {

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

    self.dismissViewControllerAnimated(true, completion: nil)
}

, :

@IBAction func backButtonTapped(sender: AnyObject) {

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

    self.dismissViewControllerAnimated(true, completion: nil)
}
+3

.

1- ViewController viewDidLoad.

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

, , ViewController, , rejectViewController.

// 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)])
+1

Source: https://habr.com/ru/post/1625684/


All Articles