iOS八种传值

来源:互联网 发布:怎样把东西放在淘宝卖 编辑:程序博客网 时间:2024/06/03 12:37

(1)NSUserDefaults传值

A

/***********NSUserDefaults传值****************/

        [[NSUserDefaultsstandardUserDefaults]setValue:_userNameText.textforKey:@"UserId"];

        [[NSUserDefaultsstandardUserDefaults]setValue:_passWordText.textforKey:@"PassWord"];

        //将缓存中的数据强制写入磁盘

        [[NSUserDefaultsstandardUserDefaults]synchronize];

        ZQMainVC *mainVC=[[ZQMainVCalloc]init];

         [self.navigationControllerpushViewController:mainVCanimated:YES];

  /***********NSUserDefaults传值****************/

B

    /**************NSUserDefaults传值**************/

    

    self.userName=[[NSUserDefaultsstandardUserDefaults]objectForKey:@"UserId"];

    self.passWord=[[NSUserDefaultsstandardUserDefaults]objectForKey:@"PassWord"];

    /************NSUserDefaults传值**********/

(2). 通知传值

B

    /***************通知传值****************/

    NSArray *array=[[NSArrayalloc]initWithObjects:_userNameText.text,_passWordText.text,nil];

    //发送通知  sendMessage表示通知详情 array表示传输数据

    [[NSNotificationCenterdefaultCenter]postNotificationName:@"ZQSendMessage"object:array];

    /*****************通知传值****************/

A

    /*****通知*********/

    [[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(getZQMessage:)name:@"ZQSendMessage"object:nil];

    /*****通知************/

(3).属性传值

A.

    /***********属性传值*******************/

        ZQMainVC *mainVC=[[ZQMainVCalloc]init];

        mainVC.userName=_userNameText.text;

        mainVC.passWord=_passWordText.text;

        [self.navigationControllerpushViewController:mainVCanimated:YES];

B.

(4).单例传值

   A

    //申明初始化单例类的对象

    ZQShareManager *shareManager = [ZQShareManagershareUserInfo];

    

    _userNameText.text = shareManager.userName;

    _passWordText.text = shareManager.password;

B

    //正向传值,直接从单例类里面读取数据

    ZQShareManager *shareManager = [ZQShareManagershareUserInfo];

    

    _userName = shareManager.userName;

    _passWord = shareManager.password;


+ (ZQShareManager *)shareUserInfo{

    static ZQShareManager *shareManager =nil;

    static dispatch_once_t predicate;

    dispatch_once(&predicate, ^{

        shareManager = [[self alloc] init];

    });

    return shareManager;

}

(5).代理传值

A.

-------------------------------------------------------------------------------------

@protocol ViewControllerDelegate <NSObject>


-(void)ViewControllerSendMessage:(NSString *)userName withPassword:(NSString *)password;


@end


@interface ViewController : UIViewController

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


@end

--------------------------------------------------------------------------------------

        ZQMainVC *mainVC=[[ZQMainVCalloc]init];

        self.delegate=mainVC;

        

        if (self.delegate&&[self.delegaterespondsToSelector:@selector(ViewControllerSendMessage:withPassword:)]) {

            [self.delegateViewControllerSendMessage:_userNameText.textwithPassword:_passWordText.text];

        }

        

        [self.navigationControllerpushViewController:mainVCanimated:YES];


B.

-(void)ViewControllerSendMessage:(NSString *)userName withPassword:(NSString *)password{

    

    _userName=userName;

    _passWord=password;

}

(6).初始化传值

A

  /***********初始化传值**************/

       

        ZQMainVC *mainVC=[[ZQMainVCalloc]initWithUserName:_userNameText.textWithPassWord:_passWordText.text];

        [self.navigationControllerpushViewController:mainVCanimated:YES];

        

  /***********初始化传值****************/

B.

-(id)initWithUserName:(NSString *)userName WithPassWord:(NSString *)passWord {

    self = [superinit];

        if (self) {

            // Custom initialization

            _passWord=passWord;

            _userName=userName;

        }

        return self;

   

}

(7).appDelegate传值

A.

- (void)viewDidAppear:(BOOL)animated

{

    //取出

    AppDelegate *appDelegate = [[UIApplicationsharedApplication]delegate];

    

    _userNameText.text = (NSString *)appDelegate.dataArray[0];

    _passWordText.text = (NSString *)appDelegate.dataArray[1];

    

    [super viewDidAppear:animated];

}

B.

    // 获取AppDelegate对象,   存入

    AppDelegate *appDelegate = [[UIApplicationsharedApplication]delegate];

    

    if (appDelegate.dataArray ==nil)

    {

        appDelegate.dataArray = [[NSArrayalloc]init];

    }

    

    appDelegate.dataArray = [NSArrayarrayWithObjects:_userNameText.text,_passWordText.text,nil];


(8).block传值

A

        ZQResignVC *resignVC=[[ZQResignVCalloc]init];

        //直接

        resignVC.block=^(NSArray *array){

            _userNameText.text=array[0];

            _passWordText.text=array[1];

        };

        //通过方法

//    [resignVC sendMessage:^(NSArray *array) {

//        _userNameText.text=array[0];

//        _passWordText.text=array[1];

//    }];

        [self.navigationControllerpushViewController:resignVCanimated:YES];

B.


#import <UIKit/UIKit.h>

// block格式: 返回值(^block名字)(参数)

// (1)定义block

typedef void(^myBlcok)(NSArray *array);

@interface ZQResignVC :UIViewController

// (2)申明block属性

@property (strong,nonatomic)myBlcok block;

-(void)sendMessage:(myBlcok)block;

@end

------------------------------------------------------------------------

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

    [selftextResignFirstResponder];

    

    NSArray *array=[[NSArrayalloc]initWithObjects:_userNameText.text,_passWordText.text ,nil];

    

    

    if (self.block)

    {

        self.block(array);

    }

    

       [self.navigationControllerpopToRootViewControllerAnimated:YES];

}



-(void)sendMessage:(myBlcok)block{

   

    self.block=block;


}





0 0
原创粉丝点击