簡體   English   中英

NSInvocation帶有枚舉?

[英]NSInvocation with an enum?

------------編輯-------------

TL; DR:

看來我錯誤地使用了NSInvocation類(缺少指針知識)。

使用這些方法的方式如下所示(工作):

NSString *str = @"string";
UIControlState state = UIControlStateNormal;
[invocation setArgument: &str atIndex: 2];
[invocation setArgument: &state atIndex: 3];

---------------原始問題---------

可以用枚舉參數設置setArgument:atIndex:嗎?

請參見以下示例(無效):

UIButton *btn = ...;
SEL selector = @selector(setTitle:forState:);
NSInovcation *invocation = [NSInovcation invocationWithMethodSignature: [btn methodSignatureForSelector: selector]];
[invocation setSelector: selector];
[invocation setArgument: @"Test" atIndex: 2];

//Nothing happens
UIControlState state = UIControlStateNormal;
[invocation setArgument: &state atIndex: 3];

//Runtime error (the pointer is NULL)
UIControlState *state = UIControlStateNormal;
[invocation setArgument: state atIndex: 3];

//No errors but no effect
int state2 = 0;
[invocation setArgument: &state atIndex: 3];

//Compile error (casting)
[invocation setArgument: @(UIControlStateNormal) atIndex: 3];
//Runtime EXC_BAD_ACCESS
[invocation setArgument: (void *)@(UIControlStateNormal) atIndex: 3];

...
[invocation invokeWithTarget: btn];

奇怪的是(或不是),它與performSelector:...一起使用時效果很好:

[btn performSelector: selector withObject: @"Testing" withObject:@(UIControlStateNormal)];

我不確定我是否以正確的方式使用NSInovcation。 有人想詳細說明嗎?

嘗試這個:

NSString *test = @"Test";
[invocation setArgument: &test atIndex: 2];

UIControlState state = UIControlStateNormal;
[invocation setArgument: &state atIndex: 3];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM