问题描述
我使用方法 performSegueWithIdentifier:sender:
以编程方式从storyboard文件中打开一个新的 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 :(
任何帮助都将受到赞赏。
Any help would be appreciated.
编辑:
我很欣赏很多回复。同时,我对项目不熟悉,无法检查你的答案。
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.
... header
... header
@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一起使用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!