How to do some stuff in viewDidAppear only once?

前端 未结 8 1426
暖寄归人
暖寄归人 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:13

    You can use this function in ViewDidLoad method

    performSelector:withObject:afterDelay:

    it will call that function after delay. so you don't have to use any custom timer object. and For once you can use

    dispatch_once DCD block.Just performSelector in the dispatch_once block it will call performSelector only once when ViewDidLoad is called

    Hope it helps

    0 讨论(0)
  • 2020-11-29 04:20

    rmaddy's answers is really good but it does not solve the problem when the view controller is the root view controller of a navigation controller and all other containers that do not pass these flags to its child view controller.

    So such situations i find best to use a flag and consume it later on.

    @interface SomeViewController()
    {
        BOOL isfirstAppeareanceExecutionDone;
    }
    @end
    
    @implementation SomeViewController
    
    -(void)viewDidAppear:(BOOL)animated {
        [super viewDidAppear:animated];
        if(isfirstAppeareanceExecutionDone == NO) {
            // Do your stuff
            isfirstAppeareanceExecutionDone = YES;
        }
    }
    
    @end
    
    0 讨论(0)
提交回复
热议问题