IBaction

Discussion in 'iOS Programming' started by Nnavick, Oct 13, 2010.

  1. Nnavick macrumors regular

    Joined:
    Oct 13, 2010
    #1
    hi all,
    I created a method that creates button

    PHP:
        CGRect buttonRect CGRectMake(006464);
        
    UIButton *HamburgerButton = [[UIButton buttonWithType:UIButtonTypeCustomretain];
        [
    HamburgerButton setFrame:buttonRect];    
        [
    HamburgerButton setTitle:@"" forState:UIControlStateNormal];;
        
    UIImage *buttonImage = [UIImage imageNamed:@"Hamburger.png"];
        [
    HamburgerButton setBackgroundImage:buttonImage forState:UIControlStateNormal];
    forControlEvents:UIControlEventTouchUpInside];
        
    HamburgerButton.center=CGPointMake(x1=25+arc4random()% 260y1=79+arc4random()% 350);
        [
    self  addSubview:HamburgerButton];
        [
    HamburgerButton release];
    How do I connect the "vitual unInterFace Builder" Button(think NSbutton but not sure)to an IBaction?
    I mean,I can't write in the .h -(IBaction) bla bla ; and then connect it visualy to the button,it's need to be via code but I don't know how to do it.

    Thanks
     
  2. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
  3. Sykte macrumors regular

    Joined:
    Aug 26, 2010
    #3

    Take a look at the documentation for addTarget:Action:
     
  4. Nnavick thread starter macrumors regular

    Joined:
    Oct 13, 2010
    #4

    I found what you sent me,but I don't understand where to put the code and where to write and call the function

    PHP:
    - (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents
     
  5. Sykte macrumors regular

    Joined:
    Aug 26, 2010
    #5



    Code:
    addTarget - Who will respond to the action.
    
    action:(SEL)action - which method do you want executed when the ControlEvent happens
    
    forControlEvents: - On which particular event do you want the action executed.
    
    Example:
    Code:
    [myButton addTarget:self action:@selector(myMethodCallHere:) forControlEvents:UIControlEventTouchUpInside];
    
     
  6. Nnavick thread starter macrumors regular

    Joined:
    Oct 13, 2010
    #6
    okkkk,and one more question(hope so),where do I write the code?
    between the code I pasted on the main post or ?and how I call the method down?

    Is it fine?

    PHP:
    - (void)addTarget:(id)HamburgerButton action:(SEL)ScoreUp forControlEvents:(UIControlEvents)UIControlEventTouchUpInside {
        
    ScoresCounter.text=[NSString stringWithFormat:@"%i",scores++];
    }
     
  7. Sykte macrumors regular

    Joined:
    Aug 26, 2010
    #7

    It depends, I would suggest for you when you create the button.


    Also, from the code given I don't see why you would need to retain the button. You could drop both the retain/release.
     
  8. Sykte macrumors regular

    Joined:
    Aug 26, 2010
    #8

    No, it's an instance method. You may want to look at UICatalog example from apple.


    Edit:

    Let me clarify a little.

    addTarget:action:forControlEvents is an instance method of UIControl, which UIButton inherits from. So you would pass your addTarget:action:forControlEvents to your instance of UIButton.
     
  9. Nnavick thread starter macrumors regular

    Joined:
    Oct 13, 2010
    #9
    Could you write a little example with the code I posted up the the other codes such as the Score++ line

    thanksss
     
  10. Sykte macrumors regular

    Joined:
    Aug 26, 2010
    #10
  11. Nnavick thread starter macrumors regular

    Joined:
    Oct 13, 2010
    #11
    COMEEE ONN,I found in the UICatalog and found this code

    PHP:
         [HamburgerButton addTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchUpInside];
    and the method I created is

    PHP:
    - (void)action:(id)sender {
        
    ScoresCounter.text=[NSString stringWithFormat:@"%i",scores++];
    }
    Thank you very much!!!!!!!!
     
  12. Sykte macrumors regular

    Joined:
    Aug 26, 2010
    #12

    That's looking better!

    Most important part:
    Do you understand how this works and what everything does.
     
  13. Nnavick thread starter macrumors regular

    Joined:
    Oct 13, 2010
    #13
    i know what is the method behind sender but not sure if I understood is well
     

Share This Page