你是否厌倦了编写一个丑陋且庞大的方法 -prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender:
?
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"segue1"]) {
UIViewController *destination = segue.destinationViewController;
destination.view.backgroundColor = [UIColor redColor];
} else if ([segue.identifier isEqualToString:@"segue2"]) {
UIViewController *destination = segue.destinationViewController;
destination.view.backgroundColor = [UIColor blueColor];
} else if ([segue.identifier isEqualToString:@"segue3"]) {
UIViewController *destination = segue.destinationViewController;
destination.view.backgroundColor = [UIColor greenColor];
}
...
}
检查 JMGBlockSegue,并享受在调用 performSegue
的相同地方 定义一个将要执行的块 的乐趣。
使用 JMGBlockSegue 非常简单,您只需要将 JMGBlockSegue 文件夹
放入您的项目中,或者如果您是 CocoaPods 的爱好者,您可以在 Podfile 中使用下列行,就像平常一样
pod 'JMGBlockSegue'
之后,您需要在需要配置转场的一些视图控制器中导入 UIViewController+BlockSegue.h
。
JMGBlockSegue 可以以两种方式使用:内联(在您以编程方式执行 segue 时)或独立(在其他行)。
执行 segue 并在相同语句中配置要执行的块。
[self performSegueWithIdentifier:@"segueIdentifier" sender:nil withBlock:^(id sender, id destinationVC) {
NSLog(@"Segue configured inline");
destinationVC.user = tmpUser;
}];
独立于其执行配置 segue 块,这种方式可以用于 viewDidLoad 等方法调用中,并随着调用 performSegueWithIdentifier:sender: 方法以及从 storyboard 执行的转场来运行。
[self configureSegue:@"segueIdentifier" withBlock:^(id sender, id destinationVC) {
NSLog(@"I'm a block fired with the segue!");
}];
我添加了一些测试作为个人练习,但如果您要运行它们,请执行以下脚本来运行(它使用 supermarin 的 xcpretty)。
./run_tests.sh
runtime.h
的支持,谢谢兄弟!。JMGBlockSegue
基于 MIT 许可证提供。有关更多信息,请参阅 LICENSE 文件。