您的位置:首页 > 移动开发 > IOS开发

iOS编程的几种传值方式

2016-09-28 12:46 288 查看

我们在设计两个界面之间进行交互时,难免就会遇到需要将上一个界面的值传入下一个界面中去使用的问题,我将我们可能会用到的传值方式进行了一下总结,希望可以帮助到盆友们。比如这里我们有一个ViewController和NextViewController两个控制器,后面用界面1和界面2代替。

一、初始化传值

初始化传值是一种很简单的传值方式,我们可以在界面跳转的事件中初始化目的界面时就将值传过去,假如这里我们需要传用户名,我们需要在界面2重写它的init方法,并用一个userName属性去接收(当然要将这个方法写到.h文件中去):

- (instancetype)initWithUserName:(NSString *)userName
{
self = [super init];
if (self) {
_userName = userName;
}
return self;
}
界面1的点击跳转方法中:

- (void)nextPage:(UIButton *)sender{
NextViewController *nextVC = [[NextViewController alloc]initWithUserName:"userName"];
[self showViewController:nextVC sender:nil];
}


这样我们就成功的将userName值传入了界面2了,当然这里我们可以传任意类型的值。

二、属性传值

属性传值是我们最常用也是最简单的传值方式了,加入我们还是传用户名,首先在界面2的.h文件中声明一个属性:

@property (nonatomic,strong)NSString *userName;
界面1的点击跳转方法中:

- (void)nextPage:(UIButton *)sender{
NextViewController *nextVC = [[NextViewController alloc]init];
nextVC.userName = @"userName";
[self showViewController:nextVC sender:nil];
}


三、单例传值

单例是生命周期与程序生命周期相同,仅能生成一次、且不能被销毁的唯一实例,通常我们会在登陆时创建一个单例用来记录当前是否处于登陆状态(Bool isLogin),这里我们就会用到单例的传值,将isLogin的值传递到我们需要判断是否登陆的任意界面:

单例类StoreUserInfo.h:

@property (nonatomic,assign)BOOL isLogin;

+ (StoreUserInfo *)defaultStoreUserInfo;
StoreUserInfo.m:

//创建单例
+ (StoreUserInfo *)defaultStoreUserInfo{
static StoreUserInfo *info = nil;
static dispatch_once_t onceToken = 0;
dispatch_once(&onceToken, ^{
info = [[StoreUserInfo alloc]init];
});
return info;
}
在我们登陆成功之后,保存登陆状态:

[StoreUserInfo defaultStoreUserInfo].isLogin = YES;
在需要用到时可以直接使用:

if ([StoreUserInfo defaultStoreUserInfo].isLogin) {
//登陆状态...
}else{
//未登录状态...
}
当然我们也可以在单例中声明其他属性,比如一个字典用来保存用户的所有信息,那么在工程中我们就都可以很容易的拿到用户信息了。

四、通知传值

这种传值方式在前面已经总结过了,OC编程中通知的使用1(传值)

五、代理传值

代理,说通俗点就是自己不能做的事情交由别人来做。通过代理我们可以在两个界面间进行相互传值,假如界面1有输入框content1,界面2有输入框content2;首先在界面2中声明属性:

@property (strong, nonatomic) UITextField *content2;

@property (weak, nonatomic) ViewController *delegate;//声明一个属性(容器)
界面1的点击跳转方法中:
- (void)nextPage:(UIButton *)sender{
NextViewController *nextVC = [[NextViewController alloc]init];
//推送之前将自身赋值给delegate
nextVC.delegate = self;
[self showViewController:nextVC sender:nil];
}
界面2一出现content2就显示content1的内容:

- (void)viewWillAppear:(BOOL)animated{
//通过属性引用拿上一页
self.content2.text = self.delegate.content1.text;//把上一个界面的值传递给当前界面
}
也可以在界面2返回到界面1时将content2中值传递到界面1的content1中:

- (void)viewWillDisappear:(BOOL)animated{
//将值传回上一页
self.delegate.content1.text = self.content2.text;
}
这样我们就做到了在一个页面编辑输入框,进入另一页面可以显示其内容了。

六、Block传值

Block是编程中常用的知识,但也是程序员们最不容易理解的一种传值方式,这里我们就以叫外卖的例子来加以说明,首先在界面2中声明block,并写一个方法来说明外卖费用(供外界调用):

#import <UIKit/UIKit.h>

typedef void(^CallBack) (NSString *money);
@interface NextViewController : UIViewController
- (void)getMoneyWithCallBlock:(CallBack)block;

@end

.m中写属性,包括输入框(需要传递的值)、block

@property (nonatomic,strong)UITextField *field;
//写block
@property (nonatomic,copy)CallBack callBlock;


假如现在我饿了:

- (void)viewWillDisappear:(BOOL)animated{
[self getHunger];
}
- (void)getHunger{
//调用block,传递输入框的值
_callBlock(self.field.text);
}

实现声明的方法:

#pragma mark -- public method
- (void)getMoneyWithCallBlock:(CallBack)block{
self.callBlock = block;
}
界面1中有一个属性label显示传递过来的值:

@property (strong, nonatomic) UILabel *label;
跳转方法中:

- (void)nextPage:(UIButton *)sender{
NextViewController *nextVC = [[NextViewController alloc]init];
__block NSInteger number = 0;//针对局部变量修改
__weak typeof (self) weakSelf = self;//修改属性时使用,为了避免重复引用持有
[nVC getMoneyWithCallBlock:^(NSString *money) {
weakSelf.label.text = [NSString stringWithFormat:@"%@",money];
number = [money integerValue];
number ++;
}];
[self showViewController:nextVC sender:nil];
}
这样我们就可以在界面1点击跳转,进入界面2编辑field,界面2返回界面1后,界面1的label上显示界面2中field所填的内容了。

总结:

今天总结了iOS编程中的几种传值方式,编程中传值处处存在,想要熟练地运用在自己的工程中,还是要经常练习才是正道。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ios 传值