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