-(void)displayNameBy:(NSString*)name{
mylable.text = name;
}
i want call this method using @selector keywords.
eg:
If you create a one-argument selector for your action (like you're doing in displayNameBy:
), the argument is the sender of the action. In this case, your button instance.
According to the UIControl docs, there are three different types of supported selectors:
- (void)action
- (void)action:(id)sender
- (void)action:(id)sender forEvent:(UIEvent *)event
Where are you expecting name
to be defined? Is it based on the user's actions, the app state, or something else? Depending on what method is used, you could create a UIButton subclass that includes either the necessary logic or declares a delegate protocol, set the delegate to your view controller, and have the delegate implementation set the button's label. The latter is probably better from a MVC standpoint, unless the source of name
is blindingly simply (ie, an attribute that could be set on the button, not something the button would have to calculate based on other application state).
The method's selector is just displayNameBy:
. The name
it the end is the name of the parameter. However, I don't know where you're expecting this NSString *name
parameter to come from. The argument for an action method is the sender, which will be the button in this case. So it would be - (void)displayNameBy:(id)sender
.
If you're trying to pass the parameter through the selector, that isn't possible. A selector is literally just a name — it doesn't specify any particular behavior.
If you wanted to use PLBlocks, you could create a trampoline class that would be called like:
[myButton setTarget:[BlockProxy proxyWithBlock:^{ [self displayNameBy:name]; }] action:@selector(call:) forControlEvents:UIControlEventTouchUpInside];
That's the closest you'll get, I think. Because what you really want is for the button to call a closure, which is what PLBlocks gives you. Whether it's worth the trouble to get this kind of expressiveness is you're call.
Have an intermediate method. like:
{
...
[MyButton addTarget:self action:@selector(displayName) forControlEvents:UIControlEventTouchUpInside];
...
}
-(void) displayName
{
[self performSelector:@selector(displayNameBy:) withObject:@"name"];
}
-(void)displayNameBy:(NSString*)name{
mylable.text = name;
}