Search code examples
iosxcodeuitableviewstoryboarddetailview

Detailed storyboard with 3 issued with


as implemented in the Xcode of a detailed storyboard with 3 issued with Tableview depending on id. Row in table hav Name and Id(int value)

If id= 1  i need load OneViewController
If id= 2 i need load TwoViewController
If id= 3 i need load ThreeViewController

I use Navigation controller from Storyboard


Solution

  • You need to add viewControllers to storyboard give them storyboardId same as that of their name.

    enter image description here

    Create a dataSourceArray which will hold dictionary objects with Id and Name values

    #define kItemId @"Id"
    #define kItemTitle @"Name"
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
    
        self.dataSourceArray = @[@{kItemId: @1,kItemTitle:@"First"},
                                 @{kItemId: @2,kItemTitle:@"Second"},
                                 @{kItemId: @3,kItemTitle:@"Third"}];
    
    }
    
    
    #pragma mark - Table view data source
    
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
        return 1;
    }
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        return self.dataSourceArray.count;
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *CellIdentifier = @"Cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    
        cell.textLabel.text = self.dataSourceArray[indexPath.row][kItemTitle];
    
        return cell;
    }
    
    #pragma mark - Table view delegate
    
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        NSDictionary *dict = self.dataSourceArray[indexPath.row];
    
        NSUInteger tag = [dict[kItemId]integerValue];
    
        NSString *identifier = nil;
    
        switch (tag) {
            case 1:
            {
                identifier = @"OneViewController";
                break;
            }
            case 2:{
                identifier = @"TwoViewController";
                break;
            }
            case 3:{
                identifier = @"ThreeViewController";
                break;
            }
    
            default:
                break;
        }
    
        UIViewController *viewController = [self.storyboard instantiateViewControllerWithIdentifier:identifier];
        viewController.title = identifier;
    
        [self.navigationController pushViewController:viewController
                                             animated:YES];
    
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
    }
    

    Source Code