How to Access User Defined Runtime Attribute from the 'Sender' Object

How to access User Defined Runtime Attribute from the 'sender' object?

Yes:

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

Note that you cannot use a User Defined Run Time attribute, unless you subclass UIButton and add it as a strong property, for example

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

If you set a User Defined Run Time attribute, and you have not done this, Xcode will badly crash unfortunately.

You can then get that value like

-(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")] )
}

List of user defined runtime attributes available for an object in swift

Any property (including your own defined) that is of type Bool, NSNumber, String, CGPoint, CGSize, CGRect, UIImage, UIColor or NSRange can be set as user defined runtime attributes.

borderWidth is button.layer propery. button.layer class is CALayer. You can check out all CALayer properties here: https://developer.apple.com/library/prerelease/ios/documentation/GraphicsImaging/Reference/CALayer_class/index.html#//apple_ref/swift/cl/CALayer

Rotate (angle) a UIView, using User Defined Runtime Attributes

You can use layer.transform.rotation.z key path for UIView object.

Value has to be set in radians:

Sample Image

Result:

Sample Image

All transformation key paths are presented in Apple documentation.

how can I change user defined runtime attribute when button state change

You can just set the properties on the view like any other property.

layer.shadowColor = UIColor.gray.cgColor
layer.cornerRadius = someotherValue

and so on.

How to get an attribute from an unspecified object?

You are on the right track.

The problem you have is that in btn_Click the sender is a generic object, so the compiler doesn't know what type it is, so you need to tell it by casting.

 public void btn_Click(object sender, EventArgs e)
{
Button senderButton = (Button)sender;
MessageBox.Show(senderButton.Name + " is clicked");
}

How do I assign a name to a control and retrieve it in an event method?

You might want to look at using the tag property. It's an integer rather than a name, but can be used to differentiate between two controls. It's inherited from UIView so any control that's sending an event should have it.

It's editable in Interface Builder under View attributes.

It's a property so it can be accessed programatically with the dot notation:

control.tag

Or you can convert to a string as follows:

[stringWithFormat:@"%d", control.tag]


Related Topics



Leave a reply



Submit