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 only invoked once, but the problem is that the alert view shows too quickly. I know I can set a timer to defer the alert's appearance, but it's not a good work-around I think.
I checked the question iOS 7 - Difference between viewDidLoad and viewDidAppear and found that there is one step for checking whether the view exists. So I wonder if there's any api for doing this?
Update: The "only once" means the lifetime of the view controller instance.
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 aboveYou can either perform selector inviewDidLoad
or you can usedispatch_once_t
in youviewDidAppear
. If you find a better solution then please do share with me. This is how I do the stuff.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.
You can use this function in ViewDidLoad method
it will call that function after delay. so you don't have to use any custom timer object. and For once you can use
Hope it helps
By reading other comments (and based on @rmaddy 's answer), I know this is not what OP asked for, but for those who come here because of title of the question:
UPDATE
You should use this method in
viewDidAppear
andviewWillAppear
. (thanks to @rmaddy)UPDATE 2
This method only works with modally presented view controllers and pushed view controllers. it's not working with a childViewController. using
didMoveToParentViewController
would be better with childViewControllers.There is a standard, built-in method you can use for this.
Objective-C:
Swift 3:
The call to
isBeingPresented
is true when a view controller is first being shown as a result of being shown modally.isMovingToParentViewController
is true when a view controller is first being pushed onto the navigation stack. One of the two will be true the first time the view controller appears.No need to deal with
BOOL
ivars or any other trick to track the first call.If I understand your question correctly, you can simply set a BOOL variable to recognize that viewDidAppear has already been called, ex: