单例---视图间数据的传递:标签显示输入的内容【多个视图中】

RootViewController.m

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor greenColor];
    
    //创建显示文字的label
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(50, 100, 200, 40)];
    label.tag = 102;
    label.backgroundColor = [UIColor grayColor];
    [self.view addSubview:label];
    [label release];
    
    //添加按钮
    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    button.frame = CGRectMake(20, 20, 90, 60);
    [button setTitle:@"打开模态" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    
    
}

- (void)viewWillAppear:(BOOL)animated {

    
     UILabel *label = (UILabel *)[self.view viewWithTag:102];
    
    //创建单例对象
    BackData *backData = [BackData shareData];
    
    label.text = backData.text;
    
    [super viewWillAppear:animated];
}

- (void)buttonAction {

    ModalViewController *modalCtrl = [[[ModalViewController alloc] init] autorelease];
    
    modalCtrl.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    
    [self presentViewController:modalCtrl animated:YES completion:NULL];
    
}
ModalViewController.m

#import "BackData.h"

@interface ModalViewController ()

@end

@implementation ModalViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor redColor];
    
    //添加按钮
    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    button.frame = CGRectMake(20, 20, 90, 60);
    [button setTitle:@"关闭模态" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    
    //创建输入框
    UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(50, 100, 200, 40)];
    textField.tag = 101;
    textField.borderStyle = UITextBorderStyleRoundedRect;
    [self.view addSubview:textField];
    [textField release];
    
    
}

- (void)buttonAction {
    
    UITextField *field = (UITextField *)[self.view viewWithTag:101];
    
    //创建单例对象
    BackData *data = [BackData shareData];
    data.text = field.text;
    
    [self dismissViewControllerAnimated:YES completion:NULL];
    
}

BackData.h

@interface BackData : NSObject

@property (nonatomic, copy) NSString *text;

+ (BackData *)shareData;


BackData.m

static BackData *data = nil;

@implementation BackData

+ (BackData *)shareData {

    if (data == nil) {
        data = [[BackData alloc] init];
    }
    
    return data;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值