A small trap for iOS calling Dismissviewcontroller

Source: Internet
Author: User

Our app starts from the start to the main page, is constructed by Presentviewcontroller a viewcontroller sequence, similar to the home page, landing pages, start loading page, home page

Among them, the Viewdidappear method that starts the loading page does a lot of logic processing:

-(void) Viewdidappear: (BOOL) animated{        dispatch_async (Dispatch_get_global_queue (dispatch_queue_priority_ DEFAULT, 0), ^ (void) {                clientinfo = [Ylsclientinfo new];                if ([Clientinfo Needinit]) {            [self mkdiranddatabasefile];        } else{            [self refreshversion:[clientinfo currentversion];        }               Various processing logic    });}

Then after entering the main page, if the user exits the landing, they need to go back to the homepage, so they will call the Dismissviewcontroller method on the homepage. The original code looks like this:

Uiviewcontroller *origin = self.presentingviewcontroller.presentingviewcontroller;if ([Origin isMemberOfClass:[ Ylsloginviewcontroller class]) {    origin = Self.presentingViewController.presentingViewController.presentingViewController;} [Origin Dismissviewcontrolleranimated:no Completion:nil];

The expected result is to go directly to the homepage and then trigger the Viewdidappear method on the homepage. In fact, by observing the console warning, it was found that the Viewdidappear method of the intermediate boot loading page was also called. Landing page because there is no write Viewdidappear method, so there is no discovery, but I guess if there is, the same will be called. It seems that Viewcontroller is sequentially one after the other, so each "previous" Viewcontroller Viewdidappear method should be triggered

Check the API, and StackOverflow search for half a day, there seems to be no way to prevent this default behavior. So finally my solution was to add a tag to the controller on the middle:

-(void) Viewdidappear: (BOOL) animated{        //If this method is triggered by a call to dismiss, do not initialize    if (self.isdismissing) {        return ;    }       Initialize load Logic}

Ylsbootstrapviewcontroller *bootstrapcontroller = (ylsbootstrapviewcontroller*) Self.presentingviewcontroller; bootstrapcontroller.isdismissing = YES;                Uiviewcontroller *origin = self.presentingviewcontroller.presentingviewcontroller;if ([Origin isMemberOfClass:[ Ylsloginviewcontroller class]) {    origin = Self.presentingViewController.presentingViewController.presentingViewController;} [Origin Dismissviewcontrolleranimated:no Completion:nil];

I don't know if we have any better practices.

A small trap for iOS calling Dismissviewcontroller

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.