Transfer values between pages (Rootviewcontroller and Subviewcontroller)
Mode 1: Forward and reverse values, define member variables within the AppDelegate.h file [UIApplication Sharedapplication].delegate will return the address of the member variable, can be stored value and value
Mode 2: Forward value, directly in the second layer of view Declaration and initialization of the first-level view of the variable, using the variable to obtain the value of the member variable of the first layer view;
The reverse value is the method that invokes the first view in the second layer view, which has the member variables of the first layer view, assigns the data to the first layer view by assigning values to the member variables, that is, the second layer view must have the member variables of the first layer view. (You cannot initialize a variable to get the address of the original view when the reverse value is passed).
Mode 3: Using the proxy for the reverse value, in the first layer of view declaration of a block, in the second layer of view implementation of the block, called in the first layer, the block will find its implementation to get the second layer of the member variables in the view, so as to achieve the reverse value.
The following is a specific code implementation of the 2nd approach
/***********************************************************/
RootViewController.h
/***********************************************************/
#import <UIKit/UIKit.h>
@interface Rootviewcontroller:uiviewcontroller
@property (nonatomic, copy) NSString *string;
-(void) Sendcontent: (NSString *) str;
@end
/***********************************************************/
Rootviewcontroller.m
/***********************************************************/
#import "RootViewController.h"
#import "SubViewController.h"
@interface Rootviewcontroller ()
@property (nonatomic, strong) Subviewcontroller *sub;
@end
@implementation Rootviewcontroller
-(void) Sendcontent: (NSString *) str
{
_STRING=STR;
}
-(void) Viewdidload {
[Super Viewdidload];
Do any additional setup after loading the view.
UIButton *button=[[uibutton Alloc] Initwithframe:cgrectmake (100, 80, 60, 30)];
[Button settitle:@ "Click1" forstate:uicontrolstatenormal];
Button.backgroundcolor=[uicolor Lightgraycolor];
[Button addtarget:self action: @selector (Btnclick:) forcontrolevents:uicontroleventtouchupinside];
[Self.view Addsubview:button];
_string=button.titlelabel.text;
UILabel *label=[[uilabel Alloc] Initwithframe:cgrectmake (100, 120, 200, 30)];
label.tag=12;
[Self.view Addsubview:label];
_sub=[[subviewcontroller alloc] init];
}
-(void) Btnclick: (UIButton *) sender
{
[Self presentviewcontroller:_sub animated:yes completion:nil];
}
-(void) Viewdidappear: (BOOL) animated
{
[Super viewwillappear:animated];
UILabel *la= (UILabel *) [Self.view viewwithtag:12];
La.text = _string;
NSLog (@ "%@", _string);
}
/***********************************************************/
SubViewController.h
/***********************************************************/
#import <UIKit/UIKit.h>
@interface Subviewcontroller:uiviewcontroller
@property (nonatomic, copy) NSString *content;
@end
/***********************************************************/
Subviewcontroller.m
/***********************************************************/
#import "SubViewController.h"
#import "RootViewController.h"
@interface Subviewcontroller ()
@end
@implementation Subviewcontroller
-(void) viewdidload
{
[Super Viewdidload];
Do any additional setup after loading the view.
for (int i=1; i<4; i++)
{
UIButton *button=[[uibutton alloc] Initwithframe:cgrectmake (50+i*40, 60, 30)];
[Button Settitlecolor:[uicolor Redcolor] forstate:uicontrolstateselected];
[Button settitle:[nsstring stringwithformat:@ "click%d", I] forstate:uicontrolstatenormal];
Button.tag=i;
[Button addtarget:self action: @selector (Btnclick:) forcontrolevents:uicontroleventtouchupinside];
Button.backgroundcolor=[uicolor Lightgraycolor];
[Self.view Addsubview:button];
}
}
-(void) Btnclick: (UIButton *) sender
{
[Self dismissviewcontrolleranimated:yes completion:nil];
Rootviewcontroller *root= (Rootviewcontroller *) Self.presentingviewcontroller;
_content=sender.titlelabel.text;
[Root sendcontent:_content];
}
-(void) Viewwillappear: (BOOL) animated
{
[Super viewwillappear:animated];
Rootviewcontroller *root= (Rootviewcontroller *) Self.presentingviewcontroller;
_content=[root string];
for (int i=1; i<4; i++)
{
UIButton *btn= (UIButton *) [Self.view viewwithtag:i];
if ([Btn.titleLabel.text isequaltostring:_content])
{
Btn.selected=yes;
}
Else
{
Btn.selected=no;
}
}
}
It's a good headache for the interface to pass the whole value.