5

Xcode 4.6.1 iOS 6 using storyboards

My problem is this

I have a UITableView with dynamic prototype cells on a UIView in a UIViewController (that is itself embedded in a navigation controller) and I want to segue from one specific cell to another view

(Before anyone suggests I should just be using a UITableViewController , I do have other things on the UIView, so i'm set up this way for a reason.)

Now i'm not sure how to go about creating the segue

If I drag from the prototype UITableViewCell to create a segue , all the generated cells automatically call the the segue - when i need only one to do so. This is normal behaviour and I would get around this if i was using a UITableViewController by creating the segue by dragging from UITableViewController and calling [self performSegueWithIdentifier:.... From my didSelectRowAtIndexPathMethod so only the specific cell I want to perform this segue triggers it.

I don't have a UITableViewController in this case - just my UITableView on a UIView that is part of a UIViewController subclass

I've been playing around and I have just discovered that i cannot drag from the UITableView - doesn't let you do that, so that was a deadend.

My only choice that seemed left to me was to drag from the UIViewController

So i tried that and of course XCode throws up an error on the perform segue line telling me i have ... No visible interface for 'LocationTV' declares the selector performSegueWithIdentifier. LocationTv being my tableview subclass.

What is the correct way to attempt to call the new view in this situation

Thank

Simon

SimonTheDiver
  • 1,158
  • 1
  • 11
  • 24

3 Answers3

5

First of all segues can be use only between UIViewControllers. So in case you want to perform a segue between two views that are on the same view controller, that's impossible.

But if you want to perform a segue between two view controllers and the segue should be trigger by an action from one view (inside first view controller) well that's possible.

So in your case, if I understand the question, you want to perform a segue when the first cell of a UITableView that's inside of a custom UIView is tapped. The easiest approach would be to create a delegate on your custom UIView that will be implemented by your UIViewController that contains the custom UIView when the delegate method is called you should perform the segue, here is a short example:

YourCustomView.h

@protocol YourCustomViewDelegate <NSObject>

-(void)pleasePerformSegueRightNow;

@end

@interface YourCustomView : UIView {
   UITableView *theTableView; //Maybe this is a IBOutlet
}
@property(weak, nonatomic) id<YourCustomViewDelegate>delegate;

YourCustomview.m

@implementation YourCustomview
@ synthesise delegate;

//make sure that your table view delegate/data source are set properly
//other methods here maybe

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if(indexPath.row == 0) {  //or any other row if you want
       if([self.delegate respondsToSelector:@selector(pleasePerformSegueRightNow)]) {
          [self.delegate pleasePerformSegueRightNow];
       }
    }
} 

YourTableViewController.h

@interface YourTableViewController : UIViewController <YourCustomViewDelegate> {
  //instance variables, outlets and other stuff here
}

YourTableViewController.m

@implementation YourTableViewController

-(void)viewDidLoad {
  [super viewDidLoad]; 
  YourCustomView *customView = alloc init....
  customView.delegate = self;
}

-(void)pleasePerformSegue {
  [self performSegueWithIdentifier:@"YourSegueIdentifier"];
}

You can create any methods to your delegate or you can customise the behaviour, this is just a simple example of how you can do it.

danypata
  • 9,895
  • 1
  • 31
  • 44
  • Hi danypata - managed to get a working piece of code together by defining a protocol in my UITableView subclass and making the UIViewController conform to the protocol and using a method similar to your suggested pleasePerformSegueto call the performSegue method on the I did get confused by your answer as I dont subclass the UIView on the UIViewController but i got there in the end so cheers for that – SimonTheDiver Jun 14 '13 at 13:18
  • @SimonTheDiver if one of the two answers posted for your question, is the right one please mark it as accepted, or if you have a better one, post it and mark is as accepted ;) – danypata Jun 14 '13 at 13:41
  • Don't worry - i will - I'm just trying to see if i can make allprogs suggestion work before i plump for one or the other or do a detailed coded example myself – SimonTheDiver Jun 14 '13 at 13:48
3

My Solution

I ended up using a delegation pattern

I made a segue dragging from the my UIViewController - specifically dragging from the viewController icon (the orange circle with a white square in it - from the name bar thats under the view in the storyboard - although you could also drag from the sidebar ) to the view that i wanted to segue to.

I needed to trigger this segue from a table view cell on a table view.

TableView Bit

So i declared a protocol in my tableview header file - which is called LocationTV.h - as follows

@protocol LocationTVSegueProtocol <NSObject>

-(void) makeItSegue:(id)sender;

@end

Below that I declare a property to hold my delegate

@property (nonatomic, strong) id<LocationTVSegueProtocol> makeSegueDelegate;

To actually trigger the segue i called the makeItSegueMethod on my makeSequeDelegate in my didSelectRowAtIndexPath method

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

