Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

ken-kennedy

Guest
Original poster
Dec 11, 2011
11
0
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,
 
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
 
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.
 
Last edited by a moderator:
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...
 
Last edited by a moderator:
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.
 
Last edited by a moderator:
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.
 
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
 
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
 
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

Sounds like something that a UIButton, with its ability to specify a highlighted image, rather than using two UIImageViews, would be a better approach.
 
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 ?
 
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 .
 
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.
 
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??
 
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
 
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.
 
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
 
but it don't work.. can somebody help me? thanks

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.


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

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"];
 
thanks,
so is there any way to call only act1 when view1 is pressed and act2 when view 2 is pressed in the TouchesBegan?
 
thanks,
so is there any way to call only act1 when view1 is pressed and act2 when view 2 is pressed in the TouchesBegan?

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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.