iPhone 应用程序的多个 Objective-C 对象?
我确信这听起来很奇怪,但我还是会问。我已经为 iPhone 制作了多个工作应用程序,但我必须将所有内容添加到原始视图控制器中。如果我不这样做,应用程序就会崩溃。这是我的示例:
在视图控制器中:
- (IBAction) someAction: (id) sender {
NSLog(@"lajkrbgl");
}
这工作得很好。但是,如果我执行“添加文件”并选择“Objective-C 类”,并放入相同的代码,则每当我按下按钮时应用程序就会崩溃。如何添加更多对象并让它们像 OS X 一样发挥作用?
编辑:以下是我制作新对象所采取的步骤。在制作 OS X 应用程序时,这些步骤对我很有用。
- 我右键单击 Xcode 中的“Classes”文件夹。
- 我选择了“添加>新文件”。
- 我选择了“Objective-C Class”和“NSObject”。
- 我做了一个动作,“- (IBAction) someAction: (id) sender;”
- 在 Interface Builder 中,我将操作连接到 UIButton,并使用“Touch Up Inside”。
- 我单击“构建并运行”按钮。
之后,我单击该按钮,应用程序退出(几乎就像我单击“主页”按钮一样)。我没有收到任何错误。然后我认为这是因为我没有输入该操作的代码,所以我接下来就这样做了。
- (IBAction) someAction: (id) sender; {
NSLog(@"lajkrbl");
}
即使在此之后,它仍然以完全相同的方式崩溃,并且没有错误消息。有人能看到我做错了什么吗?
EDIT2:我刚刚运行 Xcode 4,当我单击按钮时,它告诉我这段代码给了我问题(我在有绿线的行之前添加了“**”)。
int main(int argc, char *argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
**int retVal = UIApplicationMain(argc, argv, nil, nil);
[pool release];
return retVal;
到底
是怎么出错的?这是我做项目时由 Xcode 创建的!
I'm sure this is going to sound very weird, but I'll ask anyway. I have made multiple working applications for the iPhone, but I have to add everything to the original view controller. If I don't do so, the app crashes. Here is my example:
In the view controller:
- (IBAction) someAction: (id) sender {
NSLog(@"lajkrbgl");
}
This works just fine. But, if I do the "Add File" and I choose "Objective-C Class", and put the same code in, the app just crashes whenever I press the button. How can I add more objects and have them function like they do for OS X?
EDIT: Below are the steps I took to make the new object. These steps worked for me when making an OS X application.
- I right clicked on the "Classes" folder in Xcode.
- I selected "Add > New File".
- I selected "Objective-C Class" and "NSObject".
- I made an action, "- (IBAction) someAction: (id) sender;"
- In Interface Builder, I connected the action to a UIButton, and used "Touch Up Inside".
- I clicked the "Build and Run" button.
After that, I clicked the button, and the application quit (almost as if i clicked the Home button). I did not get any errors. I then decided that it was because I didn't put the code in for the action, so I did that next.
- (IBAction) someAction: (id) sender; {
NSLog(@"lajkrbl");
}
Even after this, it crashed in the exact same way, and without an error message. Can anybody see what I did wrong?
EDIT2: I just got Xcode 4 running, and when I clicked the button, it told me that this code is giving me the problem (I put "**" before the line that had the green line).
int main(int argc, char *argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
**int retVal = UIApplicationMain(argc, argv, nil, nil);
[pool release];
return retVal;
}
How in the world did that go wrong? That is created by Xcode when I make projects!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您将 IBAction 连接到某个离散对象中的方法,则调用该操作时该对象需要存在。确保包含 IBAction 代码的对象已实例化并由包含该按钮的任何实体(可能是视图控制器)保留。
这可能会导致该实体保留 MyObject 类型的实例变量,其中 MyObject 是包含 IBAction 代码的 NSObject 子类。
代码示例:
MyObject.m 包含:
MyViewController.xib 包含一个 UIButton,其 TouchUpInside 链接到 someAction。
MyViewController.h 包含:
MyViewController.m 包含(在
initWithNibName:bundle:
中):您可以选择将 myObject 设为一个属性,以使这一切变得更漂亮,但这是您的决定。
If you're connecting an IBAction to a method in some discrete object, that object needs to exist when the action is called. Make sure that your object that contains the IBAction code has been instantiated and is retained by whatever entity (likely a viewcontroller) contains the button.
This will probably result in that entity retaining an instance variable of type MyObject, where MyObject is your NSObject subclass containing the IBAction code.
Code example:
MyObject.m contains:
MyViewController.xib contains a UIButton whose TouchUpInside is linked to someAction.
MyViewController.h contains:
MyViewController.m contains (in
initWithNibName:bundle:
):You could optionally make myObject a property to make this all way prettier, but that's your call.
iOS 中确实不一样。此注释位于 查看控制器编程指南:
并且,更明确的是, 资源编程指南中的表 1-1:
两者都表明您应该为实现按钮操作方法的对象添加一个
IBOutlet
到您的视图控制器,并让视图控制器保留该对象(属性使这变得容易):在 ButtonActioner.m 中:
然后,您可以像往常一样在 Interface Builder 中连接视图控制器的插座和按钮的操作。
It is indeed different in iOS. This note in the View Controller Programming Guide:
and, more definitively, Table 1-1 in the Resource Programming Guide:
both indicate that you should add an
IBOutlet
for the object which implements the button's action method to your view controller, and have the view controller retain that object (a property makes this easy):In ButtonActioner.m:
You then hook the view controller's outlet and the button's action up as usual in Interface Builder.