justin_graham92
justin_graham92

Reputation: 617

If statement based on whatever the previous view controller was

Here is what I go so far:

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if (preivousViewController isEqualToString:@"...") 
    {
        if ([self.filteredArray count] == 0)
            self.person = self.users[indexPath.row];
        else
            self.person = self.filteredArray[indexPath.row];
        self.mugshot = cell.imageView.image;

        [self performSegueWithIdentifier:@"SelectPerson" sender:self];
    } else {
        if ([self.filteredArray count] == 0)
            self.person = self.users[indexPath.row];
        else
            self.person = self.filteredArray[indexPath.row];
        self.mugshot = cell.imageView.image;

        [self performSegueWithIdentifier:@"selectVisitee" sender:self];
    }
}

It looks messy, that's why im trying to fix it up (that previousViewController part is just made up to show you what I want to try to do).

What im trying to say is: If how you got to this view was via this segue, or from that view controller with the name suchandsuch, then preform this segue or that segue. Is there a way I can do that?

Upvotes: 0

Views: 128

Answers (1)

DilumN
DilumN

Reputation: 2895

You can check the previous ViewController of the stack from the Navigation Controller.

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{
        UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

           if ([self.filteredArray count] == 0)
                self.person = self.users[indexPath.row];
            else
                self.person = self.filteredArray[indexPath.row];
            self.mugshot = cell.imageView.image;

            //Not the viewcontroller name string. Use the ViewController class name
            if ([self backViewController] == YOURVIEWCONTROLLER) 
           {

            [self performSegueWithIdentifier:@"SelectPerson" sender:self];

        } else {

        [self performSegueWithIdentifier:@"selectVisitee" sender:self];

        }
}

Use this below method,

- (UIViewController *)backViewController
{
    NSInteger numberOfViewControllers = self.navigationController.viewControllers.count;

    if (numberOfViewControllers < 2)
        return nil;
    else
        return [self.navigationController.viewControllers objectAtIndex:numberOfViewControllers - 2];
}

Upvotes: 1

Related Questions