iOS传值的方法-单例反向传值
来源:互联网 发布:中国加工贸易数据 编辑:程序博客网 时间:2024/06/05 04:55
iOS传值,用单例进行反向传值,将B界面的值传到A界面。
1、A类中的代码如下:
#import "FirstViewController.h"#import "SecondViewController.h"#import "DataSource.h"@interface FirstViewController ()@property (retain,nonatomic) UILabel *label;@end@implementation FirstViewController@synthesize label;- (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor whiteColor]; self.title = @"单例反向传值"; label = [[UILabel alloc] initWithFrame:CGRectMake(10, 100, self.view.frame.size.width-20, 30)]; label.text = @"点击按钮"; [self.view addSubview:label]; UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(10, 200, self.view.frame.size.width-20, 35)]; btn.backgroundColor = [UIColor grayColor]; [btn setTitle:@"确定" forState:UIControlStateNormal]; [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:btn];}- (void)viewWillAppear:(BOOL)animated { DataSource *dataSource = [DataSource sharedDataSource]; if ([dataSource.myName length] != 0) { self.label.text = dataSource.myName; dataSource.myName = @""; }}- (void)btnClick:(id)sender { SecondViewController *second = [[SecondViewController alloc] init]; [self.navigationController pushViewController:second animated:YES]; }@end
2、B类中的代码如下:
#import "SecondViewController.h"#import "DataSource.h"@interface SecondViewController ()@property (retain, nonatomic) UITextField *textField;@end@implementation SecondViewController@synthesize textField;- (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor whiteColor]; textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 100, self.view.frame.size.width-20, 30)]; textField.borderStyle = UITextBorderStyleRoundedRect; [self.view addSubview:textField]; UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(10, 150, self.view.frame.size.width-20, 35)]; btn.backgroundColor = [UIColor grayColor]; [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:btn]; }- (void)btnClick:(id)sender { DataSource *dataSource = [DataSource sharedDataSource]; dataSource.myName = self.textField.text; [self.navigationController popViewControllerAnimated:YES];}@end
3、添加一个新类 DataSource
#import <Foundation/Foundation.h>@interface DataSource : NSObject@property (nonatomic, strong) NSString *myName;+(DataSource*)sharedDataSource;@end
#import "DataSource.h"@implementation DataSource+(DataSource *)sharedDataSource{ static DataSource *dataSource = nil; static dispatch_once_t once; dispatch_once(&once, ^{ dataSource = [DataSource new]; }); return dataSource;}@end
4、截图如下:
5、Demo下载地址: http://download.csdn.net/detail/u010545480/8915849
0 0
- iOS传值的方法-单例反向传值
- 关于iOS的UI界面反向传值方法总结
- IOS四种反向传值的方法
- iOS 反向传值
- 2、iOS传值的方法-delegate反向传值
- 4、iOS传值的方法-Notifaction反向传值
- 5、iOS传值的方法-Block反向传值
- ios UIViewController 反向传值
- iOS---block反向传值
- iOS 代理反向传值
- iOS block反向传值
- iOS 代理反向传值
- IOS代理反向传值
- iOS 代理反向传值
- ios之反向传值
- iOS 代理反向传值
- IOS传值方法- 属性反向传值(delegate)
- iOS 常用的反向传值协议的使用
- 小偷网站核心代码
- 几种数据持久化保存的方法!
- java ajax post跨域请求传递json格式数据问题
- 重定向,文件描述符
- Windows7 64位系统搭建Cocos2d-x-2.2.1最新版以及Android交叉编译环境(详细教程)
- iOS传值的方法-单例反向传值
- 使用Spring框架开发最少需要哪些jar包,依赖jar包有哪些?
- JS加载文件规范,AMD,commonJS,requireJs
- Unity接入ShareSDK过程
- Maven中的聚合实例
- Android中scrollview
- YII2 整合smarty
- CodeSmith和PowerDesigner的使用安装和数据库创建(原创系列教程)
- struts拦截器配置,以及全局的拦截器配置