mask8
mask8

Reputation: 3638

how to create a uiview with segues, which is used by multiple UIViewControllers in storyboard

Basically what I want to do is to include UIView to multiple UIViewControllers on storyboard. I could include the uiview, but segues in the UIView doesn't work. I have a storyboard something like this: enter image description here

I have a tab controller with 2 UIViewControllers First and Second. And I have a separated UITableViewController with two another UIViewControllers A and B, connected with segues.

I could add the table view into First and Second views as a subview, but when I tap cell it doesn't go to next screen A or B. I sort of figured out why it didn't work, but just can't figure the best way to accomplish this.

Is there any good way to do this? I'm new to storyboard but have been developing iOS app for a while.

EDIT:

The way I add the tableViewController to each View is following:

self.tableViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"theTableViewController"];
[self.view addSubview:_tableViewController.tableView];

When a cell in the table view is tapped, prepareForSegue:segue:sender is invoked, but no push to navigation controller since the table view controller is sitting in each view controller as just a subview

EDIT2:

I posted my test project here

Upvotes: 0

Views: 1558

Answers (2)

mask8
mask8

Reputation: 3638

I finally figured out how to make segue within sub views. It was actually very simple. It seems that I need to add the view controllers to the main view controller as a "childViewControllers"

instead of

self.tableViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"theTableViewController"];

I did like this:

[self addChildViewController:[self.storyboard instantiateViewControllerWithIdentifier:@"theTableViewController"];
[self.view addSubview:_tableViewController.tableView];

and segues in the childViewController work as I expect. Thank you guys for your help.

Upvotes: 0

Carina
Carina

Reputation: 2260

There are 2 methods you can try .

1.Create a segue between the tableview cell and next view controller directly. What I want to emphasize is the fact that do not create segue between the tableview or view and the next view controller,unless trying the method below.

2.Add this code to your tableview controller :

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([indexPath row]) {
        [self performSegueWithIdentifier:@"segueToB" sender:self];
    }else {
         [self performSegueWithIdentifier:@"segueToA" sender:self];
    }
}  

Upvotes: 1

Related Questions