IOS:预先创建的对象如何找到自己?

发布于 2025-01-03 13:33:10 字数 461 浏览 2 评论 0原文

我是IOS的初学者,所以这个问题可能很愚蠢,但我不能只见树木:

当我在XCode 4.2中开始一个新项目时,预先创建了两个实现文件,即AppDelegate.m和ViewController.m。

据我了解,它们各自声明一个类,当我运行应用程序时,操作系统会为我预先实例化两个各自的对象 AppDelegate 和 ViewController 。

现在我正在 AppDelegate 中执行某些操作,并且想要更新作为 ViewController 的属性的文本标签。

但我没有找到引用 ViewController 对象的方法。

通常,在 ViewController 内部,我可以使用以下命令更新文本标签 self.MyTextLabel = @"myText"。但AppDelegate在“外部”并且没有实例化ViewController,因此没有可用的指针。

有人可以帮忙吗?

I'm a complete beginner to IOS, so the question may be dumb, but I cannot to see the wood for the trees:

When I started a new project in XCode 4.2, two implementation files were pre-created, i.e. AppDelegate.m and ViewController.m.

As far as I understand it, they declare one class each, and when I run the application, the two respective objects AppDelegate and ViewController are pre-instantiated by the operating system for me.

Now I'm doing certain stuff in the AppDelegate and want to update a text label which is a property of the ViewController.

But I do not find a way to refer the ViewController object.

Normally, inside the ViewController, I can update the text label with
self.MyTextLabel = @"myText". But AppDelegate is "outside" and did not instantiate ViewController, so there is no pointer available.

Can anybody help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

苍白女子 2025-01-10 13:33:10

iOS 应用程序的结构可能相当复杂,但我向您保证,这里并没有什么神奇之处。

您的 main.m 通常包含这样一行:

UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));

应用程序启动后 -(BOOL) application:(UIApplication*) application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions在您的 AppDelegate.m 中将被调用。您通常会在这里找到视图控制器的创建。在您的应用程序委托中,您可以通过保留在其中的属性来访问视图控制器。

self.viewController.myTextLabel = @"myText";

但是,如果 myTextLabel 是 XIB 文件中设置的插座,则该属性将仅引用实际的 UILabel< /code> 从 XIB 加载控制器视图后。当您自己访问 .view 属性时,或者将视图控制器添加到另一个视图控制器(例如 UINavigationController)后,通常会发生这种情况。 (但这取决于您为项目选择的模板)。

The structure of an iOS app can be quite overwhelming, but let me assure you there's no magic going on here.

Your main.m usually contains a line like this:

UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));

As soon as the app has started -(BOOL) application:(UIApplication*) application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions in your AppDelegate.m will be called. Here you'll usually find the creation of your view controller. In your app delegate you can access the view controller through it's property it is retained in.

self.viewController.myTextLabel = @"myText";

However if myTextLabel is an outlet set in a XIB file, the property will only refer to an actual UILabel after the view for your controller has been loaded from the XIB. This usually happens as soon as you access the .view property yourself, or after you add your view controller to another view controller like a UINavigationController. (This however depends on the template you chose for your project).

日记撕了你也走了 2025-01-10 13:33:10

在 xcode 4.2 中,您可以在方法中找到 ViewController 对象的创建:

-(BOOL) application:(UIApplication*) application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions

您应该看到类似这样的行

self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController" bundle:nil] autorelease];

In xcode 4.2 you can find the creation of the ViewController object in the method:

-(BOOL) application:(UIApplication*) application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions

you should see a line that looks like

self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController" bundle:nil] autorelease];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文