UI總結-界面傳值

UI總結-界面傳值(屬性傳值,協議傳值,block傳值,通知中指傳值)

在編程過程中,界面傳值是很重要的一部分,常用的傳值方式就有四種:屬性傳值,協議傳值,block傳值,通知中指傳值,下面通過簡單的代碼來實現這四種傳值方式:

前一個界面ViewController.m文件:

#import "ViewController.h"

#import "SecondViewController.h"

//4.簽訂協議

@interface ViewController ()

@property(nonatomic, retain)UITextField *text;

@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

self.view.backgroundColor = [UIColor whiteColor];

UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];

btn.backgroundColor = [UIColor redColor];

[self.view addSubview:btn];

btn.frame = CGRectMake(100, 100, 200, 50);

btn.layer.borderWidth = 1;

btn.layer.cornerRadius = 5;

[btn setTitle:@"下一頁" forState:UIControlStateNormal];

[btn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];

self.text = [[UITextField alloc]initWithFrame:CGRectMake(100, 200, 200, 50)];

self.text.backgroundColor = [UIColor blueColor];

[self.view addSubview:self.text];

[_text release];

self.text.layer.borderWidth = 1;

self.text.layer.cornerRadius = 5;

//第四種傳值方式:通知中心

[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(jieshou:) name:@"text" object:nil];

}

-(void)click:(UIButton *)button{

SecondViewController *vc = [[SecondViewController alloc]init];

[self.navigationController pushViewController:vc animated:YES];

//屬性傳值

//在要傳值的后一個頁面定義屬性,在對應的視圖控制器調用屬性進行賦值,就可以將值傳到后一個頁面

vc.str = self.text.text;

vc.arr = @[@"james", @"alice", @"tom"];

//5.設置代理人

vc.delegate = self;

//先寫一個block

void(^block)() = ^(){

self.view.backgroundColor = [UIColor blueColor];

};

//傳值

vc.block = block;

[vc release];

}

//6.實現代理方法

-(void)sendValue:(NSString *)str{

self.title = str;

}

-(void)jieshou:(NSNotification *)notification{

self.text.text = [notification.userInfo valueForKey:@"name"];

}

后一個界面SecondViewController.h文件:

#import

//1.聲明協議

@protocol SecondViewControllerDelegate

-(void)sendValue:(NSString *)str;

@end

@interface SecondViewController : UIViewController

@property(nonatomic, retain)NSString *str;

@property(nonatomic, retain)NSArray *arr;

//2.設置代理人屬性@property(nonatomic, assign)iddelegate;

//block傳值

@property(nonatomic, copy)void(^block)(void);

@end

后一個界面SecondViewController.m文件:

#import "SecondViewController.h"

@interface SecondViewController ()

@property(nonatomic, retain)UITextField *text;

@end

@implementation SecondViewController

- (void)viewDidLoad {

[super viewDidLoad];

// Do any additional setup after loading the view.

self.view.backgroundColor = [UIColor whiteColor];

NSLog(@"%@", self.str);

self.title = self.str;

NSLog(@"%@", self.arr);

UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];

btn.backgroundColor = [UIColor redColor];

[self.view addSubview:btn];

btn.frame = CGRectMake(100, 100, 200, 50);

btn.layer.borderWidth = 1;

btn.layer.cornerRadius = 5;

[btn setTitle:@"返回" forState:UIControlStateNormal];

[btn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];

self.text = [[UITextField alloc]initWithFrame:CGRectMake(100, 200, 200, 50)];

self.text.backgroundColor = [UIColor blueColor];

[self.view addSubview:self.text];

[_text release];

self.text.layer.borderWidth = 1;

self.text.layer.cornerRadius = 5;

}

-(void)click:(UIButton *)button{

[self.navigationController popViewControllerAnimated:YES];

//3.觸發協議生效

[self.delegate sendValue:self.text.text];

//調用block

self.block();

[[NSNotificationCenter defaultCenter]postNotificationName:@"text" object:nil userInfo:@{@"name":@"james"}];

}

運行結果如下:


最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容