It is possible to use an existing ViewController with PerformSegueWithIdentifier?

后端 未结 7 1525
一整个雨季
一整个雨季 2020-12-31 05:32

I use the method performSegueWithIdentifier:sender: to open a new ViewController from a storyboard-file programmatically. This works like a charm.<

7条回答
  •  盖世英雄少女心
    2020-12-31 05:50

    Use shouldPerforSegueWithIdentifier to either allow the segue to perform or to cancel the segue and manually add your ViewController. Retain a pointer in the prepareForSegue.

    ... header

    @property (strong, nonatomic) MyViewController *myVC;
    

    ... implementation

    -(BOOL) shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender{
        if([identifier isEqualToString:@"MySegueIdentifier"]){
            if(self.myVC){
                // push on the viewController
                [self.navigationController pushViewController:self.myVC animated:YES];
                 // cancel segue
                return NO; 
            }
        }
        // allow the segue to perform
        return YES;
    }
    
    
    -(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
    {
        if([segue.identifier isEqualToString:@"MySegueIdentifier"]){
            // this will only be called the first time the segue fires for this identifier
            // retian a pointer to the view controller
            self.myVC = segue.destinationViewController;
        }
    }
    

提交回复
热议问题