多个ViewController之间传值

来源:互联网 发布:python 自动化 工业 编辑:程序博客网 时间:2024/06/01 08:03

   多个ViewController进行传值,有三种方式:

     

            1:使用代理

            2:   使用Block

            3:使用单例

 

- (void)viewDidLoad

{

    [superviewDidLoad];

    self.navigationItem.title=@"title1";

    self.navigationItem.rightBarButtonItem=[[UIBarButtonItemalloc] initWithTitle:@"next"style:UIBarButtonItemStylePlaintarget:selfaction:@selector(next)];

    

}

-(void)next

{

    AZSecondViewController *secondVC=[[AZSecondViewControlleralloc] init];

    secondVC.title=@"title2";

    //第一种:使用代理 ,secondVC中见代码

    

    secondVC.delegate=self;

    

    //第二种:使用BLOCK

    

   //定义secondVC中blockFun

    secondVC.blockFun=^(NSString *title)

    {

        self.navigationItem.title=title;

    };

    

    //第三种:使用单例  -(void)viewWillAppear:(BOOL)animated

    

    [self.navigationControllerpushViewController:secondVC animated:YES];

}


//第三种:使用单例

-(void)viewWillAppear:(BOOL)animated

{

    AZSingleton *sington=[AZSingletonshareSingleton];

    if (sington.str==nil) {

        return;

    }

    self.navigationItem.title=sington.str;

}

//实现协议中的方法

-(void)getVaule:(NSString *)title

{

    self.navigationItem.title=title;

}


UISecondViewController:   

    //创建三个按键

    UIButton *btn1=[UIButtonbuttonWithType:UIButtonTypeCustom];

    btn1.frame=CGRectMake(10,60, 300, 20);

    [btn1 setTitle:@"使用代理" forState:UIControlStateNormal];

    btn1.tag=10;

    [btn1 addTarget:selfaction:@selector(btnClick:)forControlEvents:UIControlEventTouchUpInside];

    [self.viewaddSubview:btn1];

    

    UIButton *btn2=[UIButtonbuttonWithType:UIButtonTypeCustom];

    btn2.frame=CGRectMake(10,90, 300, 20);

    btn2.tag=11;

    [btn2 setTitle:@"使用block"forState:UIControlStateNormal];

      [btn2 addTarget:selfaction:@selector(btnClick:)forControlEvents:UIControlEventTouchUpInside];

    [self.viewaddSubview:btn2];

    

    UIButton *btn3=[UIButtonbuttonWithType:UIButtonTypeCustom];

    btn3.frame=CGRectMake(10,120, 300, 20);

    btn3.tag=12;

    [btn3 setTitle:@"使用单例" forState:UIControlStateNormal];

      [btn3 addTarget:selfaction:@selector(btnClick:)forControlEvents:UIControlEventTouchUpInside];

    [self.viewaddSubview:btn3];

}

-(void)btnClick:(UIButton *)btn

{

    if (btn.tag==10) {

        [self.delegategetVaule:@"使用代理"];

       

    }

    if (btn.tag==11) {

         self.blockFun(@"使用block");//定义了一个快函数,这里只是调用一下块函数,实现在rootVC中。

    }

    if (btn.tag==12) {

        AZSingleton *singleton=[AZSingletonshareSingleton];

        singleton.str=@"使用单例";

    }

}


效果:







0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 联通用超了流量怎么办 联通3g上不了网怎么办 车玻璃水冻住了怎么办 车里玻璃水冻了怎么办 做现货亏了60万怎么办 宿舍太吵晚上睡不着觉怎么办 脚扎了钉子肿了怎么办 龙血树叶子下垂怎么办 龙血树叶子卷曲怎么办 3岁宝宝长期便秘怎么办 4岁小儿便秘严重怎么办 3岁宝宝便秘严重怎么办 3岁宝宝一直便秘怎么办 11个月宝宝便秘怎么办 2个月的宝宝便秘怎么办 宝宝便秘拉不下来怎么办 5一6岁儿童便秘怎么办 3个月宝宝便秘怎么办 8个月宝宝便秘怎么办 孕5个月咳嗽厉害怎么办 孕8个月咳嗽厉害怎么办 拆石膏后关节僵硬怎么办 宝宝的小腿不直怎么办 鸡咳嗽有痰呼噜怎么办 风热感冒怎么办小窍门 吃完虾喝了牛奶怎么办 三文鱼头汤腥怎么办 晚上咳嗽厉害怎么办睡不着觉 刚怀孕发烧39度怎么办 刚怀孕发烧38度怎么办 怀孕10天发烧了怎么办 怀孕2个月发烧了怎么办 lol误封3年怎么办 心悦会员到期了怎么办 心悦游戏家到期怎么办 无间鬼后运气背怎么办 趣店被骗提现了怎么办 微转奇迹闪退怎么办 奇迹暖暖ios闪退怎么办 奇迹mu任务没做怎么办 外地人在北京上社保怎么办