简体   繁体   English

UIButton target:action:Selector Argument

[英]UIButton target: action: Selector Argument

I have this method: 我有这个方法:

- (void)retweet:(NSString *)idString {
    STTwitterAPI *twitter;
    [twitter postStatusRetweetWithID:idString successBlock:^(NSDictionary *status) {
        UIAlertView *retweetSuccess = [[UIAlertView alloc]initWithTitle:@"Retweeted!" message:@"You have retweeted this tweet." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [retweetSuccess show];

    } errorBlock:^(NSError *error){

    }];
}

I also have a uibutton in my tableview cell so I was using [cell.retweetButton addTarget:self action:@selector(retweet:)forControlEvents:UIControlEventTouchUpInside]; 我的tableview单元格中也有一个uibutton所以我正在使用[cell.retweetButton addTarget:self action:@selector(retweet:)forControlEvents:UIControlEventTouchUpInside];

How can I use my idString as the argument for the method parameter with the selector? 如何使用我的idString作为方法参数与选择器的参数?

If the id string is originating from button the change your action method parameter to paramButton and acces it. 如果id字符串来自按钮,则将操作方法​​参数更改为paramButton并访问它。 if it is from elsewhere then you need to simply get it from wherever it is comming from. 如果它来自其他地方那么你需要简单地从它来自的地方获取它。

In any case, your action method has to have the :(UIButon *)paramButton parameter. 无论如何,你的action方法必须有:(UIButon *)paramButton参数。 ( (id)sender woul be also acceptable as an alternative). ((id)发送者也可以接受作为替代)。

Use objective c associated objects 使用目标c关联对象

#import <objc/runtime.h>

static const void *idStringKey = &idStringKey;

@implementation UIButton (idString)

- (void)setIDString:(NSString *)idString
{
   objc_setAssociatedObject(self, idStringKey, idString, OBJC_ASSOCIATION_COPY_NONATOMIC);
}

- (NSString *)idString
{
   return objc_getAssociatedObject(self, idStringKey);
}

@end

Use 采用

cell.retweetButton.idString = @"Any string here"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM