iOS传值(代理传值,block 传值,单例传值,通知传值,属性传值)

特别声明:文章多为网络转载,资源使用一般不提供任何帮助,特殊资源除外,如有侵权请联系!

1、代理传值

有A \B 两个页面需要将B页面的值传个A

总结:

1.首先应该在b页面写一个协议,协议里有带参数的方法,并创建实现该协议的属性delegate,然后判断该delegate_ 是否实现了协议的方法,实现该协议方法,并传入值。

2.在A.h 文件里声明协议,并在A.m文件里实现协议方法

例如:

B.h

//声明协议

@protocol Senvalue <NSObject>

-(void)sendname:(NSString *)name;

-(void)sendpass:(NSString *)pass;

@end

@interface ViewController : UIViewController

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

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

- (IBAction)regester:(UIButton *)sender;//按钮事件

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

@end

B.m

#import "ViewController.h"
@interface ViewController ()
@end

@implementation ViewController

@synthesize delegate = delegate_;
-(IBAction)regester:(UIButton *)sender
{
    NSString *name1 = self.nametext.text;
    NSString *pass1 = self.password.text;
    if([delegate_ respondToSelector :@selector(sendname:)]&&[delegate_ respondToSelector:@selector(sendpass:)])
    {
        [delegate_ sendname:name1];
        [delegate_ sendpass:pass1];
    [self.navigationController popToRootViewControllerAnimated:YES];
    }
}

A.h

#import <UIKit/UIKit.h>

#import "ViewController.h"

@interface RootViewController : UIViewController <SenValue> //实现了B的协议

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

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

- (IBAction)Sender:(UIButton *)sender;

@end

A.m

/**

 主要实现了B协议的方法

 */

-(void)sendname:(NSString *)name{

    _NameText.text =name;

    NSLog(@"%@....",_NameText.text);

}

-(void)sendpass:(NSString *)pass{

    _PassWord.text =pass;

    NSLog(@"%@....",_PassWord.text);

}

2.用block 传值

有A \B 两个页面需要将B页面的值传个A

总结:

1.首先在b.h文件中声明一个字符串指针函数:(typedef void(^PostString)(NSString *str)),然后声明一个该指针函数的属性,用该属性传值,

2.在A.m 文件中创建B的对象,用该对象实现block;
————————————————
B.h

#import <UIKit/UIKit.h>

@interface DemoViewController : UIViewController

typedef void (^ PostString)(NSString *str); //声明一个字符串指针函数

@property (nonatomic,copy)PostString mblock;

@end

B.m

#import "DemoViewController.h"
@interface DemoViewController ()

@end

@implementation DemoViewController

-(void)touchesEnded:(NSSet *)touches withEnent:(UIEvent *)event{

    NSLog (@"反向传值")

    if (self.mblock) {

        self.mblock(@"Test");

    }

}

A.m

#import "QFViewController.h"
#import "DemoViewController.h"
@interface QFViewController ()
@end

@implementation QFViewController
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{

    DemoViewController *ctl = [[DemoViewController alloc]init];

    ctl.mblock = ^(NSString *str){

        NSLog(@"接收到DemoViewController 传递过来的: %@",str);

}

3.单例传值

AppStatus.h  创建一个单例类 AppStatus

#import <Foundation/Foundation.h>
@interface AppStatus : NSObject{

    NSString *_contextStr;

}
@property(nonatomic,retain)NSString *contextStr;
+(AppStatus *)shareInstance;
@end

AppStatus.m

#import "AppStatus.h"
@implementation AppStatus
@synthesize contextStr = _contextStr;
static AppStatus *_instance = nil;

+(AppStatus *)shareInstance
{
      if (_instance == nil)
        {
            _instance = [[super alloc]init];

        }

        return _instance;

    }

 -(id)init{
    if (self = [super init]) {
            

        }

        return  self;

    }
    -(void)dealloc

    {

        [super dealloc];

    }
    @end

A页面RootViewController.h

#import "RootViewController.h"

#import "DetailViewController.h"

#import "AppStatus.h"

    @interface RootViewController ()

    @end

    @implementation RootViewController

    -(void)loadView
    {

        //核心代码

        UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];

        btn.frame = CGRectMake(0, 0, 100, 30);

        [btn setTitle:@"Push" forState:0];

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

        [self.view addSubview:btn];

    }
    -(void)pushAction:(id)sender {

        tf = (UITextField *)[self.view viewWithTag:1000];

        //单例传值  将要传递的信息存入单例中(共享中)

        //  [[AppStatus shareInstance]setContextStr:tf.text]; 跟下面这种写法是等价的

        [AppStatus shareInstance].contextStr = tf.text;

        //导航push到下一个页面

        //pushViewController 入栈引用计数+1,且控制权归系统

        DetailViewController *detailViewController = [[DetailViewController alloc]init];

        //导航push到下一个页面

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

        [detailViewController release];

    }
    @end

