UIView(子视图)不将IBActions发送到AppDelegate
我遇到这个问题是因为我最初在主 NIB 中制作了所有内容,但后来阅读后了解到最好使用子视图。
我在 AppDelegate 中获得了 IBActions,并且已成功连接并加载了我的子视图。但是,当我尝试将子视图中的按钮连接到 AppDelegate 中的 IBAction 时,IBAction 出现在“第一响应者”下方。它们似乎连接良好,但在运行应用程序时,它们不会触发 IBActions(我已经通过一些 NSLog 确认了这一点,这不是 IBActions 中代码中的错误)。我做错了什么?
谢谢!
I'm having this problem because I originially made everything in the main NIB, but then after reading learned that it is better to use subviews.
I've got my IBActions in the AppDelegate, and I've successfully connected and loaded my subviews. However, when I try to connect my buttons in the subviews to the IBActions in the AppDelegate, the IBActions appear under the "First Responder". They seem to connect fine, but when running the application they do not trigger the IBActions (I've confirmed this with some NSLogs, it's not an error in the code within the IBActions). What am I doing wrong?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
AppDelegate 只能用于非常具体的项目,例如实现 UIApplicationDelegate 协议(即诸如 applicationDidFinishLaunching 之类的方法)或在某些情况下存储全局变量。
您应该将 IBActions 和其他出口保留在各自的视图控制器文件中(即,如果您创建了 MyViewController.h 和 MyViewController.m,它们与 MyViewController.xib 链接,其中有一些按钮、图像等)。然后可以通过将所需的检查器控件(即 TouchUpInside)拖到文件所有者来连接它们。
为了更好地理解视图控制器,您应该阅读一些内容: http:// developer.apple.com/iphone/library/featuredarticles/ViewControllerPGforiPhoneOS/Introduction/Introduction.html
通常,最好为要呈现给用户的每个视图创建一个唯一的视图控制器。例如,如果我有一个主屏幕,然后是一个“关于”或一个设置屏幕,我会让每个屏幕都有自己的视图控制器。与使用一个视图和一大堆隐藏/显示的子视图相比,它可以更好地组织事物,并且还可以提高加载时间和总体性能。
更新有关访问应用程序委托的评论中的第二个问题:
首先,您需要导入 .h 文件(即
#import "AppDelegate.h"
)应用程序委托到您想要用来访问存储在应用程序委托文件中的任何变量、数组等的视图控制器 .m 文件中。确保将在应用程序委托的 .h 文件中创建的任何对象合成到应用程序委托的 .m 文件中,以便创建 getter 和 setter(以便您可以访问它们)。然后在视图控制器 .m 文件中,无论您使用哪种方法:
希望有帮助!
The AppDelegate should only be used for very specific items such as implementing the
UIApplicationDelegate
protocol (i.e. methods likeapplicationDidFinishLaunching
) or in some cases storing global variables.You should keep IBActions and other outlets in their respective view controller files (i.e. if you created MyViewController.h and MyViewController.m which are linked with MyViewController.xib where you have some buttons, images, etc.). They can then be hooked up via dragging the inspector control you want (i.e. TouchUpInside) to the File's Owner.
Something you should read to better understand view controllers: http://developer.apple.com/iphone/library/featuredarticles/ViewControllerPGforiPhoneOS/Introduction/Introduction.html
Typically it is best to create a unique view controller for each view you will present to the user. For instance, if I had a main screen and then an "about" or a settings screen, I would make each of those their own view controller. It helps organize things better than using one view with a whole bunch of subviews that you hide/show and will also improve loading times and general performance.
Update for your 2nd question in the comments about accessing the app delegate:
First, you need to import the .h file (i.e.
#import "AppDelegate.h"
) for the app delegate into whichever view controller .m file you wanna use to access whatever variables, arrays, etc you have stored in the app delegate files. Make sure you synthesize whichever objects you create in the app delegate's .h file in the app delegate's .m file so the getters and setters are created (so you can access them).Then in the view controller .m file, in whichever method you are using:
Hope that helps!