标签:
页面间传值(RootViewController和subViewController)
方式1: 正向传值和反向传值,在AppDelegate.h文件内定义成员变量[UIApplication sharedApplication].delegate将返回该成员变量的地址,可以进行存值和取值
方式2: 正向传值,直接在第二层视图声明并初始化第一层视图的变量,使用该变量来获取第一层视图的成员变量的值;
反向传值,就是在第二层视图调用第一曾视图的方法,该方法内有第一层视图的成员变量,通过对成员变量赋值,把数据传给第一层视图, 就是说第二层视图要有第一层视图的成员变量.(原因反向传值时,不能通过初始化变量来获取原来视图的地址).
方式3: 使用代理进行反向传值,在第一层视图声明一个block, 在第二层视图实现该block, 在第一层中调用,block就会找到其实现来获取第二层视图中的成员变量,从而实现反向传值.
以下是第2种方式的具体代码实现
/***********************************************************/
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(100, 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;
}
}
}
标签:
原文地址:http://www.cnblogs.com/PaulpauL/p/4774368.html