user2889249
user2889249

Reputation: 909

How to avoid viewWillAppear initially calling

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

Upvotes: 3

Views: 3041

Answers (3)

Sakshi
Sakshi

Reputation: 1

[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
    }
}

Upvotes: 0

user3142969
user3142969

Reputation: 111

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
}

Upvotes: 1

darren102
darren102

Reputation: 2830

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;

}

Upvotes: 8

Related Questions