lugonja
lugonja

Reputation: 313

iPhone open multiple view controllers on table view item click

Is it possible to open different view controllers depending on witch table view cell user clicks? I tried to do that with:

[self presentViewController:obj animated:YES completion:nil];

but when next view is presented, there is no navigation bar and I can't go back to table view.

EDIT:

Here is MasterViewController class that I am using

#import "MasterViewController.h"

@interface MasterViewController () {
    NSArray *viewArray;
}
@end

@implementation MasterViewController
@synthesize items,itemImges;

- (void)awakeFromNib
{
    if ([[[UIDevice currentDevice] systemVersion] compare:@"7" options:NSNumericSearch] != NSOrderedAscending) {
        self.preferredContentSize = CGSizeMake(320.0, 480.0);
    }
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
        self.clearsSelectionOnViewWillAppear = NO;
    }
    self.title = NSLocalizedString(@"MasterTitle",@"Options:");

    [super awakeFromNib];
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    items = [NSArray arrayWithObjects:@"Media Explorer",@"Live TV",@"Settings",nil];
    itemImges = [NSArray arrayWithObjects:
             [UIImage imageNamed:@"listicon_guide.png"],
             [UIImage imageNamed:@"listicon_livetv.png"],
             [UIImage imageNamed:@"listicon_settings.png"],
             nil];

// Do any additional setup after loading the view, typically from a nib.
    self.detailViewController = (DetailViewController *)[[self.splitViewController.viewControllers lastObject] topViewController];

    MediaExpDetailViewController *DVCA = [self.storyboard instantiateViewControllerWithIdentifier:@"MediaExpDetailViewController"];
    LiveTVDetailViewController *DVCB = [self.storyboard instantiateViewControllerWithIdentifier:@"LiveTVDetailViewController"];
    SettingsDetailViewController *DVCC = [self.storyboard instantiateViewControllerWithIdentifier:@"SettingsDetailViewController"];

    //Create Array of views
    viewArray = [NSArray arrayWithObjects:DVCA, DVCB, DVCC, nil];
}

#pragma mark - Table View

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return items.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

    NSString *object = items[indexPath.row];
    UIImage *image = itemImges[indexPath.row];
    cell.textLabel.text = [object description];
    cell.imageView.image = image;

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //for iPad
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
        //something goes here
    }
    else { //for iPhone
        UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main_iPhone" bundle:nil];
        MediaExpDetailViewController *objSynergy = (MediaExpDetailViewController *)[mainStoryboard instantiateViewControllerWithIdentifier:@"MediaExpDetailViewController"];
        [self.navigationController pushViewController:objSynergy animated:YES];
    }
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
}

@end

Upvotes: 3

Views: 4484

Answers (3)

Ali Sufyan
Ali Sufyan

Reputation: 2046

First set Storyboard IDs for your Next View controllers in Interface Builder and then.

-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
   // Check Row and Select Next View controller
   if (indexPath.row == 1)
   {     
       // Push Selected View
       UIViewController *view1 = [self.storyboard instantiateViewControllerWithIdentifier:@"StoryboardID"];
       [self.navigationController pushViewController:view1 animated:YES];
   }
}

Upvotes: 10

Tamás Zahola
Tamás Zahola

Reputation: 9321

Modally presented view controllers don't have navigation bars by default. You have to embed them in a UINavigationController, in order to have one. You should also implement how to dismiss the presented view controller by yourself, calling dismissViewControllerAnimated at the appropriate times.

However, I'd recommend to push your view controllers (with pushViewControllerAnimated), instead of presenting them modally, if you don't specifically need the modal functionality.

Upvotes: 0

Mick MacCallum
Mick MacCallum

Reputation: 130193

That's because

[self presentViewController:obj animated:YES completion:nil];

Presents the new view controller modally (over top of the existing visible controller). If you want to push to a new view controller using your navigation controller, you'll want to use this. Of course you'll want to make sure that the view controller you're pushing from is embedded within a UINavigationController.

[self.navigationController pushViewController:obj animated:YES];

And to answer your first question, yes it absolutely is possible. Just add some condition logic to your didSelectRowAtIndexPath: UITableViewDelegate method.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (someCondition) {
        [self.navigationController pushViewController:obj animated:YES];
    }else{
        [self.navigationController pushViewController:otherObj animated:YES];
    }
}

Upvotes: 0

Related Questions