IOS:预先创建的对象如何找到自己?
我是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 withself.MyTextLabel = @"myText"
. But AppDelegate is "outside" and did not instantiate ViewController, so there is no pointer available.
Can anybody help?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
iOS 应用程序的结构可能相当复杂,但我向您保证,这里并没有什么神奇之处。
您的
main.m
通常包含这样一行:应用程序启动后
-(BOOL) application:(UIApplication*) application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
在您的AppDelegate.m
中将被调用。您通常会在这里找到视图控制器的创建。在您的应用程序委托中,您可以通过保留在其中的属性来访问视图控制器。但是,如果
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:As soon as the app has started
-(BOOL) application:(UIApplication*) application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
in yourAppDelegate.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.However if
myTextLabel
is an outlet set in a XIB file, the property will only refer to an actualUILabel
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 aUINavigationController
. (This however depends on the template you chose for your project).在 xcode 4.2 中,您可以在方法中找到 ViewController 对象的创建:
您应该看到类似这样的行
In xcode 4.2 you can find the creation of the ViewController object in the method:
you should see a line that looks like