@selector key word in iPhone programming

前端 未结 3 1744
误落风尘
误落风尘 2021-01-24 09:40

-(void)displayNameBy:(NSString*)name{

    mylable.text = name;

}

i want call this method using @selector keywords.

eg:



        
3条回答
  •  温柔的废话
    2021-01-24 10:07

    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.

提交回复
热议问题