If statement based on whatever the previous view controller was

无人久伴 提交于 2019-12-25 03:38:45

问题


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?


回答1:


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];
}


来源:https://stackoverflow.com/questions/34432800/if-statement-based-on-whatever-the-previous-view-controller-was

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!