iOS 中控制器传值

控制器之间传值常用的方式有:属性传值,委托传值,单例传值,Block传值以及通过通知中心传值。

1、属性传值

1.1导航栏跳传传值功能实现:在a视图输入信息点击按钮跳转b视图信息出现在b视图中

核心代码

在ZAViewController.m里。其中str为ZBViewController的属性

ZBViewController * bView = [self.storyboard instantiateViewControllerWithIdentifier:@"ZBViewController"];

bView.str = self.textField.text;

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


1.2Storyboard按钮跳转传值

在按钮上鼠标右键拖拽到另一个ViewController

这时点击按钮会触发prepareForSegue 系统方法

在ZAViewController中实现prepareForSegue方法

//属性声明
@property (weak, nonatomic) IBOutlet UITextField *Textnew;
//方法实现
//data是ZBViewController的NSString 属性
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    NSString *str=self.Textnew.text;
    ZBViewController * send = segue.destinationViewController;
    send.data = str;
}



2、委托传值

功能实现:在a视图里点击按钮跳转到b视图,在b视图文本框中输入信息点击按钮跳回a视图,信息出现在a视力文本框中。

委托声明:

#import <Foundation/Foundation.h>


@protocol ZViewPassValueDelegate <NSObject>


-(void)passValue:(NSString *)value;


@end


AViewController.h

#import <UIKit/UIKit.h>

#import "ZViewPassValueDelegate.h"


@interface ZViewController : UIViewController<ZViewPassValueDelegate>


@property (weak, nonatomic) IBOutlet UITextField *textField;


- (IBAction)button:(id)sender;


@end



AViewController.m

#import "ZViewController.h"

#import "ZBViewController.h"


@interface ZViewController ()


@end


@implementation ZViewController


- (void)viewDidLoad

{

    [super viewDidLoad];

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

}


-(void)passValue:(NSString *)value

{

    self.textField.text = value;

}

- (IBAction)button:(id)sender

{

    ZBViewController * bView = [self.storyboard instantiateViewControllerWithIdentifier:@"ZBViewController"];

    bView.delegate = self;

    [self presentViewController:bView animated:YES completion:nil];

}

@end



BViewController.h

#import <UIKit/UIKit.h>

#import "ZViewPassValueDelegate.h"


@interface ZBViewController : UIViewController


@property (weak, nonatomic) IBOutlet UITextField *textField;

@property (nonatomic,assign)NSObject <ZViewPassValueDelegate> * delegate;


- (IBAction)button:(id)sender;


@end



BViewController.m

#import "ZBViewController.h"


@interface ZBViewController ()


@end


@implementation ZBViewController


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self) {

        // Custom initialization

    }

    return self;

}


- (void)viewDidLoad

{

    [super viewDidLoad];

    // Do any additional setup after loading the view.

    

}


- (IBAction)button:(id)sender

{

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

    [self dismissViewControllerAnimated:YES completion:nil];

}


@end


3、通知中心传值

功能实现:在a视图里点击按钮跳转到b视图,在b视图文本框中输入信息点击按钮跳回a视图,信息出现在a视力文本框中。

a.m


#import "ZViewController.h"


@interface ZViewController ()


@property (weak, nonatomic) IBOutlet UITextField *textField;

@end


@implementation ZViewController


- (void)viewDidLoad

{

    [super viewDidLoad];

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

    //注册通知 @"RELOADVOEWNOTIFICATION" 为通知唯一标识 只有跟标识一样的通知才会被传值

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

}


- (void)reloadView: (NSNotification *)sender

{

    self.textField.text = sender.object;

}


- (IBAction)buttion:(id)sender

{

    [self performSegueWithIdentifier:@"qwe" sender:self];

}


@end


b.m

//按钮点击

- (IBAction)button:(id)sender

{

    //此处参数是reloadView的参数

    [[NSNotificationCenter defaultCenter]postNotificationName:@"RELOADVOEWNOTIFICATION" object:self.textField.text];

    [self dismissViewControllerAnimated:YES completion:nil];

}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值