iOS - 应用程序初始 ViewController 实例
有没有办法在不创建新实例的情况下访问它?因为我想使用以下命令执行转场:
[self performSegueWithIdentifier:@"loginSegue" sender:sender];
但是,如果我尝试创建一个实例,编译器会说转场不存在。我必须创建一个新实例的原因是因为我从另一个类调用 ViewController 类的方法。有没有办法从首先创建的实例运行该方法?
Is there any way to access this without creating a new instance? As I want to perform a segue using:
[self performSegueWithIdentifier:@"loginSegue" sender:sender];
However if I try and create an instance, the compiler says that the segue doesn't exist. The reason I have to create a new instance is because I'm calling a method of the ViewController class from another class. Is there a way to run the method from the instance that is created in the first place?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否使用
[UIStoryboard instantiateViewControllerWithIdentifier:]
方法创建UIViewController
实例?如果您使用 alloc-init 实例化,它不会从 Storyboard 实例化实例,因此它不会连接到 segue。这是对 UIStoryboard 类的引用。
http://developer.apple.com/库/ios/#documentation/UIKit/Reference/UIStoryboard_Class/Reference/Reference.html
Are you creating
UIViewController
instance using[UIStoryboard instantiateViewControllerWithIdentifier:]
method? If you instantiate with alloc-init, it will not instantiate the instance from storyboard, so it will not connect to the segue.Here is reference to UIStoryboard class.
http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIStoryboard_Class/Reference/Reference.html
您可以将该对象创建的第一个实例保存到静态变量中,并定义一个静态方法来访问它。
然后,在应用程序的其他任何地方,您都可以使用以下方式访问该变量:
这不是一种非常常用的模式,并且有一些缺点(例如:它永远不会被释放。因此请确保它不会使用太多内存),但是UIKit/Foundation 中的几个类都使用它(NSFileManager、NSUserDefaults、NSBundle 等)。
You can save the first instance ever created of this object to a static variable, and define a static method to access that.
Then, anywhere else in your app you can access that variable using:
It's not a very commonly used pattern, and has a few drawbacks (eg: it will never be deallocated. so make sure it doesn't use too much memory), but several classes in UIKit/Foundation use it (NSFileManager, NSUserDefaults, NSBundle, etc).