Addchildviewcontroller and Viewwillappear, viewdidappear relationship Description

Source: Internet
Author: User

The most recent project in the use of Addchildviewcontroller encountered a very strange problem, we use the following, in the Parentviewcontroller viewwillappear request data, After the request data succeeds, add the child view controller using the following code:

[Self.view AddSubview:childViewController.view]; [Self addchildviewcontroller:childviewcontroller];

A strange phenomenon occurs when the Viewwillappear method of Childviewcontroller is called once, and the Viewdidappear method calls two consecutive times. When we change the code to add the child view controller to the following code:

[Self addchildviewcontroller:childviewcontroller]; [Self.view AddSubview:childViewController.view];

There is also a strange phenomenon that Childviewcontroller's Viewwillappear method is not called, and the Viewdidappear method is called once.

After some research, we finally found the reason. When Childviewcontroller is not added to any parent view controller, Viewwillappear and viewdidappear are called normally if the Childviewcontroller view is added to the other views. However, when Childviewcontroller is added to a parent view controller, Viewwillappear and Viewdidappear are synchronized with the Viewwillappear and Viewdidappear events of the parent view controller.

So in the first case in the above code, the first call to Addsubview,viewwillappear and Viewdidappear will be called once, and then Addchildviewcontroller, with the parent view controller's event synchronization, That is, when the parent view controller's viewdidappear is called, the Childviewcontroller Viewdidappear method is called again. So the Viewdidappear method was called two times.

In the second case, the event that calls Addchildviewcontroller,childviewcontroller first synchronizes with the parent view controller when the parent view controller's viewdidappear is called. The Viewdidappear method of the Childviewcontroller is called once, and then Addsubview does not trigger Viewwillappear and viewdidappear.

Therefore, the solution to the above problem is very simple, put the code of the request data into the viewdidappear.


If you feel that you can help, but also hope to help the top, thank you:)
Personal blog: http://blog.csdn.net/zhaoxy2850
This address: http://blog.csdn.net/zhaoxy_thu/article/details/50826190
Reprint please indicate the source, thank you!

Addchildviewcontroller and Viewwillappear, viewdidappear relationship Description

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.