@selector key word in iPhone programming

前端 未结 3 1736
误落风尘
误落风尘 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:05

    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).

提交回复
热议问题