属性 从 a到b b不能到 a
代理
代码块
单例
通知
属性传参 a为父视图 b为子视图
1.属性
在b的.h文件中设置对应的接收属性 NSString*recText;
UITextField *receiveText;
在 a中对属性赋值
TwoViewController*twov=[TwoViewControllernew];
[self.navigationController pushViewController:twovanimated:YES];
twov.recText=self.userText.text;
在b中 接收传输的属性
[self.receiveText setText:self.recText];//图片类似
2.代理
在传送方写协议 接收方遵守协议
b a
b的h文件中写
@protocol Mypro<NSObject>
-(void)setTextValue:(NSString *)text;
@end
@property(assign,nonatomic)id<Mypro>delegate;//设置代理属性
a中
@interface OneViewController()<Mypro>//遵守协议
-(void)setTextValue:(NSString *)text//代理传参
{
self.userText.text=text;
}
TwoViewController *twov=[TwoViewControllernew];
twov.delegate=self;//指明协议遵守者为a
b中
[self.delegate setTextValue:self.receiveText.text];//代理传参
3.代码块传参
a b
在b的.h中定义
typedefvoid(^setupTitle) (NSString*);
@interface NcViewController : UIViewController
@property(copy,nonatomic)setupTitlemytitle;
@end//定义出代码块1
在b.m中赋值
self.mytitle(self.userText.text);//给代码块的参数赋值2
在a中调用
twoc.mytitle=^(NSString *xc)
{
self.title=xc;
};//正式调用代码块
4.单例
//1.定义单例对象 static类型变量
staticSing * instance;
//2.单例对象创建方法
+(id)getinstance
{ @synchronized(self)//多线程同步,防止多线程的并发访问
{
if (instance==nil)
{
instance=[[Singalloc]init];//创建新对象
}
}
returninstance;
}
//重写方法 防止用户通过alloc方法构建多个对象
+(instancetype)allocWithZone:(struct_NSZone *)zone
{
if (instance==nil) {
instance=[[superallocWithZone:zone]init];
}
returninstance;
}
-(void)pass:(id)sender//单例传参方法
{
Sing *obj=[Singgetinstance];//创建单例对象obj
obj.titleStr=self.usertext.text;//
}
接收方
-(void)viewDidAppear:(BOOL)animated//每次视图要显示的时候
{
self.title=[[Sing getinstance]titleStr];
}
5.通知传参
//1注册通知
[[NSNotificationCenterdefaultCenter]addObserver:twoc selector:@selector(changeBadgeValue:) name:@"w的通知"object:nil];
// 2发送通知
[[NSNotificationCenterdefaultCenter]postNotificationName:@"w的通知" object:nil];