B页面DetailViewController.h

#import <UIKit/UIKit.h>
    @protocol ChangeDelegate;//通知编译器有此代理
    @interface DetailViewController : UIViewController
    {

        UITextField *textField;

    }
    @end

B页面DetailViewController.m

#import "DetailViewController.h"

#import "AppStatus.h"

    

    @interface DetailViewController ()

   
    @end


    @implementation DetailViewController

    @synthesize naviTitle = _naviTitle;

    -(void)loadView

    {

        self.view = [[[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)]autorelease];

        

        //单例

        self.title = [AppStatus shareInstance].contextStr;
        textField = [[UITextField alloc]initWithFrame:CGRectMake(100, 100, 150, 30)];

        textField.borderStyle = UITextBorderStyleLine;

        [self.view addSubview:textField];

        [textField release];

        UIBarButtonItem *doneItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDonetarget:self action:@selector(doneAction:)];

        self.navigationItem.rightBarButtonItem = doneItem;

        [doneItem release];

    }

    //这个方法是执行多遍的  相当于刷新view

    -(void)viewWillAppear:(BOOL)animated{

        [super viewWillAppear:animated];

        tf = (UITextField *)[self.view viewWithTag:1000];

        tf.text = [AppStatus shareInstance].contextStr;
    }

    //pop回前一个页面
    -(void)doneAction:(id)sender
    {
        //  单例传值

        [AppStatus shareInstance].contextStr = textField.text;

        [self.navigationController popToRootViewControllerAnimated:YES];

    }

4.通知传值 谁要监听值的变化,谁就注册通知  特别要注意,通知的接受者必须存在这一先决条件

A页面RootViewController.h

#import <UIKit/UIKit.h>

#import "DetailViewController.h"

    @interface RootViewController : UIViewController<ChangeDelegate> {

        UITextField *tf;

    }

    @end

A页面RootViewController.m

#import "IndexViewController.h"

#import "DetailViewController.h"

#import "AppStatus.h"

@implementation IndexViewController
    -(void)dealloc{

        [[NSNotificationCenter defaultCenter] removeObserver:self

                                                        name:@"CHANGE_TITLE" object:nil];

        [super dealloc];

    }
    -(id)init
    {

        if (self = [super init])

        {

            [[NSNotificationCenter defaultCenter] addObserver:self

                                                     selector:@selector(change:)

                                                         name:@"CHANGE_TITLE"

                                                       object:nil];

        }

        return self;

    }

    -(void)change:(NSNotification *)aNoti {

        // 通知传值

        NSDictionary *dic = [aNoti userInfo];

        NSString *str = [dic valueForKey:@"Info"];

        UITextField *tf =  (UITextField *)[self.view viewWithTag:1000];

        tf.text = str;

    }

    -(void)viewWillAppear:(BOOL)animated
    {

        [super viewWillAppear:animated];

        /*

         // 单例传值

         UITextField *tf =  (UITextField *)[self.view viewWithTag:1000];

         tf.text = [AppStatus shareInstance].contextStr;

         */
    }
    @end

DetailViewController.h

#import <UIKit/UIKit.h>
@protocol ChangeDelegate;//通知编译器有此代理
@interface DetailViewController : UIViewController
    {

        UITextField *textField;

    }
    @end

DetailViewController.m

#import "DetailViewController.h"

#import "AppStatus.h"

    @implementation DetailViewController

    @synthesize naviTitle = _naviTitle;

    -(void)loadView{

        UIBarButtonItem *doneItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDonetarget:self action:@selector(doneAction:)];

        self.navigationItem.rightBarButtonItem = doneItem;

        [doneItem release];

    }
    // pop回前一个页面

    -(void)doneAction:(id)sender {

        NSDictionary *dic = [NSDictionary dictionaryWithObject:textField.text forKey:@"Info"];

        [[NSNotificationCenter defaultCenter] postNotificationName:@"CHANGE_TITLE" object:nil userInfo:dic];

        [self.navigationController popViewControllerAnimated:YES];

    }

 

 

未经允许不得转载:作者:SheaYang, 转载或复制请以 超链接形式 并注明出处 技术Dog|博客
原文地址:《iOS传值(代理传值,block 传值,单例传值,通知传值,属性传值)》 发布于2019-10-31

分享到:
赞(0) 打赏

评论 抢沙发

3 + 7 =


iOS传值(代理传值,block 传值,单例传值,通知传值,属性传值)

长按图片转发给朋友

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏

Vieu4.0主题
专业打造轻量级个人企业风格博客主题!专注于前端开发,全站响应式布局自适应模板。

登录

忘记密码 ?