How to Do Some Stuff in Viewdidappear Only Once

Perfom an action in viewDidAppear only when view is first displayed

If you want this code to execute only the first time when the view appears, just add a boolean variable that says "is initialized". If not run the code in viewDidAppear then set it to true.

How can I call the ViewDidAppear only one time?

You can try moving all that functionality in the viewDidLoad: method instead of the viewDidAppear. That one only fires once. Unless there is a reason for you not to...?

EDIT: more code to show what i mean in the comment

in the .h file:

BOOL firstTime;

in the .m file:

-(void)viewDidLoad {
NSLog(@"viewDidLoad actually fired");
//...
firstTime = YES;
}
-(void)viewDidAppear {
//...
if(firstTime){
//show it
firstTime = NO;
}
}

Run method only once (at launch)?

ViewDidLoad will fire every time your view loads, which can be more than once per session (i.e. a memory leak that released views). What I would do is create a boolean instance variable and in your init method set it to true. Then check to see that this boolean var is true before showing your alert view (either from viewWillAppear or viewDidLoad).

For example:

@interface YourViewController : UIViewController {
BOOL showAlert;
}
@end

@implementation YourViewController
- (id) init {
// initiate everything else and add this line
showAlert = true;
return self;
}

- (void) viewDidLoad {
if(showAlert) {
//UIAlertView... blah blah blah, show your view
showAlert = false;
}
}
@end

This ensures that your alert will only be shown once per session. Unless for some reason your ViewController should be released. In which case, you should store this BOOL in the AppDelegate.h class.

How to not perform query on viewDidAppear once a picture has JUST been selected?

You should move your initial query in viewDidLoad(). That only gets called once, so it won't replace your image each time the view appears.

Otherwise, if, for some reason, you need to have it in view did viewDidAppear(), you could use dispatch_once to make sure your query only runs the first time the view appears.

Rough example of using dispatch_once:

var token: dispatch_once_t = 0
dispatch_once(&token) { () -> Void in
// your query goes here
}

If you are using Swift 3, please see this other response on how to get dispatch_once like functionality.

viewDidAppear called twice on the same instance, but only the first time this class loads form NIB

You should never rely on -viewWillAppear:/-viewDidAppear: being called appropriately balanced with the disappear variants. While the system view controllers will do the best they can to always bracket the calls properly, I don't know if they ever guarantee it, and certainly when using custom view controllers you can find situations where these can be called multiple times.

In short, your -viewWillAppear:/-viewDidAppear: methods should be idempotent, meaning if -viewDidAppear: is called twice in a row on your controller, it should behave properly. If you want to load custom views, you may want to do that in -viewDidLoad instead and then simply put the on-screen (if they aren't already) in -viewDidAppear:.

You could also put a breakpoint in your -viewDidAppear: method to see why it's being called twice the first time it shows up.

How to make viewDidLoad method of ViewController call only once?

The tab bar controller has an array of child controllers which persist when switching between them.

Basically what you need to do is:

  • create a container to hold the child controllers (an array)
  • add controller to the array if it does not exist
  • reuse the controller from the array if it exists

I use this procedure in quite a few places.

Note this also works great with having navigation controllers in the content view but the navigation stack will persist as well which may not be a desired result. For instance if you have 3 tabs and the 1st tab is navigated to 2nd view controller it will stay on 2nd view controller even after switching to 3rd tab and back to the 1st one.

Call Function only once in UIViewController, iOS Swift4.2, Xcode10.1

If you want to call that PopView function only once in you App then try this,

In App delegate, set bool value

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
UserDefaults.standard.set(true, forKey: "showPop") // like so
return true
}

Then, in first view controller try this,

  func hasLaunchPop() {
let isshowPop: Bool = UserDefaults.standard.bool(forKey: "showPop")
if isshowPop == true {
popView()
UserDefaults.standard.set(false, forKey: "showPop")
}
}

then in viewdidload call like this,

override func viewDidLoad() {
super.viewDidLoad()
hasLaunchPop()
}

So that PopView appears only once in your app when its launched and will never show up again.

What to do on -init, -viewdidload, -viewdidappear, -viewdiddisapper

Init:

Instantiate any objects that you class will use. Do not add them to the view if they are to be subviews you must do this in viewDidLoad after the view has loaded.

ViewDidLoad:

At this point all you views have been instantiated so you can make any modifications, add subviews etc.

viewDidAppear:

Means what it says. If you want to change a background picture ever 5 seconds, I would start the timer here as you know the view is being seen by the user.

ViewDidDisappear:

The view is not currently being displayed -- so tidy up anything you don't need.

There are lots of other posts that have more detail if you search.

Link to Apple Doc (the first point of call)



Related Topics



Leave a reply



Submit