《iOS总结》传值方式-MickyChiang

来源:互联网 发布:js 判断是function 编辑:程序博客网 时间:2024/05/24 02:38

最近有闲暇时间,没事就多发几篇有用的= = 

下面总结一下iOS中经常碰到的几种传值方式(属性传值、协议传值、Block传值、通知传值、单例传值)


一. 属性传值

属性传值就是我们常说的正常的从前往后传,即将A页面所拥有的信息通过属性传递到B页面中使用。

举例:有A页面和B页面,B页面中定义了一个c属性,在A页面中直接通过属性赋值将A页面中的值传到B页面。

这种属性传值方式是最常见也是最简单的。


二. 协议传值

A页面push到B页面,如果B页面的信息想回调到A页面,用协议传值,其中B页面定义协议和声明代理,A页面确认并实现代理,A作为B的代理。

举例:

FirstViewController和SecondViewController


SecondViewController.h中:


#import <UIKit/UIKit.h>

 

// 协议传值1:在第二页创建协议

@protocol SecondViewControllerDelegate <NSObject>

//定义一个协议方法,带一个参数

@optional

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

@end


@interface SecondViewController : UIViewController


//从前一个页面传递信息到后一个页面的过程,其实就是给后面的页面赋值的过程

//想从外部获得就写在.h

//一般传数据

@property (nonatomic, retain) NSString *name;

// 协议传值2:在第二页创建一个代理人属性

@property (nonatomic, assign) id<SecondViewControllerDelegate> delegate;


@end




SecondViewController.m中:


#import "SecondViewController.h"

// 利用延展,管理私有的属性和方法,目的是让.h文件更简洁

@interface SecondViewController ()

// 按钮

@property (nonatomic, retain) UIButton *button;

// 文本

@property (nonatomic, retain) UITextField *textField;

@end


@implementation SecondViewController

// 因为我用的是MRC 所以需要内存管理

- (void)dealloc

{

    [_name release];

    [_button release];

    [_textField release];

    [super dealloc];

}


- (void)viewDidLoad {

    [super viewDidLoad];

   

    self.view.backgroundColor = [UIColor yellowColor];


    self.navigationItem.title = @"第二页";


    self.textField = [[UITextField alloc] initWithFrame:CGRectMake(20, 100, 335, 40)];

    self.textField.borderStyle = UITextBorderStyleRoundedRect;


   //自己的属性可以直接使用

    self.textField.text = self.name;


    [self.view addSubview:self.textField];

    [_textField release];

    

    self.button = [UIButton buttonWithType:UIButtonTypeSystem];

    self.button.frame = CGRectMake(80, 180, 215, 40);

    self.button.backgroundColor = [UIColor whiteColor];

    [self.button setTitle:@"返回" forState:UIControlStateNormal];

    [self.button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:self.button];

}


- (void)buttonAction:(UIButton *)button

{

    if (self.delegate && [self.delegate respondsToSelector:@selector(passValueWithString:)]) {

#warning协议传值3:在第二页的按钮方法中,让自己的代理人执行协议方法

        

        // 参数填写要 给第一页 的值

        [self.delegate passValueWithString:self.textField.text]; 

    }    

    [self.navigationController popViewControllerAnimated:YES];

}


- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


@end




FirstViewController.m中:

#import "FirstViewController.h"

#import "SecondViewController.h"


// 协议传值4:第一页签订协议

@interface FirstViewController () <SecondViewControllerDelegate>

@property (nonatomic, retain) UIButton *button1;

@end


@implementation FirstViewController


- (void)dealloc

{

    [_button1 release];

    [super dealloc];

}


- (void)viewDidLoad {

    [super viewDidLoad];


    self.view.backgroundColor = [UIColor cyanColor];


    self.navigationItem.title = @"第一页";


    self.button1 = [UIButton buttonWithType:UIButtonTypeSystem];

    self.button1.frame = CGRectMake(20, 100, 335, 40);

    self.button1.backgroundColor = [UIColor whiteColor];

    [self.button1 setTitle:@"Button" forState:UIControlStateNormal];

    [self.button1 addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:self.button1];

}


- (void)buttonAction:(UIButton *)button

{

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

    // 给第二个页面的属性赋值

    //把当前buttontitle传给第二个页面

    secondVC.name = button.currentTitle;


// 协议传值5:给第二页指定代理人

    secondVC.delegate = self;

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

    [secondVC release];

}


// 协议传值6:实现相应的协议方法,利用第二页给的参数

- (void)passValueWithString:(NSString *)str

{

    //给按钮重新赋值

    [self.button1 setTitle:str forState:UIControlStateNormal];

}


- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


@end


自己运行一下= =

 



三. Block传值

讲传值之前先要回忆一下Block的用法





具体传值如下:

SecondViewController.h中:




SecondViewController.m中:



FirstViewController.m中:



演示效果类似 故不截图了= =


四. 通知传值

我记得在我的博客中 《iOS总结》设计模式中 有简单的介绍,在这里不说了 之后会有代码资源 = = 





0 0