2013-02-21 53 views

回答

22

是:除非你繼承的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")]) 
    } 
該值
相關問題