Call IBAction method from code
I have a button findTarget_Btn on which I want to call a method more开发者_StackOverflow社区Details. In viewDidLoad, I am writing
[findTarget_Btn addTarget:self action:@selector(moreDetails) forControlEvents:UIControlEventTouchUpInside];
But for some reason moreDetails is not getting called when I click the button? Am I doing something wrong?
Also is it fine if I call 2 methods on the button; 1 via IB and other via code similar to above ?
Check how your moreDetails
is defined. Most likely, it takes id sender
parameter: in this case you should use @selector(moreDetails:)
(colon signature reflects presence of parameter in)
Regarding your second question: yes, you can definitely call other actions from your code. IBAction
is just a synonym for void
, it's no different from any other method.
edit
I might have misunderstood your second question. If you want to assign one action in IB and another one in the code, that might not work. But you can always create a dedicated handler and call two methods sequentially from it.
- (IBAction) findTargetClick:(id) sender {
[self handler1:sender];
[self moreDetails:sender];
}
Have you made sure that you've assigned something to findTarget_Btn
? If you forgot to initialise it or if you are using a nib and forgot to hook it up as an outlet, then it will be nil, so any messages you send to it will be ignored.
精彩评论