14
我在故事板場景中有一個UIButton
。該按鈕具有配置的User-Defined-RunTime-Attribute'type'(String)。當按下按鈕呼叫如何從'發件人'對象訪問用戶定義的運行時屬性?
-(IBAction)pressedButton:(id)sender
我將能夠訪問由「發送方」用戶自定義運行時屬性?
我在故事板場景中有一個UIButton
。該按鈕具有配置的User-Defined-RunTime-Attribute'type'(String)。當按下按鈕呼叫如何從'發件人'對象訪問用戶定義的運行時屬性?
-(IBAction)pressedButton:(id)sender
我將能夠訪問由「發送方」用戶自定義運行時屬性?
是:除非你繼承的UIButton並將其添加爲一個強大的屬性你不能使用用戶定義的運行時間屬性,,例如
-(IBAction)pressedButton:(id)sender
{
id value = [sender valueForKey:key];
}
注
@interface UINamedButton : UIButton
@property (strong) NSString *keyName;
@end
如果您設置了「用戶定義的運行時間」屬性,而您還沒有這樣做,那麼Xcode將不幸地崩潰。
然後,您可以得到像
-(IBAction)clicked:(UIControl *)sender
{
NSString *test = @"???";
if ([sender respondsToSelector:@selector(keyName)])
test = [sender valueForKey:@"keyName"];
NSLog(@"the value of keyName is ... %@", test);
// if you FORGOT TO SET the keyName value in storyboard, that will be NULL
// if it's NOT a UINamedButton button, you'll get the "???"
// and for example...
[self performSegueWithIdentifier:@"idUber" sender:sender];
// ...the prepareForSegue could then use that value in the button.
// note that a useful alternative to
// if ([sender respondsToSelector:@selector(stringTag)])
// is...
// if ([sender respondsToSelector:NSSelectorFromString(@"stringTag")])
}
該值