ios 页面传值

今天看了一下ios 页面传值的方式大致分为四种:

  • 代理delegate
  • block
  • 通知
  • 单例class

今天试了一下前三种,在这里记录一下

下面示例是有两个页面,每个页面都有一个按钮Button,点击第一个页面的按钮回调到第二个页面,再点击第二个页面回跳转道第一个页面,第一个按钮的标题变为第二个按钮传回的值。

代理delegate

代理似乎是我的心结,能用API 但是就是不会自己写,这也是今天会写传值的原因。

假设两个页面传值,协议类应该写在哪,代理应该定义在那个页面?

总结的时候我觉得可以这样来想:第二个页面想要修改第一个页面的值,但是他没办法做到,所以他需要一个代理来帮助它修改,所以代理需要定义在第二个页面。第一个页面需要支持代理。

第一个页面:

@interface frontVC : UIViewController<change>

@property (weak, nonatomic) IBOutlet UIButton *frontBtn;

@end
#import "frontVC.h"

@interface frontVC ()
{
    realendVC *realendvc;//第二个页面
}

@end

@implementation frontVC

- (void)viewDidLoad {
    [super viewDidLoad];
    //从storyboard中获得第二个页面
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
    realendvc = [storyboard instantiateViewControllerWithIdentifier:@"second"];
    //设置代理
    realendvc.delegate = self;
}

//按钮点击事件
- (IBAction)click:(id)sender {
    [self.navigationController pushViewController:realendvc animated:YES];
}

//代理方法
- (void)changebutton:(NSString *)string
{
    [_frontBtn setTitle:string forState:UIControlStateNormal];
}

@end

第二个页面:

@protocol change <NSObject>

- (void) changebutton:(NSString *)string;

@end

@interface realendVC : UIViewController

@property (weak, nonatomic) IBOutlet UIButton *realendBtn;

@property (nonatomic,weak) id<change> delegate;

@end
#import "realendVC.h"

@interface realendVC ()

@end

@implementation realendVC

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (IBAction)click:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
    if (self.delegate) {
        [self.delegate changebutton:@"成功"];
    }
}

@end

block

第一个页面

#import <UIKit/UIKit.h>
#import "realendVC.h"

@interface frontVC : UIViewController

@property (weak, nonatomic) IBOutlet UIButton *frontBtn;

@end
#import "frontVC.h"

@interface frontVC ()
{
    realendVC *realendvc;//第二个页面
}

@end

@implementation frontVC

- (void)viewDidLoad {
    [super viewDidLoad];
    //从storyboard中获得第二个页面
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
    realendvc = [storyboard instantiateViewControllerWithIdentifier:@"second"];

    //在block中允许访问变量,但不允许修改,需要加上下面一句代码
    __block UIButton * button = _frontBtn;
    realendvc.myblock = ^(NSString *string)
    {
        [button setTitle:string forState:UIControlStateNormal];
    };
}

//按钮点击事件
- (IBAction)click:(id)sender {
    [self.navigationController pushViewController:realendvc animated:YES];
}

@end

第二个页面

#import <UIKit/UIKit.h>

typedef void(^Myblock) (NSString *);
@interface realendVC : UIViewController

@property (weak, nonatomic) IBOutlet UIButton *realendBtn;

@property (copy,nonatomic) Myblock myblock;

@end
#import "realendVC.h"

@interface realendVC ()

@end

@implementation realendVC

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (IBAction)click:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
    self.myblock(@"成功");
}

@end

通知

第一个页面

#import <UIKit/UIKit.h>
#import "realendVC.h"

@interface frontVC : UIViewController

@property (weak, nonatomic) IBOutlet UIButton *frontBtn;

@end
#import "frontVC.h"

@interface frontVC ()
{
    realendVC *realendvc;//第二个页面
}

@end

@implementation frontVC

- (void)viewDidLoad {
    [super viewDidLoad];
    //从storyboard中获得第二个页面
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
    realendvc = [storyboard instantiateViewControllerWithIdentifier:@"second"];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(change:) name:@"button"object:nil];

}
//通知调用事件
- (void)change:(NSNotification *)notification
{
    [_frontBtn setTitle:(NSString *)[notification object] forState:UIControlStateNormal];
}

//按钮点击事件
- (IBAction)click:(id)sender {
    [self.navigationController pushViewController:realendvc animated:YES];
}

@end

第二个页面

#import <UIKit/UIKit.h>

@interface realendVC : UIViewController

@property (weak, nonatomic) IBOutlet UIButton *realendBtn;

@end
#import "realendVC.h"

@interface realendVC ()

@end

