首页 > 代码库 > 单例---视图间数据的传递:标签显示输入的内容【多个视图中】
单例---视图间数据的传递:标签显示输入的内容【多个视图中】
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 = http://www.mamicode.com/[BackData shareData];>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 = http://www.mamicode.com/[BackData shareData];>
BackData.h@interface BackData : NSObject @property (nonatomic, copy) NSString *text; + (BackData *)shareData;BackData.m
static BackData *data = http://www.mamicode.com/nil;>单例---视图间数据的传递:标签显示输入的内容【多个视图中】
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。