UIBarButtonItem 导致“无法识别的选择器发送到实例”

发布于 2024-12-08 11:36:38 字数 944 浏览 3 评论 0原文

我将 main.m 中的类的实例调用到 Controls.m 类,但它似乎给了我一个“无法识别的选择器发送到实例”错误。知道我在这里做错了什么吗?每次我点击按钮时它都会崩溃,但是这段代码中的 Controls.m 不是设置为 self 吗?找到测试选择器操作应该不会有问题。

Main.m

- (void)loadView {
    Controls *ct = [[Controls alloc] init];
    [ct addControls];
    [ct release];
}

Controls.m

- (void)addControls {
    UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, mv.frame.size.height-60, mv.frame.size.width, 40)];
    UIBarButtonItem *barBtnDataOverlay = [[UIBarButtonItem alloc] initWithTitle:@"Test Button" style:UIBarButtonSystemItemAction target:self action:@selector(test)];
    NSArray *toolbarButtons = [[NSArray alloc] initWithObjects:barBtnDataOverlay, nil];
    toolbar.items = toolbarButtons;
    [mv addSubview:toolbar];
    [barBtnDataOverlay release];
    [toolbar release];
 }

- (void)test {
    NSLog(@"TEST button hit");
}

I call an instance to a class in my main.m to my Controls.m class but it seems to be giving me a "Unrecognized selector sent to instance" error. Any idea what I am doing wrong here? Every time I hit the button it just crashes, but isn't Controls.m set to self in this code? It shouldn't have trouble finding the test selector action.

Main.m

- (void)loadView {
    Controls *ct = [[Controls alloc] init];
    [ct addControls];
    [ct release];
}

Controls.m

- (void)addControls {
    UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, mv.frame.size.height-60, mv.frame.size.width, 40)];
    UIBarButtonItem *barBtnDataOverlay = [[UIBarButtonItem alloc] initWithTitle:@"Test Button" style:UIBarButtonSystemItemAction target:self action:@selector(test)];
    NSArray *toolbarButtons = [[NSArray alloc] initWithObjects:barBtnDataOverlay, nil];
    toolbar.items = toolbarButtons;
    [mv addSubview:toolbar];
    [barBtnDataOverlay release];
    [toolbar release];
 }

- (void)test {
    NSLog(@"TEST button hit");
}

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

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

发布评论

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

评论(1

猫瑾少女 2024-12-15 11:36:38

由于没有保留,ct 将由 [ct release] 释放。尝试向您的类添加一个 ct retin 属性以保留它。

在类定义中:

@property(nonatomic, retain) Controls *ct;

在您的实现中:

@synthesize ct;

...

将您的 loadView 更改为以下内容:

- (void)loadView {
    self.ct = [[Controls alloc] init];
    [self.ct addControls];
    [self.ct release];
}

或者甚至更简洁:

- (void)loadView {
    self.ct = [[[Controls alloc] init] autorelease];
    [self.ct addControls];
}

您还应该在 viewDidUnload 中的某个地方释放 ct

- (void)viewDidUnLoad {
    self.ct = nil;
}

顺便说一句,这是在UIViewController 类?那么 loadView 方法可能应该分配 view 实例变量。如果您查看 UIViewController 的文档,您将看到以下内容:

如果您重写此方法以手动创建视图,则应该这样做并将层次结构的根视图分配给视图属性。

ct will be dealloced by [ct release] as no retain is left. Try to add a ct retin property to your class to keep it around.

In class definition:

@property(nonatomic, retain) Controls *ct;

In your implementation:

@synthesize ct;

...

Change your loadView to something like:

- (void)loadView {
    self.ct = [[Controls alloc] init];
    [self.ct addControls];
    [self.ct release];
}

Or even neater:

- (void)loadView {
    self.ct = [[[Controls alloc] init] autorelease];
    [self.ct addControls];
}

You should also release ct somewhere like in viewDidUnload

- (void)viewDidUnLoad {
    self.ct = nil;
}

BTW, is this in a UIViewController class? then the loadView method should probably assign the view instance variable. If you look in the documentation for UIViewController you will see this:

If you override this method in order to create your views manually, you should do so and assign the root view of your hierarchy to the view property.

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