How to do some stuff in viewDidAppear only once?

前端 未结 8 1451
暖寄归人
暖寄归人 2020-11-29 03:25

I want to check the pasteboard and show an alert if it contains specific values when the view appears. I can place the code into viewDidLoad to ensure it\'s onl

8条回答
  •  庸人自扰
    2020-11-29 04:07

    This solution will call viewDidAppear only once throughout the life cycle of the app even if you create the multiple object of the view controller this won't be called after one time. Please refer to the rmaddy's answer above

    You can either perform selector in viewDidLoad or you can use dispatch_once_t in you viewDidAppear. If you find a better solution then please do share with me. This is how I do the stuff.

    - (void)viewDidLoad {
      [super viewDidLoad];
      [self performSelector:@selector(myMethod) withObject:nil afterDelay:2.0];
    }
    
    - (void)viewDidAppear:(BOOL)animated {
      [super viewDidAppear:animated];
      static dispatch_once_t once;
      dispatch_once(&once, ^{
        //your stuff
        [self myMethod];
      });
    }
    

提交回复
热议问题