Question

I want to know something aboutViewWillAppear.I have a viewwillappar method for data refreshing. What I want to do is when this viewcontroller push from the previous one this refreshing should not be happen. (when initially loading this controller viewwillappear should not be call). Is this possible? If so how can I do that?

Please help me Thanks

Was it helpful?

Solution

viewWillAppear will always be called when the view appears

You can use an instance variable to make sure it is not called the first time i.e.

    @implmentation ViewController {
   BOOL _firstLoad
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    _firstLoad = YES;
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    if (!_firstLoad) {
      // do what you want to do when it is not the first load
    }
    _firstLoad = NO;

}

OTHER TIPS

This is a example using swift 4.

var isLoadedFirstTime = false
override func viewDidLoad() {
    super.viewDidLoad()
    isLoadedFirstTime = true
}

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    if !isLoadedFirstTime {
      // Do what you want to do when it is not the first load
    }
    isLoadedFirstTime = false
}

[updated solution] The example using swift 5:

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    if !isMovingFromParent {
       // Do what you want to do when it is not the first load
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top