VenushkaT
VenushkaT

Reputation: 1148

Redirect to different ViewControllers according to segue

I'm new to iOS and its developing.I have tableview and two cells.when i press the cell i want to redirected to viewController object.i want to do it according to segue identifier.please find below the code i used.

   #import "Essentialinfocontroller.h"

    @interface Essentialinfocontroller ()
    {

        NSMutableArray * titlearray;

        NSMutableArray * subtitilearray;

    }
    @end

    @implementation Essentialinfocontroller

    @synthesize info;


    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {
        self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil\];
        if (self) {

        }
        return self;
    }

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        self.info.delegate=self;
        self.info.dataSource=self;

        titlearray =[[NSMutableArray alloc]initWithObjects:@"Custom",@"Department Of Immigration",@"Foriegn Currency Regulations",@"Sri Lankan Embassies" ,@"Sri Lankan Visa",@"When You Are There..", nil];

    }

    -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

    {

        return 1;

    }

    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

    {

        return [titlearray count];

    }

    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

    {

        static NSString * cellidentifier=@"cell";

        UITableViewCell * cell= [info dequeueReusableCellWithIdentifier:cellidentifier forIndexPath:indexPath];



        cell.textLabel.text =[titlearray objectAtIndex:indexPath.row];

        return cell;



    }

    -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
    {
        if([segue.identifier isEqualToString:@"custom"])
        {
            //redirected to Viewcontroller custome
        }
        else if([segue.identifier isEqualToString:@"depart"])
        {
            // redirected to viewController Department
        }
    }

@end][1]

My story Board

Upvotes: 0

Views: 492

Answers (1)

Dhrumil
Dhrumil

Reputation: 3204

Use this method at the point from where you want to segue to a new Controller.

[self performSegueWithIdentifier:@"yourSegueIdentifierName" sender:nil];

This method will automatically trigger the prepareForSegue method as soon as the above statement is encountered and as mentioned in your prepareForSegue method, you are checking the segue identifier names using if..else so that should do it.

Follow these steps :

  1. Use the didSelectRowAtIndexPath delegate method to check which row in the tableView was clicked. This is to know which segue identifier are we going to use for the particular row. Now you have your selectedRow index. So we go to next step.
  2. After knowing which row is being clicked, use this function for redirecting to the appropriate controller with the segue Identifer from your source Controller.

    [self performSegueWithIdentifier:@"yourSegueIdentifierName" sender:nil];

  3. As soon as your performSegueWithIdentifier is called, the prepareForSegue will be called immediately after it. And in that, as you have done right now, you can check the segue Identifier and use your own logic in that if...else code.

Upvotes: 1

Related Questions