Git Product home page Git Product logo

uiviewcontroller-blocksegue's Introduction

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.

uiviewcontroller-blocksegue's People

Contributors

mpurland avatar patoroco avatar readmecritic avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

uiviewcontroller-blocksegue's Issues

Sometimes you need the segue

Hello!
Trying to use this class in combination of WYPopoverController but is not possible because I need the segue to configure the popover properly:

[code]

  • (void)prepareForSegue:(UIStoryboardSegue )segue sender:(id)sender
    {
    if ([segue.identifier isEqualToString:NSStringFromClass(SWMapTypeTableTableViewController.class)]) {
    WYStoryboardPopoverSegue
    popoverSegue = (WYStoryboardPopoverSegue_)segue;
    SWMapTypeTableTableViewController_ destinationViewController = (SWMapTypeTableTableViewController _)segue.destinationViewController;
    destinationViewController.preferredContentSize=CGSizeMake(200, 44_3);
    destinationViewController.delegate=self;
    destinationViewController.currentType=_mapView.mapType;
    _mapTypePopoverController = [popoverSegue popoverControllerWithSender:sender permittedArrowDirections:WYPopoverArrowDirectionAny animated:YES];
    _mapTypePopoverController.delegate = self;
    }
    }
    [/code]

That cannot be implemented in:
-(void)configureSegue:(NSString *)identifier withBlock:(UIViewControllerSegueBlock)block

Maybe you could add the segue as a parameter in the block.
Thanks.

It does not work if prepareForSegue is implemented

Hello!
I'm using your category to jump from a controller to another.
However, my origin controller uses

  • (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
    for another third controller.
    I realised if that is used, then your category does not work properly (the block is not called). You should say that in the docs or let the caegory be compatible with both. I spent a while until I discovered the problem.
    Anyway, thanks a lot for your code!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.