1.通知传值
通知传值,是一种全局的传值手段, 能够在不同控制器,不同界面传值。
具体示例代码:
NSString *c1id = [NSString stringWithFormat:@"%@",thirtytagArr[indexPath.section][@"c1id"]];
NSString *c2id = [NSString stringWithFormat:@"%@",thirtytagArr[indexPath.section][@"c2id"]];
NSString *tapstr = [NSString stringWithFormat:@"%@",thirtytagArr[indexPath.section][@"taps"]];
NSArray *taparr = [tapstr componentsSeparatedByString:@","];
NSString *taps = [NSString stringWithFormat:@"%@,%@,%@",self.c2name,thirtytagArr[indexPath.section][@"name"],taparr[indexPath.row]];
NSDictionary *dict =[[NSDictionary alloc] initWithObjectsAndKeys:c1id,@"c1id",c2id,@"c2id",taps,@"taps", nil];
//创建通知
NSNotification *notification =[NSNotification notificationWithName:@"tongzhi" object:nil userInfo:dict];
//通过通知中心发送通知
[[NSNotificationCenter defaultCenter] postNotification:notification];
AddGoodViewController* oneVC =nil;
for(UIViewController* VC in self.navigationController.viewControllers){
if([VC isKindOfClass:[AddGoodViewController class]]){
oneVC =(AddGoodViewController *) VC;
[self.navigationController popToViewController:oneVC animated:YES];
}
}
- (void)viewDidLoad {
//注册通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(tongzhi:) name:@"tongzhi" object:nil];
}
- (void)dealloc{
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"tongzhi" object:nil];
}
2.单例的安全写法:static MySingleton *shareSingleton = nil;
+( instancetype ) sharedSingleton {
static dispatch_once onceToken;
dispatch_once ( &onceToken, ^ {
shareSingleton = [[super allocWithZone:NULL] init] ;
} );
return sharedSingleton;
}
+(id) allocWithZone:(struct _NSZone *)zone {
return [Singleton shareInstance] ;
}
-(id) copyWithZone:(struct _NSZone *)zone {
return [Singleton shareInstance] ;
}