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:
Result:
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
How to Ignore Certain Uitouch Points in Multitouch Sequence
Creating a Custom Progress Bar with Images
Hide a Phone Call Completely in iOS (Jailbreak Device)
How to Sort 1 Array in Swift/Xcode and Reorder Multiple Other Arrays by the Same Keys Changes
Uialertview Is Not Working in Swift
Open Uiimagepickercontroller in Landscape Mode
Uiview Drawrect: Draw the Inverted Pixels, Make a Hole, a Window, Negative Space
Sort Array by Distance Near User Location from Firebase
How to Upload Multiple Image on Firebase Using Swift
Searching Through Child Values Firebase/Swift
Getting the Local iPhone Number Through Sdk
What Is the Use of Singleton Class in Objective-C
Fbsopenapplicationerrordomain Code=3
Why Was Uitextfield's Text Property Changed to an Optional in Swift 2
Type 'Nsattributedstringkey' (Aka 'Nsstring') Has No Member 'Font'
Auto-Implement Swift Protocol Methods in Xcode
Uiactivityviewcontroller Completion Handler Returns Success When Tweet Has Failed
Google 400 Error: Invalid Request Custom Scheme Uris Are Not Allowed for 'Web' Client Type