[英]UIButton target: action: Selector Argument
我有這個方法:
- (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){
}];
}
我的tableview單元格中也有一個uibutton所以我正在使用[cell.retweetButton addTarget:self action:@selector(retweet:)forControlEvents:UIControlEventTouchUpInside];
如何使用我的idString作為方法參數與選擇器的參數?
如果id字符串來自按鈕,則將操作方法參數更改為paramButton並訪問它。 如果它來自其他地方那么你需要簡單地從它來自的地方獲取它。
無論如何,你的action方法必須有:(UIButon *)paramButton參數。 ((id)發送者也可以接受作為替代)。
使用目標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
采用
cell.retweetButton.idString = @"Any string here"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.