该 Pod 被用于
SHUIKitBlocks
,作为许多组件的一部分,覆盖从 Foundation、UIKit、CoreLocation、GameKit、MapKit 等方面的 iOS 应用程序架构中缺失的部分。
SHSegueBlocks 是 UIViewController 上的一种类别,允许基于块的 segue 不会涉及任何滥用和令人作呕的技巧 - 还添加了在 viewController 上设置 userInfo 的能力。它是建立在 NSMapTable 之上的,在控制器及其 block 和 userInfo 之间使用弱到弱的引用。
在这个过程中,没有任何开发者的心理或生命受到了伤害。我注意到其他类似的库都在像明天就要到来的样子 swizzle。如果 API 可以保持不变而不使用 Swizzle,那么就不应该使用 Swizzle。
总的来说,userInfo 和基于块的 segue 都使用了 100 行代码。一旦 segue 完成,块就不存在了,userInfo 内容在你将控制器设置为 nil 时或控制器消失后也会消失。
pod 'SHSegueBlocks'
将此内容放置在特定的控制器中或项目前缀文件中
#import "UIViewController+SHSegueBlocks.h"
或者
#import "SHSegueBlocks.h"
使用 SHSegueBlocks,您可以将所有操作都在一个地方完成,如下所示
[self SH_performSegueWithIdentifier:@"push"
andDestionationViewController:^(UIViewController * theDestinationViewController) {
theDestinationViewController.whateverPropety = anotherLocalVariable
}];
或者如果您想要访问完整的 segue 对象
[self SH_performSegueWithIdentifier:@"push"
andPrepareForSegueBlock:^(UIStoryboardSegue *theSegue) {
id<SHExampleProtocol> destionationController = theSegue.destinationViewController;
destionationController.name = theSegue.identifier;
}];
您可以直接在 segue 选择器上设置一个 userInfo(可变)字典,针对目标控制器
#import "SHObjectUserInfo.h"
[self SH_performSegueWithIdentifier:@"unwinder" withUserInfo:@{@"date" : [NSDate date]}];
在 destinationViewController
self.myDate = self.SH_userInfo[@"date"];
或者
[self SH_performSegueWithIdentifier:@"push"
andDestionationViewController:^(UIViewController * theDestinationViewController) {
theDestinationViewController.SH_userInfo = myDictionary
}];
如果您已经
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender;
在代码库中实现并使用,您可以使用块处理程序
-(BOOL)SH_handlesBlockForSegue:(UIStoryboardSegue *)theSegue;
如下所示
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender; {
UIViewController * destionationVc = segue.destinationViewController;
destionationVc.SH_userInfo = nil;
if([self SH_handlesBlockForSegue:segue])
NSLog(@"Performed segueue programatically user info: %@", destionationVc.SH_userInfo);
else
NSLog(@"Performed unwind segueue via IB");
}
这将检查是否存在块 和 如果存在,则执行它。
[self performSegueWithIdentifier:@"theIdentifier" sender:@"lolz"];
然后实现回调
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender; {
UIViewController * destinationViewController = segue.destinationViewController;
destionationViewController.whateverPropety = sender;
}
如果您在一个项目中使用了SHSegueBlocks,我非常想听到您的反馈。
电子邮件: [email protected]
推特:@seivanheidari
SHSegueBlocks版权© 2013 Seivan,可以在MIT许可下自由分发。请参阅LICENSE.md
文件。