@implementation realendVC

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (IBAction)click:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"button" object:@"成功"];
}

@end

感谢各博主大神。

时间: 2024-08-05 12:36:56

ios 页面传值的相关文章

iOS 页面传值的几种方式之NSNotificationCenter传值

接着上回说的iOS页面传值问题 传送门---------->iOS页面传值之代理传值 接下来我们说说NSNotificationCenter传值方式 在开始之前,我们首先得知道KVO模式 Key-Value Observing (KVO) 键值监听 就是说当你告诉通知中心一个Key 他会根据Value的变化做些事情,或者是获取一些数据 说上千回,不如用上一回. 我们在B控制器发送一个监听 [[NSNotificationCenter defaultCenter] postNotification

iOS页面传值-wang

iOS页面间传值的方式(NSUserDefault/Delegate/NSNotification/Block/单例) 实现了以下iOS页面间传值:1.委托delegate方式:2.通知notification方式:3.block方式:4.UserDefault或者文件方式:5.单例模式方式:6.通过设置属性,实现页面间传值 在iOS开发中,我们经常会遇到页面间跳转传值的问题,现归纳总结一下: 情况1:A页面跳转到B页面 方法: 在B页面的控制器中,编写对应的属性,在A页面跳转到B页面的地方,给

iOS页面传值的方式

iOS页面间传值的方式(Delegate/NSNotification/Block/NSUserDefault) [摘要]本文是对iOS页面间传值的方式(Delegate/NSNotification/Block/NSUserDefault)的讲解,对学习IOS苹果软件开发有所帮助,与大家分享. iOS页面间传值的方式(NSUserDefault/Delegate/NSNotification/Block) 实现了以下iOS页面间传值:1.委托delegate方式:2.通知notificatio

ios页面传值的几种方法

1.属性2.方法3.代理方法4.SharedApplication5.NSUserdefault6.通过一个单例的class来传递 属性这种方法传值挺方便的,只需要拿到它的指针,如果重新声明一个指针,就不是原来的内容,不是同一个指针,因此需要传指针. xxxViewController *document = [[xxxViewController alloc] initWithStyle:UITableViewStyleGrouped]; document.docDict = [self.da

iOS页面传值总结

从最简单的页面跳转开始说起, FirstViewController -----> SecondViewController 方法:直接在跳转处直接给第二个控制器的属性赋值 // FirstViewController.m // ... SecondViewController *sec = [SecondViewController new]; sec.backgroundColor = [UIColor RedColor];   // 直接给SecondViewController属性赋值

iOS页面传值之Block传值

场景A控制器中有一个Label  B控制器中有一个文本输入框textField A push 到B 当,B返回A时,我们让B中文本框的内容展示到A中去 第一步: 在B中定义Block #import <UIKit/UIKit.h> typedef void(^PassValueOption)(NSString*); @interface ZPViewControllerB : UIViewController @property(nonatomic,copy)PassValueOption o

iOS页面间传值的方式(Delegate/NSNotification/Block/NSUserDefault/单例)

iOS页面间传值的方式(NSUserDefault/Delegate/NSNotification/Block/单例) 实现了以下iOS页面间传值:1.委托delegate方式:2.通知notification方式:3.block方式:4.UserDefault或者文件方式:5.单例模式方式:6.通过设置属性,实现页面间传值 在iOS开发中,我们经常会遇到页面间跳转传值的问题,现归纳总结一下: 情况1:A页面跳转到B页面 方法: 在B页面的控制器中,编写对应的属性,在A页面跳转到B页面的地方,给

iOS 页面间传值 之 单例传值 , block 传值

ios 页面间传值有许多,前边已经分享过属性传值和代理传值,今天主要说一下单例传值和 block 传值 单例传值:单例模式一种常用的开发的模式,单例因为在整个程序中无论在何时初始化对象,获取到的都是同一个对象,对象的属性相同,所以可以用来传值. block 传值 与 代理传值相似,主要用于第二个页面向第一个页面传值,block 传值具体步骤: 在第二个页面: 1.声明: block typedef void(^SendMessagerBlock) (NSString *str); 2.创建方法:

iOS页面间传值的方式

实现了以下iOS页面间传值:1.委托delegate方式:2.通知notification方式:3.block方式:4.UserDefault或者文件方式:5.单例模式方式:6.通过设置属性,实现页面间传值 在iOS开发中,我们经常会遇到页面间跳转传值的问题,现归纳总结一下: 情况1:A页面跳转到B页面 方法: 在B页面的控制器中,编写对应的属性,在A页面跳转到B页面的地方,给B的属性赋值即可 //SecondViewController.h @property(nonatomic) NSInt