iOS - 应用程序初始 ViewController 实例

发布于 2024-12-25 02:47:43 字数 230 浏览 2 评论 0原文

有没有办法在不创建新实例的情况下访问它?因为我想使用以下命令执行转场:

[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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

手心的温暖 2025-01-01 02:47:43

您是否使用 [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

魄砕の薆 2025-01-01 02:47:43

您可以将该对象创建的第一个实例保存到静态变量中,并定义一个静态方法来访问它。

static MyViewController *sharedInstance = nil;

@implementation MyViewController

- (id)initWithNibName:(NSString *)nibName bundle:(NSBundle *)nibBundle
{
  ... init code here ...

  if (!sharedInstance) {
    sharedInstance = self;
  }

  return self;
}

- (id)initWithCoder:(NSCoder *)decoder
{
  ... init code here ...

  if (!sharedInstance) {
    sharedInstance = self;
  }

  return self;
}

+ (MyViewController *)sharedInstance
{
  if (!sharedInstance)
    [[[self alloc] init] autorelease]; // will be retained inside the init method

  return sharedInstance;
}

@end

然后,在应用程序的其他任何地方,您都可以使用以下方式访问该变量:

[MyViewController sharedInsatnce];

这不是一种非常常用的模式,并且有一些缺点(例如:它永远不会被释放。因此请确保它不会使用太多内存),但是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.

static MyViewController *sharedInstance = nil;

@implementation MyViewController

- (id)initWithNibName:(NSString *)nibName bundle:(NSBundle *)nibBundle
{
  ... init code here ...

  if (!sharedInstance) {
    sharedInstance = self;
  }

  return self;
}

- (id)initWithCoder:(NSCoder *)decoder
{
  ... init code here ...

  if (!sharedInstance) {
    sharedInstance = self;
  }

  return self;
}

+ (MyViewController *)sharedInstance
{
  if (!sharedInstance)
    [[[self alloc] init] autorelease]; // will be retained inside the init method

  return sharedInstance;
}

@end

Then, anywhere else in your app you can access that variable using:

[MyViewController sharedInsatnce];

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文