title: Passing arguments in a selector in IPhone
body: <p>I have a barButtonItem that calls an action which swaps two views in an animation block. I disable the <code>barButtonItem</code> at the beginning of the action method that it calls, and the I want to enable it when its finished, so that the user can't flip the view again before it has finished flipping. </p>
  
  <p>It does this in a UIView animation block, so If I enable it at the ned of the method, it will be instant and that destroys the purpose because it gets enabled before the animation finishes. </p>
  
  <p>In the method, I cast sender to a UIBarButtonItem, and I disable that. So I found <code>[UIView setAnimationDidStopSelector:@selector()]</code>, but I need to pass my barButtonItem in the selector so the enableControl Method will enable that (this would be a lot easier if I could make an anonymous function..). </p>
  
  <p>So how can I pass the barButton as an argument? I'd rather not have to make an ivar just for this.. Here's the code:</p>
  
  <pre><code>- (void)barBtnItemSwitchViews_Clicked:(id)sender {
      UIBarButtonItem *barButton = (UIBarButtonItem *)sender;
      barButton.enabled = NO;
      [UIView beginAnimations:nil context:NULL];
      [UIView setAnimationDuration:0.8];
      [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
      if(!self.yellowVC.view.superview) {
          [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:innerView cache:YES];
          [yellowVC viewWillAppear:YES];
          [blueVC viewWillDisappear:YES];
          [blueVC.view removeFromSuperview];
          self.blueVC = nil;
          [innerView addSubview:yellowVC.view];
      } else if(!self.blueVC.view.superview) {
          [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:innerView cache:YES];
          [blueVC viewWillAppear:YES];
          [yellowVC viewWillDisappear:YES];
          [yellowVC.view removeFromSuperview];
          self.yellowVC = nil;
          [innerView addSubview:blueVC.view];
      }
      [UIView commitAnimations];
      [UIView setAnimationDidStopSelector:@selector(enableControl:)]; // How do I pass barButton??
  }
  
  - (void)enableControl:(UIControl *)control {
      control.enabled = YES;
  }
  </code></pre>
  
  <hr>
  
  <p>Here's what I changed it to, but the method isn't being called.. What am I doing wrong?</p>
  
  <pre><code>- (void)barBtnItemSwitchViews_Clicked:(id)sender {
      UIBarButtonItem *barButton = (UIBarButtonItem *)sender;
      barButton.enabled = NO;
      [UIView beginAnimations:nil context:barButton];
      [UIView setAnimationDuration:0.8];
      [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
      if(!self.yellowVC.view.superview) {
          [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:innerView cache:YES];
          [yellowVC viewWillAppear:YES];
          [blueVC viewWillDisappear:YES];
          [blueVC.view removeFromSuperview];
          self.blueVC = nil;
          [innerView addSubview:yellowVC.view];
      } else if(!self.blueVC.view.superview) {
          [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:innerView cache:YES];
          [blueVC viewWillAppear:YES];
          [yellowVC viewWillDisappear:YES];
          [yellowVC.view removeFromSuperview];
          self.yellowVC = nil;
          [innerView addSubview:blueVC.view];
      }
      [UIView setAnimationDidStopSelector:@selector(flipViewAnimationDidStop:finished:context:)];
      [UIView commitAnimations];
  }
  
  - (void)flipViewAnimationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
      UIBarButtonItem *barButton = (UIBarButtonItem *)context;
      barButton.enabled = NO;
      NSLog(@"Disabled");
  }
  </code></pre>
  
  <hr>
  
  <p>EDIT: I figured it out. I had to make the button the context, and then do <code>setAnimationDelegate:self</code>, and then <code>setAnimationDidEndSelector:@selector(myMethod:finished:context:)</code>, and then in the method cast context to <code>uibarbuttonitem</code> and re-enable it. </p>