gpt4 book ai didi

iphone - 另一个类中方法的参数化 UIButton Action 选择器?

转载 作者:太空狗 更新时间:2023-10-30 03:59:09 25 4
gpt4 key购买 nike

这主要是一个句法问题。如何设置 UIButton 操作选择器以调用不同类的方法?我已经完成了类的#import,我需要用按钮调用它的方法,我对按钮代码应该是什么样子有以下部分理解:

    UIButton *btnSplash = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btnSplash.frame = CGRectMake(250, 270, 180, 30);
[btnSplash setTitle:@"Menu" forState:UIControlStateNormal];
[btnSplash addTarget:self action:@selector([CLASS METHOD:PARAMETER]) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:btnSplash];

但是,我收到以下错误:

expected ':' before '[' token

method name missing in @selector

我在引用库中看到的示例代码调用了本地方法,因此我试图进行概括,但我的尝试到目前为止没有结果。

谢谢

最佳答案

选择器是方法名称的表示,无论哪个类或类别实现它。

假设您有一个名为 AnotherClass 的类,它实现了方法 - (void)doSomething:(id)sender。对应的选择器是doSomething:,在代码中表示为@selector(doSomething:)。如果您希望按钮操作调用该方法,您需要有一个 AnotherClass 的实例——这个实例是操作目标,而不是 self 。因此你的代码应该有:

#import "AnotherClass.h"

AnotherClass *instanceOfAnotherClass;
// assign an instance to instanceOfAnotherClass

UIButton *btnSplash = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btnSplash.frame = CGRectMake(250, 270, 180, 30);
[btnSplash setTitle:@"Menu" forState:UIControlStateNormal];

[btnSplash addTarget:instanceOfAnotherClass
action:@selector(doSomething:)
forControlEvents:UIControlEventTouchUpInside];

[self addSubview:btnSplash];

关于iphone - 另一个类中方法的参数化 UIButton Action 选择器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5610523/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com