Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/104.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/43.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 有关UIButton如何通过@selector传递多个参数_Ios_Iphone_Ios6_Uibutton - Fatal编程技术网

Ios 有关UIButton如何通过@selector传递多个参数

Ios 有关UIButton如何通过@selector传递多个参数,ios,iphone,ios6,uibutton,Ios,Iphone,Ios6,Uibutton,我需要为单个ui按钮传递两个操作 第一个参数已成功传递,如下所示: [imageButton addTarget:self action:@selector(imageClicked:) forControlEvents:UIControlEventTouchUpInside]; imageButton.tag = 1; 但我还需要为同一按钮传递另一个参数: int secondAction =10; [imageButton addTarget:self action:@select

我需要为单个
ui按钮
传递两个操作

第一个参数已成功传递,如下所示:

[imageButton addTarget:self action:@selector(imageClicked:) forControlEvents:UIControlEventTouchUpInside];
imageButton.tag = 1;
但我还需要为同一按钮传递另一个参数:

int secondAction =10;
    [imageButton addTarget:self action:@selector(imageClicked:*secondAction) forControlEvents:UIControlEventTouchUpInside];

有人能帮我为一个按钮/选择器传递两个值吗?

按钮可以接收的一个参数是(id)sender。这意味着您可以创建一个从UIButton继承的新按钮,该按钮允许您存储其他预期参数。希望这两个片段能说明该做什么

imageButton.tag = 1;
[imageButton addTarget:self action:@selector(buttonTouchUpInside:) forControlEvents:UIControlEventTouchUpInside];


请参阅此链接以获取进一步的解释

您可以使用Objective C Runtime功能将数据与对象关联,如下所示:

步骤1:在类中导入此项:
#导入

步骤2:创建一个键名:
static char*kDataAssociationKey=“associated_data_key”

步骤3:将数据与对象(例如:按钮)关联为:

步骤4:获取方法中的关联数据,如下所示:

NSString *value  = (NSString *)objc_getAssociatedObject(imageButton, kDataAssociationKey);

希望它对您有所帮助。

每个事件都有一个发送方,可以通过选择器方法获取

    (void)notify:(NSNotification *)notification {

     id notificationSender = [notification object];
     //do stuff

    }
现在这个发送者实际上是一个实例,它的属性可以用来获取关于它的信息 现在您可以做的是创建一个类,并向其中添加一些要通过选择器传递的属性,然后使用NSNotificationCenter广泛播送事件

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notify:) name:@"Event" object:yourobjectwithattributes];
将其放入要接收事件并具有选择器的类中


这是您想要抛出事件的地方

简短回答:您没有。那里的
@选择器
告诉按钮点击时调用什么方法,而不是应该传递给该方法的参数

更长的回答:如果您知道在创建按钮时参数是什么,那么您可以这样总结:

// In loadView or viewDidLoad or wherever:
[imageButton addTarget:self action:@selector(imageButtonTapped:) forControlEvents:UIControlEventTouchUpInside];

... later ...

- (void)imageButtonTapped:(id)sender
{
    [self doStuffToMyImageWithArgument:10];
}

- (void)doStuffToMyImageWithArgument:(NSInteger)argument
{
    ... do what you gotta do ...
如果您不知道,那么您可能希望将参数保存到某个变量中

// In your @interface
@property (nonatomic, assign) NSInteger imageStuffArgument;

... later ...

// In loadView or viewDidLoad or wherever:
[imageButton addTarget:self action:@selector(imageButtonTapped:) forControlEvents:UIControlEventTouchUpInside];

... later ...

- (void)respondToWhateverChangesTheArgument
{
    self.imageStuffArgument = 10;
}

... later ...

- (void)imageButtonTapped:(id)sender
{
    [self doStuffToMyImageWithArgument:self.imageStuffArgument];
}

- (void) doStuffToMyImageWithArgument:(NSInteger)argument
{
    ... do what you gotta do ...

要通过选择器传递多个参数,请参阅这不是重复!在上述解决方案中有[self-performSelector:]解决方案,而不是[Object addTarget]解决方案!您无缘无故地将参数包装在NSNumber中-您可以使用OBJC_ASSOCIATION_分配并关联int。然后您将NSNumber指针取回并将其转换为int,这将为您提供内存中NSNumber的地址,不是原始参数。@Simon是的,我应该使用OBJC_ASSOCIATION_ASSIGN作为整数值。但我的重点不仅仅是整数参数,我的重点是让用户知道任何类型的数据如何以这种方式关联。@mureinik你投票反对我只是因为编辑错误,我是新手,不知道你贬低了我的声誉点。。。不酷。不是我,只是为你修改了格式。哦,我真诚的道歉,然后我很抱歉
    [[NSNotificationCenter defaultCenter] postNotificationName:@"Event" object:notificationSender];
// In loadView or viewDidLoad or wherever:
[imageButton addTarget:self action:@selector(imageButtonTapped:) forControlEvents:UIControlEventTouchUpInside];

... later ...

- (void)imageButtonTapped:(id)sender
{
    [self doStuffToMyImageWithArgument:10];
}

- (void)doStuffToMyImageWithArgument:(NSInteger)argument
{
    ... do what you gotta do ...
// In your @interface
@property (nonatomic, assign) NSInteger imageStuffArgument;

... later ...

// In loadView or viewDidLoad or wherever:
[imageButton addTarget:self action:@selector(imageButtonTapped:) forControlEvents:UIControlEventTouchUpInside];

... later ...

- (void)respondToWhateverChangesTheArgument
{
    self.imageStuffArgument = 10;
}

... later ...

- (void)imageButtonTapped:(id)sender
{
    [self doStuffToMyImageWithArgument:self.imageStuffArgument];
}

- (void) doStuffToMyImageWithArgument:(NSInteger)argument
{
    ... do what you gotta do ...