【学习笔记】AB传

来源:互联网 发布:女友花钱大手大脚 知乎 编辑:程序博客网 时间:2024/06/07 19:04

//

//  AAAViewController.h


#import <UIKit/UIKit.h>

#import "BBBViewController.h"


@interface AAAViewController :UIViewController

{

    UILabel*label; //label的标题声明全局

}

@end


//

//  AAAViewController.m


#import "AAAViewController.h"


@interface AAAViewController ()

@end


@implementation AAAViewController


- (void)viewDidLoad {

    [superviewDidLoad];


////////////建立button,从AAA推送到BBB视图

   UIButton*button1=[[UIButtonalloc]initWithFrame:CGRectMake(105,100,50, 30)]  ;

    [button1 setBackgroundColor:[UIColorredColor]];

    [button1 setTitle:@"BBB"forState:UIControlStateNormal];

    [button1 addTarget:selfaction:@selector(GOBBB)forControlEvents:UIControlEventTouchUpInside];

    [self.viewaddSubview:button1];

    

///////////label的标题///////////

    label=[[UILabelalloc]initWithFrame:CGRectMake(50,255,200, 40)];

    label.text=@"yiuhuohuoho";

    label.backgroundColor=[UIColoryellowColor];

    [self.viewaddSubview:label];

}


///////////PUSH 方法,加传值的方法///////////

-(void)GOBBB

{

    BBBViewController*bbbView=[[BBBViewControlleralloc]init];

    

    bbbView.mystr =label.text;     //传值关键

    

    [self.navigationControllerpushViewController:bbbViewanimated:YES];  

}


@end




//

//  BBBViewController.h



#import <UIKit/UIKit.h>


@interface BBBViewController :UIViewController


@property (nonatomic,copy)NSString *mystr;   //设置变量,复制保存从AAA传来的label标题,而且是字符串


@end



//

//  BBBViewController.m


#import "BBBViewController.h"


@interface BBBViewController ()

@end


@implementation BBBViewController


- (void)viewDidLoad {

    [superviewDidLoad];

    

    UILabel*label=[[UILabelalloc]initWithFrame:CGRectMake(50,200,200, 40)];

    

    label.text=self.mystr;     //完成传值

    

    label.backgroundColor=[UIColoryellowColor];

    [self.viewaddSubview:label];



    UIButton*button=[[UIButtonalloc]initWithFrame:CGRectMake(50,300, 200, 30)];

    

    [button setTitle:self.mystrforState:UIControlStateNormal]; //完成传值,可以多传,注意用self.mystr


    button.backgroundColor=[UIColorredColor];

    [self.viewaddSubview:button];

    



}


@end



0 0