在iOS中也切,常見的傳值方式有以下幾種:
1.屬性傳值
2.單例傳值
3.通知傳值
4.代理傳值
5.Block
這些傳值方式,各有各自的優(yōu)勢,下面我們來具體看看這幾種傳值方式的實現(xiàn)方法:
1.屬性傳值
屬性傳值第一步就得確定傳值得類型,然后定義屬性
例子:點擊MainViewController 中的一個按鈕跳到SecondViewContrller界面并且傳一個值到 SecondViewContrller
代碼實現(xiàn):
#import <UIKit/UIKit.h>
@interface SecondViewController : SecondViewController
@property(nonatomic,retain)NSString *nameStr;
@end
在MainViewController中實現(xiàn)如下代碼
-(void) name
{
SecondViewController *second = [[SecondViewController alloc] init];
Second.nameStr = @”pegboggs”;
[Self.navigationController pushViewController:second animated:YES];
}
2.單例傳值
單例只會對某個類實例化一次/單例類线召,對單例這個類實例化一次有且僅有一個對象,單例初始化多矮,只能初始化一次缓淹,然后指向?qū)ο蠊颍鋵嵍际侵赶蛞粋€內(nèi)存地址,也就是同一塊內(nèi)存讯壶,所以都是一樣的/
那么料仗,只能有一個對象,就是實例化的那個
單例方法使用很簡單伏蚊,就是相當(dāng)于定義了整個工程使用的變量立轧,在整個工程中可以隨時調(diào)用,隨時更改躏吊,全局唯一氛改。主要實現(xiàn)代碼如下:
.h文件中
@interface Single : NSObject
+(Single*) sharedInstance;
-(id)init;
@end
.m文件中
#import "Single.h"
static Single *instance = nil;
@implementation Single
+(Single*) sharedInstance
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken,^{
instance = [[Single alloc] init];
});
return instance;
}
-(id) init
{
if (self = [super init]) {
}
return self;
}
-(id) copyWithZone:(NSZone*)zone
{
return instance;
}
+(id) allocWithZone:(struct _NSZone *)zone
{
static dispatch_once_t onceToken = 0;
dispatch_once(&onceToken,^{
instance = [super allocWithZone:0];
});
return instance;
}
@end
3.通知傳值
NSNotificationCenter提供了一種更加解耦的方式。最典型的應(yīng)用就是任何對象對可以發(fā)送通知到中心比伏,同時任何對象可以監(jiān)聽中心的通知胜卤。
發(fā)送通知的代碼如下:
[[NSNotificationCenter defaultCenter] postNotificationName:@”myNotificationName” object:broadcasterObject];
注冊接收通知的代碼如下:
[[NSNotificationCenter defaultCenter] addObserver:listenerObject selector:@selector(receivingMethodOnListener:) name:@”myNotificationName” object:nil];
4.代理傳值
這種傳值主要用于A進(jìn)入B,然后B輸入值后傳回給A赁项,比較常見的就是用于修改個人信息葛躏,點擊進(jìn)入修改界面,修改完之后回到顯示界面悠菜,顯示修改后的結(jié)果(以前我都是用單例舰攒,雖然也可以實現(xiàn),但是明顯這種方法更加實用)李剖。
在第二個界面里實現(xiàn)協(xié)議芒率,并設(shè)置代理囤耳,第一個界面實現(xiàn)協(xié)議方法篙顺,并在跳轉(zhuǎn)的時候,設(shè)置代理為本身充择,在第二個界面德玫,修改數(shù)據(jù)的時候,執(zhí)行代理方法椎麦,實現(xiàn)數(shù)據(jù)傳遞
代碼例子:
第一個界面.h文件
#import <UIKit/UIKit.h>
#import "FirstViewController.h"
@interface ViewController : UIViewController<theValue>
@property (nonatomic,strong) UILabel *nameLab;
@property (nonatomic,strong) UILabel *ageLab;
@end
第一個界面.m中
#import "ViewController.h"
#import "FirstViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize nameLab,ageLab;
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(100, 100, 100, 30)];
[btn setTitle:@"跳轉(zhuǎn)" forState:UIControlStateNormal];
[btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[btn addTarget:self action:@selector(btn) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
nameLab = [[UILabel alloc] initWithFrame:CGRectMake(10, 200, 100, 30)];
nameLab.text = @"name";
[self.view addSubview:nameLab];
ageLab = [[UILabel alloc] initWithFrame:CGRectMake(10, 250, 100, 30)];
ageLab.text = @"age";
[self.view addSubview:ageLab];
}
-(void) btn
{
FirstViewController *first = [[FirstViewController alloc] init];
first.delegate = self;
[self presentViewController:first animated:YES completion:nil];
}
//協(xié)議實現(xiàn)
-(void) passValue:(NSString *)value
{
nameLab.text = value;
ageLab.text = value;
}
第二個界面.h文件
#import <UIKit/UIKit.h>
@protocol theValue <NSObject>
-(void)passValue:(NSString*)value;
@end
@interface FirstViewController : UIViewController
@property (nonatomic,strong) UITextField *nameField;
@property (nonatomic,strong) UITextField *ageField;
@property (nonatomic,assign) NSObject<theValue> *delegate;
@end
第二個界面.m文件
#import "FirstViewController.h"
@interface FirstViewController ()<theValue>
@end
@implementation FirstViewController
@synthesize nameField,ageField;
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(100, 400, 100, 30)];
[btn setTitle:@"dismis" forState:UIControlStateNormal];
[btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[btn addTarget:self action:@selector(btn) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
nameField = [[UITextField alloc] initWithFrame:CGRectMake(10, 100, 100, 30)];
nameField.placeholder = @"name";
[self.view addSubview:nameField];
ageField = [[UITextField alloc] initWithFrame:CGRectMake(10, 200, 100, 30)];
ageField.placeholder = @"age";
[self.view addSubview:ageField];
}
-(void) btn
{
[self.delegate passValue:nameField.text];
[self dismissViewControllerAnimated:YES completion:nil];
}
5.Block
首先在第二界面的.h文件里定義聲明block屬性
typedef void (^ReturnTextBlock)(NSString *showText);
@property (nonatomic, copy) ReturnTextBlock returnTextBlock;
-(void) returnText :(ReturnTextBlock)block;
第二個界面.m文件中
-(void) returnText:(ReturnTextBlock)block{
self.returnTextBlock = block;
}
把傳進(jìn)來的Block語句塊保存到本類的實例變量returnTextBlock
最后在第一個視圖中宰僧,獲得第二個視圖的控制器,并且用第二個視圖控制器來調(diào)用定義的屬性
-(void) btn
{
FirstViewController *first = [[FirstViewController alloc] init];
[first returnText:^(NSString *showText) {
nameLab.text = showText;
}];
[self presentViewController:first animated:YES completion:nil];
}