문제

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

도움이 되었습니까?

해결책

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;

}

다른 팁

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
    }
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top