14
私はUIButton
をストーリーボードのシーンに持っています。ボタンには、ユーザ定義のランタイム属性 'タイプ'(String)が設定されています。ボタンが「送信者」オブジェクトからユーザー定義のランタイム属性にアクセスする方法は?
-(IBAction)pressedButton:(id)sender
を呼び出す押されたとき、私は「送信者からユーザー定義のランタイム属性にアクセスすることができるだろうか?
私はUIButton
をストーリーボードのシーンに持っています。ボタンには、ユーザ定義のランタイム属性 'タイプ'(String)が設定されています。ボタンが「送信者」オブジェクトからユーザー定義のランタイム属性にアクセスする方法は?
-(IBAction)pressedButton:(id)sender
を呼び出す押されたとき、私は「送信者からユーザー定義のランタイム属性にアクセスすることができるだろうか?
はい:あなたはUIButtonのサブクラスを作成し、強力な財産として追加しない限り、あなたは一例
@interface UINamedButton : UIButton
@property (strong) NSString *keyName;
@end
のために、ユーザー定義のランタイム属性、
を使用することはできません -(IBAction)pressedButton:(id)sender
{
id value = [sender valueForKey:key];
}
注意
ユーザー定義の実行時属性を設定していて、これを実行していないと、残念なことに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")])
}
のような、その値を得ることができます