2013-02-21 23 views

Trả lời

22

Có:

-(IBAction)pressedButton:(id)sender 
{ 
    id value = [sender valueForKey:key]; 
} 

Lưu ý rằng bạn không thể sử dụng một User Defined Run Time thuộc tính, trừ khi bạn phân lớp UIButton và thêm nó như là một tài sản mạnh mẽ, ví dụ

@interface UINamedButton : UIButton 
@property (strong) NSString *keyName; 
@end 

Nếu bạn đặt thuộc tính Thời gian chạy do người dùng xác định và bạn chưa thực hiện điều này, Xcode sẽ gặp sự cố không may.

Sau đó bạn có thể nhận được rằng giá trị như

-(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")]) 
    } 
Các vấn đề liên quan