Developing iOS about child Uiviewcontroller and parent Uiviewcontroller calling each other method

Source: Internet
Author: User

One of the common requirements for doing iphone development today is to add another viewcontroller to one viewcontroller, while ensuring that the two Viewcontroller interact with each other and invoke methods and functions. Search on the Internet a lot of information, many developers say that the need to use objective-c abnormal delegate, but I feel delegate is used in the two-level between the UIView better, as to whether it can be used in parent-child relationship and is Uiveiwcontroller I am not very clear, and did not personally experiment, by looking at the SDK API and other information I used my own method to achieve the requirements I want, but I do not know if my method will have a fatal problem, or there will be a great disadvantage, If there is an expert in the presence of the words also look pointing, I am just a beginner, I will post my method:

First, define two Uiveiwcontroller, let's first name the viewcontrollerparent (parent container) and viewcontrollchild (child container), we can pass UIView

The Insertsubview method adds a child container to the parent container, which is not to mention in the first place.

Second, let's take a look at the methods and functions in the child container that are invoked through the parent container, and I'll first write the following methods in the child container viewcontrollchild and the parent container viewcontrollerparent respectively:

The method is to eject a warning box

-(void) Alertwindow: (NSString *) transvalue{

Uialertview *alertview=[[uialertview alloc] Initwithtitle:transvalue message:transvalue delegate:self

cancelbuttontitle:@ "OK" otherbuttontitles:nil];

[Alertview show];

[Alertview release];

}

Because the parent container viewcontrollerparent to insert Viewcontrollchild, viewcontrollchild must already be defined in Viewcontrollerparent, as follows:

@synthesize Viewcontrollchild;

if (self. Viewcontrollchild==nil) {

Viewcontrollchild *viewcontroll=[[viewcontrollchild alloc] initwithnibname:@ "Viewcontrollchild" bundle:nil];

Self. Viewcontrollchild=viewcontroll;

[Viewcontroll release];

}

So when the parent container calls the child container, you only have to do one of the following steps:

[Self. Viewcontrollchild alertwindow:@ "I was shot from the container"];

This enables the parent container to invoke the method in the child container;

Finally, look at how to invoke the parent container in the child container, my idea is this, I set the parent controller of the child container at Insertsubview, and finally call the method through the parent controller in the child container. So I added one such method to the viewcontrollchild of the child container:

Sets the parent container for the current window

-(void) Setparentview: (Uiviewcontroller *) viewcontroller{

[Self setparentviewcontroller:viewcontroller];

}

Add the following code before viewcontrollerparent implements Insertsubview:

[Self. Viewcontrollchild Setparentview:self];

This code implements the setting of the parent container

This allows you to invoke the parent container's methods or functions in the child container Viewcontrollchild by using the following code:

[Self.parentviewcontroller alertwindow:@ "I was shot from the parent container"];

The above is to realize Viewcontroller mutual interaction methods and ideas, if there are any mistakes or drawbacks also hope that we can come up with a common discussion

Related Article

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.