2

I am new to iOS development, with a couple of years of Android experience. I started directly with XCode 5 and the Storyboard paradigm. I like the visual approach to sketching out the flow of an app, but IMHO, it does not really force component reuse, or maybe I do not know how to do it.

I have an actual problem, which is the following: Instead of the typical master-detail approach, I have a situation, in which clicking on a TableView cell forces a push to another TableView, which looks the same and behaves the same way. There are two specific types of a TableViewCell that I want to reuse across the whole application, so I can't just duplicate the first TableViewController several times. I need changes in one type of TableViewCell to be affected everywhere, same for the look and behaviour of the Tableview. Sort of like reusing a component, you get the picture I hope.

I tried creating a custom TableView and TableViewCell in a separate xib file, connecting it to a custom controller class. Yet, when I want to reuse that controller class in the storyboard, I can't make a segue from the cell to the next one, because only view controllers are displayed, but no views inside.

Maybe, I am doing it all wrong. Perhaps, I should make a single controller, and force it to seque to itself if possible, no idea.

What would you do?

Preslav Rachev
  • 3,983
  • 6
  • 39
  • 63
  • could you try and be more concrete? add some code about what you are trying to do... – sergio Dec 13 '13 at 11:09
  • Hi. Are you using only XIB for cells? Or you also have custom classes? – Mário Carvalho Jan 22 '14 at 16:34
  • Welcome to the wonderful world of iOS development! I would advise to to reconsider whether you really want to use Storyboards: http://stackoverflow.com/questions/9404471/when-to-use-storyboard-and-when-to-use-xibs/19457257#19457257 – Johannes Fahrenkrug Jan 29 '14 at 15:01

3 Answers3

0

You can do programatic Segue from the didselected.....

#import "someVC.h"

Then when you want to Segue to the new VC

// Create a VC and remember to set the Storyboard ID of someVC (example someVCID)
someVC *newView = [self.storyboard instantiateViewControllerWithIdentifier:@"someVCID"];

// Can't remember how this works but you can change the Transition method
// newView.modalTransitionStyle = UIModalTransition;

// If you want to pass data, setup someArray in the someVC .h with @property (nonatomic, strong) NSArray *someArray;
newView.someArray = MyLocalArray;

// Animated or not
[self presentViewController:newView animated:NO completion:nil];
Recycled Steel
  • 2,272
  • 3
  • 30
  • 35
  • +1. This is also the solution I would recommend, but just to not confuse the OP, I wanted to point out that this does not use a segue at all. It's the older way to use `UINavigationController`, where you just instantiate the next VC and manually push it. There is nothing wrong with dropping to the lower level when segues don't fit your needs! – Taum Jan 22 '14 at 15:47
  • Well if OP is actually using `UINavigationController` the last line should be `[self.navigationController pushViewController:newView animated:YES]` :) – Taum Jan 22 '14 at 15:49
0

If what you're trying to do is performing a segue on the same UITableView you can check this answer by Rob

I'll report what it contains for completeness:

If you're using dynamic cell prototypes, you obviously can do a segue from the table view cell to the controller without any problem.

make segue from cell to controller

When you make your segue, you end up with:

circular segue

But let's imagine for a second that there's some reason that doesn't work for you, e.g. you could not have a segue from the cell (for example, you need to invoke segue from didSelectRowAtIndexPath, not upon selecting the cell). In this case, you couldn't use that previous technique.

There are a couple of options in this case:

  1. As pointed out by Chris, if only supporting iOS 6 and above, use the above technique, but (a) make sure your didSelectRowAtIndexPath uses self for the sender and then have shouldPerformSegueWithIdentifier only allow the segue if the sender == self (thus when the sender is the table view cell, it will be canceled);

  2. Perhaps even easier, just don't define a segue at all and have your didSelectRowAtIndexPath manually instantiateViewControllerWithIdentifier and then push/present that view controller as appropriate; or

  3. You can use the following, kludgy work-around: In short, add a button to your view controller (drag it down to the bar at the bottom), where it won't show up on the view, but you can use its segues. So first, drag the rounded button to the controller's bar at the bottom of the scene:

    drag button

    Now you can make a segue from that button to your view controller:

    make segue from hidden button

    And thus, you end up with the self-referential segue again:

    final button segue

    You must give that segue an identifier, so you can invoke it programmatically via performSegueWithIdentifier, but it works. Not the most elegant of solutions, but I think it's better than having an extra scene on your storyboard.

None of these are ideal, but you have lots of options.

Community
  • 1
  • 1
Antonio E.
  • 4,381
  • 2
  • 25
  • 35
0

well.. in iOs think about it differently, if i were you i would create multiple ViewController, even if they're almost the same, and create a custom cell class, and make this cell take a configuration block. Now for each ViewController (( or TableViewController )), you reuse your same custom UITableViewCell and just pass it what's slightly different for each case, and moreover you can also create a BaseTableViewController that will have the general configuration, and in each ViewController pass the custom changes you need.

Think about it, when you look at your storyboard you'll be able to see all your workflow, and when something goes wrong you'll be able to debug, if you have are gonna use what you suggested, debugging will be a real pain i imagine.

Anyway, try it out, and ask more if you need further clarification.

Nour Helmi
  • 705
  • 1
  • 6
  • 17