iPhone 应用程序的多个 Objective-C 对象?

发布于 2024-11-02 14:23:15 字数 1208 浏览 1 评论 0原文

我确信这听起来很奇怪,但我还是会问。我已经为 iPhone 制作了多个工作应用程序,但我必须将所有内容添加到原始视图控制器中。如果我不这样做,应用程序就会崩溃。这是我的示例:

在视图控制器中:

- (IBAction) someAction: (id) sender {
    NSLog(@"lajkrbgl");

}

这工作得很好。但是,如果我执行“添加文件”并选择“Objective-C 类”,并放入相同的代码,则每当我按下按钮时应用程序就会崩溃。如何添加更多对象并让它们像 OS X 一样发挥作用?

编辑:以下是我制作新对象所采取的步骤。在制作 OS X 应用程序时,这些步骤对我很有用。

  1. 我右键单击 Xcode 中的“Classes”文件夹。
  2. 我选择了“添加>新文件”。
  3. 我选择了“Objective-C Class”和“NSObject”。
  4. 我做了一个动作,“- (IBAction) someAction: (id) sender;”
  5. 在 Interface Builder 中,我将操作连接到 UIButton,并使用“Touch Up Inside”。
  6. 我单击“构建并运行”按钮。

之后,我单击该按钮,应用程序退出(几乎就像我单击“主页”按钮一样)。我没有收到任何错误。然后我认为这是因为我没有输入该操作的代码,所以我接下来就这样做了。

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

  1. I right clicked on the "Classes" folder in Xcode.
  2. I selected "Add > New File".
  3. I selected "Objective-C Class" and "NSObject".
  4. I made an action, "- (IBAction) someAction: (id) sender;"
  5. In Interface Builder, I connected the action to a UIButton, and used "Touch Up Inside".
  6. 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 技术交流群。

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

发布评论

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

评论(2

慢慢从新开始 2024-11-09 14:23:16

如果您将 IBAction 连接到某个离散对象中的方法,则调用该操作时该对象需要存在。确保包含 IBAction 代码的对象已实例化并由包含该按钮的任何实体(可能是视图控制器)保留。

这可能会导致该实体保留 MyObject 类型的实例变量,其中 MyObject 是包含 IBAction 代码的 NSObject 子类。

代码示例:

MyObject.m 包含:

- (IBAction) someAction: (id) sender {
    NSLog(@"lajkrbgl");
}

MyViewController.xib 包含一个 UIButton,其 TouchUpInside 链接到 someAction。

MyViewController.h 包含:

@interface MyViewController : UIViewController {
    MyObject *myObject_;
}

MyViewController.m 包含(在 initWithNibName:bundle: 中):

myObject_ = [[MyObject alloc] init];

您可以选择将 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:

- (IBAction) someAction: (id) sender {
    NSLog(@"lajkrbgl");
}

MyViewController.xib contains a UIButton whose TouchUpInside is linked to someAction.

MyViewController.h contains:

@interface MyViewController : UIViewController {
    MyObject *myObject_;
}

MyViewController.m contains (in initWithNibName:bundle:):

myObject_ = [[MyObject alloc] init];

You could optionally make myObject a property to make this all way prettier, but that's your call.

深府石板幽径 2024-11-09 14:23:15

iOS 中确实不一样。此注释位于 查看控制器编程指南

每当您将对象添加到 nib 文件的顶层时,您应该始终将这些对象连接到 nib 文件中其他位置的插座。 [...]因为顶级对象被保留然后自动释放,如果您没有保留该对象,它可能会在您有机会使用它之前被释放。

并且,更明确的是, 资源编程指南中的表 1-1

nib 文件中的对象在创建时保留计数为 1,然后自动释放。 [...] 如果您为 nib 文件对象定义了outlet,则应始终定义一个setter 方法(或声明的属性)来访问该outlet。出口的 setter 方法应保留其值,包含顶级对象的出口的 setter 方法必须保留其值以防止它们被释放。

两者都表明您应该为实现按钮操作方法的对象添加一个 IBOutlet 到您的视图控制器,并让视图控制器保留该对象(属性使这变得容易):

@interface ButtonCrashViewController : UIViewController {
    IBOutlet ButtonActioner * myButtonActioner;
}

@property (retain, nonatomic) IBOutlet ButtonActioner *myButtonActioner;

@end

在 ButtonActioner.m 中:

- (IBAction)someAction:(id)sender {
    NSLog(@"This didn't crash!");
}

然后,您可以像往常一样在 Interface Builder 中连接视图控制器的插座和按钮的操作。

It is indeed different in iOS. This note in the View Controller Programming Guide:

Whenever you add objects to the top-level of your nib file, you should always connect those objects to outlets somewhere else in the nib file. [...] because top-level objects are retained and then autoreleased, if you did not retain the object, it might possibly be released before you had a chance to use it.

and, more definitively, Table 1-1 in the Resource Programming Guide:

Objects in the nib file are created with a retain count of 1 and then autoreleased. [...] If you define outlets for nib-file objects, you should always define a setter method (or declared property) for accessing that outlet. Setter methods for outlets should retain their values, and setter methods for outlets containing top-level objects must retain their values to prevent them from being deallocated.

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

@interface ButtonCrashViewController : UIViewController {
    IBOutlet ButtonActioner * myButtonActioner;
}

@property (retain, nonatomic) IBOutlet ButtonActioner *myButtonActioner;

@end

In ButtonActioner.m:

- (IBAction)someAction:(id)sender {
    NSLog(@"This didn't crash!");
}

You then hook the view controller's outlet and the button's action up as usual in Interface Builder.

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