button change the action of the other button

Discussion in 'iOS Programming' started by ken-kennedy, Dec 11, 2011.

  1. ken-kennedy Guest

    Joined:
    Dec 11, 2011
    #1
    Hello guys,

    i'm programming a new iPhone app and i need your help.
    i want to call an IBAction inside another IBAction.
    to be more clear, when you press a "button1" then you press the button "color" the background become red but when you press "button2" then you press the button "color" the back group turn blue

    Code:
    
    @property (retain, nonatomic) IBOutlet UILabel *nom;
    @property (retain, nonatomic) IBOutlet UIButton *color;
    @property (retain, nonatomic) IBOutlet UIButton *button1;
    @property (retain, nonatomic) IBOutlet UIButton *button2;
    
    - (IBAction)couleur;
    - (IBAction)action;
    - (IBAction)action2;
    

    Code:
    - (IBAction)action{
        [self couleur] ;
          nom.backgroundColor = [UIColor redColor]; 
    }
    
    - (IBAction)action2{
        [self couleur1] ;   
         nom.backgroundColor = [UIColor blueColor]; 
         
    }
    
    thanks,
     
  2. ken-kennedy thread starter Guest

    Joined:
    Dec 11, 2011
    #2
    i changed the .m code and now it work but after 3 press, the button disappear.
    Code:
    int s;
    - (IBAction)action{ 
        s=1; 
        [test2 release ];
        printf("1");
    }
    - (IBAction)action2{
        s=2; 
        
        [test release ];   
        printf("2");
    }
    -(IBAction)couleur{
        if (s==1){
            nom.backgroundColor = [UIColor redColor];
            [color release];   
            printf("marche");
        }
        if(s==2){
            nom.backgroundColor = [UIColor blueColor];
            [color release];
        }
        
    }
    

    is there a better way to do it? thanks
     
  3. jnoxx, Dec 11, 2011
    Last edited by a moderator: Dec 12, 2011

    jnoxx macrumors 65816

    jnoxx

    Joined:
    Dec 29, 2010
    Location:
    Aartselaar // Antwerp // Belgium
    #4
    Hmm, first off, you are making IBActions, why not make use of that? you forgot
    Code:
     id:sender 
    Because you can compare the sender to the actual button you are expecting, and do stuff based upon those.
    And your code looks so weird, you are using UIColor someColor.
    And after releasing, it's really complicated, (do you understand some basic memory management, or just trial and error?)
    And why call an IBAction, and inside call just 1 other action, it doesn't make sence in coding style. Just some questions so we can help you on the way.
     
  4. ArtOfWarfare, Dec 11, 2011
    Last edited by a moderator: Dec 12, 2011

    ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #5
    To change what action a button does you'll need to use:

    Code:
    - (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents
    So, for example, you might have:

    Code:
    - (IBAction)action:(id)sender
    {
        [button2 addTarget:self action:@selector(newAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    And you'd need to have set an IBOutlet for what button2 is and you'd need to define newAction: somewhere...
     
  5. jnoxx, Dec 11, 2011
    Last edited by a moderator: Dec 12, 2011

    jnoxx macrumors 65816

    jnoxx

    Joined:
    Dec 29, 2010
    Location:
    Aartselaar // Antwerp // Belgium
    #6
    or just on the IBAction
    Code:
    [self action:argument withOtherArgument:otherArgument];
    But i'm not sure what he's trying to produce, or the MVC aspect of it all.
     
  6. ken-kennedy thread starter Guest

    Joined:
    Dec 11, 2011
    #7
    thanks,i will try to be more clear.

    there are 3 simple button and 1 label.

    -when you press "button1" and then you press "button3",the background of the label become red.

    -when you press "button2"and then you press "button3",the background become blue.

    i hope you understand what i want to do now.thanks.
     
  7. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #8
    Seems to me, there's no need to change the action of any of the buttons. Here's a pseudo-code version of how I'd code this:

    - Setup an instance variable to store the "selected color"
    - When button1 is pressed, set the selected-color to red
    - When button2 is pressed, set the selected-color to blue
    - When button3 is pressed, set the background to the selected-color, stored in the ivar
     
  8. ken-kennedy thread starter Guest

    Joined:
    Dec 11, 2011
    #9
    dejo : thank you, with your help i don't have the memory problem anymore.
    but i have another problem. :D

    in my app now i have a UIview and inside that view i have two picture:ImageView .

    the first is shown from the start and the second is hidden. when i touch the view , the picture must change and become picture 2 until i release the screen.

    please can someone help me. thanks
     
  9. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #10
    Sounds like something that a UIButton, with its ability to specify a highlighted image, rather than using two UIImageViews, would be a better approach.
     
  10. ken-kennedy thread starter Guest

    Joined:
    Dec 11, 2011
    #11
    yes but i have to use the UIView,because i have to work with the tap and long press gesture recognizer.is there any solution ?
     
  11. ken-kennedy thread starter Guest

    Joined:
    Dec 11, 2011
    #12
    i find a solution for my problem .
    i put the first picture img1.jpeg into the UIImageView.
    and then in the IBAction :

    Code:
    myUIImageView.image = [UIImage imageNamed:@"img2.jpeg"];
    but when i release the screen , the img2 didn't hide .
     
  12. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #13
    Add another IBAction that runs when the image is released.
     
  13. ken-kennedy thread starter Guest

    Joined:
    Dec 11, 2011
    #14
    thanks,but i believe that adding another IBAction won't resolve my problem because when the view is pressed , the image change and also a sound is played. so i want when the view is released , the sound stop even if it doesn't finish playing and the image change again to the main image.
     
  14. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #15
    And what makes you think another IBAction can't handle that?
     
  15. ken-kennedy thread starter Guest

    Joined:
    Dec 11, 2011
    #16
    yes, i think i'm wrong sorry..but how to do this : "IBAction that runs when the image is released."

    i mean in the tap gesture recognizer we can chose the number of tap and touch and also enable the gesture recognizer but how to runs the IBAction when the view is released??
     
  16. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
  17. ken-kennedy thread starter Guest

    Joined:
    Dec 11, 2011
    #18
    thanks for your help again:D

    now i did this:


    Code:
    
    @interface ViewController : UIViewController  <AVAudioPlayerDelegate>
    {
    	AVAudioPlayer *player;
        
    }
    @property (nonatomic, retain) AVAudioPlayer *player;
    
    
    @property (retain, nonatomic) IBOutlet UIImageView *picture1;
    @property (retain, nonatomic) IBOutlet UIImageView *picture2;
    
    // when the user tap the screen
    -(IBAction)act1;
    -(IBAction)act2;
    
    //when the user release the screen
    -(IBAction)act12;
    -(IBAction)act22;
    

    in my .m file:


    Code:
    -(IBAction)act1{
        
    NSString *soundFilePath1 =
            [[NSBundle mainBundle] pathForResource: @"music1"ofType: @"caf"];
            NSURL *fileURL1 = [[NSURL alloc] initFileURLWithPath: soundFilePath1];
            AVAudioPlayer *newPlayer1 =
            [[AVAudioPlayer alloc] initWithContentsOfURL: fileURL1
                error: nil];
            [fileURL release];
            self.player = newPlayer1;
           
            
            [super viewDidLoad];
            
     [self.player play];
    
        picture1.image=[UIImage imageNamed:@"tap1.jpg"];
    }
    
    -(IBAction)act2{
    NSString *soundFilePath2 =
            [[NSBundle mainBundle] pathForResource: @"music2"ofType: @"caf"];
            NSURL *fileURL2 = [[NSURL alloc] initFileURLWithPath: soundFilePath2];
            AVAudioPlayer *newPlayer2 =
            [[AVAudioPlayer alloc] initWithContentsOfURL: fileURL2
                error: nil];
            [fileURL release];
            self.player = newPlayer2;
           
            
            [super viewDidLoad];
            
     [self.player play];
    
        picture2.image=[UIImage imageNamed:@"tap2.jpg"];
    }
    
    
    
    -(IBAction)ct12{
        
        picture1.image=[UIImage imageNamed:@"release1.jpg"];
    }
    
    -(IBAction)act22{
        
        picture2.image=[UIImage imageNamed:@"release2.jpg"];
    }
    
    
    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [self act1];
    [self act2];
    }
    
    
    - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    [self ct12];
    [self ct22];
    }
    
    
    
    

    but it don't work.. can somebody help me? thanks
     
  18. jnoxx macrumors 65816

    jnoxx

    Joined:
    Dec 29, 2010
    Location:
    Aartselaar // Antwerp // Belgium
    #19
    define "didn't" work, it's like saying, it broke, don't know what happened, fix it..
    So give a stacktrace, what did you log/breakpoints etc, maybe you should read up a bit more about how to debug etc.
     
  19. ken-kennedy thread starter Guest

    Joined:
    Dec 11, 2011
    #20
    yes i'm sorry ,i did define what didn't work with the edit but it seems like i didn't submit...

    i remove the audio part to find the problem first then i'll add it.so here what i have:

    the application run but...


    at the start:
    view1 show "release1.jpg" and view 2 show "release2.jpg"
    :until here it works fine

    when press view1:

    view 1 show "tap1.jpg" :that what i want
    but view 2 show "tap2.jpg" :that's the error

    when release view1:

    view 1 show "release1.jpg" : ok
    but view 2 stay "tap2.jpg": the error continue

    if then i press view 2
    view2 show "tap2.jpg" :eek:k
    but view 1 show "tap1.jpg" :the same error as before

    when release view2
    view2 show "release2.jpg": ok
    but view 1 show "tap1.jpg" :no

    thanks
     
  20. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #21
    First, you should either post the actual code or don't ignore the compiler warnings. Since in the code you provided, you are calling ct22 but have only defined act22.


    Well, that's not really an error (more of an issue) since the code is doing exactly what you are asking it to do. touchesBegan: calls act1 and act2. Part of the code of act2 is this:
    Code:
    picture2.image=[UIImage imageNamed:@"tap2.jpg"];
     
  21. ken-kennedy thread starter Guest

    Joined:
    Dec 11, 2011
    #22
    thanks,
    so is there any way to call only act1 when view1 is pressed and act2 when view 2 is pressed in the TouchesBegan?
     
  22. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #23
    Yes. But it seems you are really trying hard to emulate the exact behavior that a UIButton provides for you automatically. Since you don't seem to want to use the best controls Apple gives you for the job at hand, I'm afraid I'm going to have to bow out of providing you assistance but wish you luck in figuring it out.
     

Share This Page