Latest 1.0.0
Homepage https://github.com/patoroco/UIViewController-BlockSegue
License MIT
Platforms ios 5.0, requires ARC
Frameworks UIKit
Authors

CocoaPods Version
CocoaPods Platform
Build Status

Are you tired of writing an ugly and big -prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender: method like that?


- (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];
    }
    ...
}

Check UIViewController+BlockSegue and enjoy defining a block which will be executed to prepare segue in the same place which you call to performSegue.

IMPORTANT: if you override -prepareForSegue:sender: method, BlockSegue won’t be performed in these UIViewController.

Install

Use UIViewController+BlockSegue category it’s really easy, you only need to drop UIViewController+BlockSegue folder to your project, or if you are a CocoaPods-lover, you can include this line in your Podfile as usual:

pod 'UIViewController+BlockSegue'

After that, you need to import UIViewController+BlockSegue.h on each view controller where you want to configure segues.

Use

UIViewController+BlockSegue can be used in two ways: inline (when you performSegue programatically) or independently (in other line).

Inline

Perform segue and configure the block which is executed in the same sentence.


[self performSegueWithIdentifier:@"segueIdentifier" sender:nil withBlock:^(id sender, id destinationVC, UIStoryboardSegue *segue) {
    NSLog(@"Segue configured inline");
    destinationVC.user = tmpUser;
}];

Independently

Configure segue block independently of his execution, this way could be called on viewDidLoad for example, and it’s run with both the call performSegueWithIdentifier:sender: method and storyboard segue perform.


[self configureSegue:@"segueIdentifier" withBlock:^(id sender, id destinationVC, UIStoryboardSegue *segue); {
    NSLog(@"I'm a block fired with the segue!");
}];

Testing

I added some tests as personal exercise, but if you want run them, exec script (it uses xcpretty by supermarin):

./run_tests.sh

Acknowledges

Author

License

UIViewController+BlockSegue is available under the MIT license. See the LICENSE file for more info.

Latest podspec

{
    "name": "UIViewController+BlockSegue",
    "version": "1.0.0",
    "summary": "UIViewController category which allows to use a block to prepare segue instead of ugly -prepareForSegue method.",
    "homepage": "https://github.com/patoroco/UIViewController-BlockSegue",
    "license": {
        "type": "MIT",
        "file": "LICENSE.md"
    },
    "authors": {
        "Jorge Maroto Garcia": "[email protected]"
    },
    "source": {
        "git": "https://github.com/patoroco/UIViewController-BlockSegue.git",
        "tag": "1.0.0"
    },
    "platforms": {
        "ios": "5.0"
    },
    "source_files": "UIViewController+BlockSegue",
    "frameworks": "UIKit",
    "requires_arc": true
}

Pin It on Pinterest

Share This