code_junkie

Passing a parameter in setAction

Nov 13th, 2011
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.51 KB | None | 0 0
  1. [replyAllBtn addTarget:self.target action:@selector(ReplyAll:) forControlEvents:UIControlEventTouchUpInside];
  2.  
  3. - (void)replyAll:(NSInteger)tid {
  4. // some code
  5. }
  6.  
  7. [replyAllBtn addTarget:self.target action:@selector(ReplyAll:) forControlEvents:UIControlEventTouchUpInside];
  8. replyAllBtn.tag=15;
  9.  
  10. (void) ReplyAll:(id)sender{
  11. NSInteger *tid = ((UIControl*)sender).tag;
  12. //...
  13.  
  14. - (void) ReplyAll:(id)sender;
  15.  
  16. NSInteger tid = sender.tag;
  17.  
  18. @interface Controller : NSObject {
  19. UIButton *_replyToSender;
  20. UIButton *_replyToAll;
  21. }
  22. - (void)buttonClicked:(id)sender;
  23. @end
  24.  
  25. @implementation Controller
  26. - (void)buttonClicked:(id)sender {
  27. if (sender == _replyToSender) {
  28. // reply to sender...
  29. } else if (sender == _replyToAll) {
  30. // reply to all...
  31. }
  32. }
  33. @end
  34.  
  35. typedef enum {
  36. ReplyButtonTypeSender = 1,
  37. ReplyButtonTypeAll,
  38. } ReplyButtonType;
  39.  
  40. @interface Controller : NSObject {
  41. }
  42. - (void)buttonClicked:(id)sender;
  43. @end
  44.  
  45. @interface MyButton : UIButton {
  46. }
  47. - (ReplyButtonType)typeOfReply;
  48. @end
  49.  
  50. @implementation Controller
  51. - (void)buttonClicked:(id)sender {
  52. // You aren't actually assured that sender is a MyButton, so the safest thing
  53. // to do here is to check that it is one.
  54. if ([sender isKindOfClass:[MyButton class]]) {
  55. switch ([sender typeOfReply]) {
  56. case ReplyButtonTypeSender:
  57. // reply to sender...
  58. break;
  59. case ReplyButtonTypeAll:
  60. // reply to all...
  61. break;
  62. }
  63. }
  64. }
  65. @end
Add Comment
Please, Sign In to add comment