iOS 页面之间传参的总结

属性 从 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];

 

 

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值