2012-08-16 60 views
2

有人能告诉我如何获得它曾经创建的UIView控制器?,我正在使用故事板,并且已经阅读了有关方法instantiateViewControllerWithIdentifier的苹果文档:但是这个文档说每次我调用这个方法时,它都会创建一个新的实例ViewController和我想要的是使用现有的。如何在故事板上获得活动的UIViewController?

我在故事板上寻找一个UIViewController单身人士。可能吗?

在此先感谢!

回答

2

如果您只拨打instantiateViewControllerWithIdentifier:一次,然后将结果保存为某个地方的重要属性以供重复使用,那么您只会创建一次。

0

我有这样一个解决方案:

  1. ControllerA:

' SingleController * B = [SingleController shareSingleController]; 
[self.navigationController pushViewController:con animated:YES];' 

//push ControllerB 
  • ControllerB:
  • +(instancetype)shareSingleController 
    { 
        static SingleController * single; 
    
        static dispatch_once_t onceToken; 
    
        dispatch_once(&onceToken, ^{  
         //a static instance from StoryBoard 
    
         single = (SingleController *)[[UIStoryboard storyboardWithName:@"SingleController" bundle:nil]instantiateInitialViewController]; 
        }); 
        return single;' 
    } 
    

    您可以测试它的单个实例。