switch (indexPath.section) {
        DLog(@"selected row %d",indexPath.row);
    case dLocation:
    {
        if(indexPath.row == 2){

            [_makeSegueDelegate makeItSegue:self];

        } else if (indexPath.row == 7){

UIViewController Bit

and set up my UIViewController (named MultiTableHoldingVC) as implementing that protocol

@interface MultiTableHoldingView : UIViewController    
                                  <EnviroTVProtocol,LocationTVSegueProtocol> {

}

Below that i declared the protocol method in the list of my classes methods (although i'm not sure that is necessary as the compiler should know about the method as the decalration of implementing a protocol is essentially a promise to implement this method)

-(void) makeItSegue:(id)sender;

And then over in the implementation file of my UIViewController i wrote the method which essentially just calls preformSegueWithIdentifier

-(void) makeItSegue:(id)sender{ 
    [self performSegueWithIdentifier:@"ChooseCountryNow" 
                              sender:sender];   
}

And to link it all together,as in the header file I had declared my instance of the tableView as follows

@property (strong, nonatomic) IBOutlet LocationTV *dsLocationTV;

I had to set that tables views delegate property to be self - which I did in my UIViewControllers -(void)ViewDidLoad method

_dsLocationTV.makeSegueDelegate = self;

It all seems a bit of a kludge calling a method to call a method and allprog suggestion is simpler (I cant for the life of me work out why it threw up errors for me) but this works just fine . Thanks to both allprog and danypata for their suggestions.

Hope this is helpful to someone out there

Alex Cio
  • 6,014
  • 5
  • 44
  • 74
SimonTheDiver
  • 1,158
  • 1
  • 11
  • 24
0

performSegueWithIdentifier: is a method of the UIViewController class. You cannot call it on a UITableView instance. Make your view controller implement the UITableViewDelegate protocol and set it as the delegate for the UITableView.

Another option is that you don't use segues. In the same delegate method do:

OtherViewController ov = [[OtherViewController alloc] init<<some initializer>>];
// Or in case of storyboard:
OtherViewController ov = [self.storyboard instantiateViewControllerWithIdentifier:@"ovidentifier"];

// push view controller
[self.navigationController pushViewController:ov animated:YES];

If the delegate object is different from the view controller, then the easiest solution is to add a weak property to the delegate's class that keeps a reference to the viewController, like this:

@property (weak) UIViewController *viewController;

and set it up in the viewDidLoad of the viewController

- (void) viewDidLoad {
    self.tableView1.viewController = self;
}

Make sure that the tableView1 property is declared like this:

@property (IBACTION) (weak) SpecialTableView *tableView1;

Sometimes using the storyboard is more painful than writing the code yourself.

allprog
  • 16,540
  • 9
  • 56
  • 97
  • My tableview is already its own UITableViewDelegate . (I have 4 different tableviews that i used a UISegmentedCntrol to switch the hidden properties of on and off so only one appears at a time. - its one of those tableviews that needs to call the new view). Does this mean ill need to shift all the delegate stuff from the UITableView onto the UIViewController its sat on ? – SimonTheDiver Jun 14 '13 at 08:50
  • So you created specialized UITableView classes. These classes must have a way to access the ViewController they are running in or you have to create an implementation similar to what the other answer proposes. I'd go for simplicity and create a property (call it `viewController`) on the UITableview subclasses that contain a reference to the ViewController. In the `didSelectRowAtIndexPath:` you can call `[self.viewController performSegueWithIdentifier:identifier]`. The `viewController` property should be set in the `viewDidLoad` of your view controller. – allprog Jun 14 '13 at 09:14
  • I defined a protocol(with method to call performSegue) inside mytableView.h & made my UIViewContoller subclass conform to it. Similar to danypata suggested.Worked. Your idea seems simpler & I want to try it. I imported the UIViewController class - #import "MultiTableHoldingView.h" into my UItableView subclass but when i tried to declare it in the interface of my tableView subclass- @property (nonatomic,strong) MultiTableHoldingView* theParentViewController; -i get2 errors: Property with retain(or strong) attribute must be off object type & Unknown type name 'MultiTableHoldingView'. ideas? – SimonTheDiver Jun 14 '13 at 13:34
  • MultitableHoldingView is your view controller? I can't say much without seeing your code. Can you send a gist or something? Additionally, the property MUST be weak otherwise you'll create a retain cycle that will result in memory leak. – allprog Jun 14 '13 at 13:58
  • @SimonTheDiver did you succeed? – allprog Jun 17 '13 at 22:17
  • It works fine with the protocol i set up - I havent had the time to get back onto it and try what should be your simpler suggestion . I'm off to a NSCoder night tonight at the pub in London so I figured I'd chat to some of the guys there first – SimonTheDiver Jun 18 '13 at 06:54