可以将现有的 ViewController 与 PerformSegueWithIdentifier 一起使用吗? [英] It is possible to use an existing ViewController with PerformSegueWithIdentifier?

查看:18
本文介绍了可以将现有的 ViewController 与 PerformSegueWithIdentifier 一起使用吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用方法 performSegueWithIdentifier:sender: 从故事板文件中以编程方式打开一个新的 ViewController.这就像一个魅力.

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

但是每次调用此方法时,都会创建一个新的 ViewController.是否可以使用现有的 ViewController(如果存在)?我没有找到有关此问题的任何信息(apple-doc、Stack Overflow ......).

But on every time when this method is being called, a new ViewController would be created. Is it possible to use the existing ViewController, if it exista? I don't find anything about this issue (apple-doc, Stack Overflow, ...).

问题是:在创建的 ViewController 上,用户设置了一些表单元素,如果 ViewController 将再次被调用,则表单元素具有初始设置:(

The Problem is: On the created ViewController the user set some form-Elements and if the ViewController would be called again, the form-elements has the initial settings :(

任何帮助将不胜感激.

我很欣赏许多回应.同时,我不熟悉该项目,无法检查您的答案.

I appreciate the many responses. Meanwhile, I'm not familiar with the project and can not check your answers.

推荐答案

使用 shouldPerforSegueWithIdentifier 来允许 segue 执行或取消 segue 并手动添加您的 ViewController.在 prepareForSegue 中保留一个指针.

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.

...标题

@property (strong, nonatomic) MyViewController *myVC;

...实现

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

这篇关于可以将现有的 ViewController 与 PerformSegueWithIdentifier 一起使用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