I can't get this code to work.
Declared playpauseButton just above the given below statements
UIButton *playpauseButton = [UIButton buttonWithType:UIButtonTypeCustom];
Getting local declaration of playpauseButton hides instance variable warning message for following statements
playpauseButton addTarget:self action:#selector(playpauseAction:) forControlEvents:UIControlEventTouchUpInside];
playpauseButton.frame = CGRectMake(0, 0, 30, 30);
[playpauseButton setImage:[UIImage imageNamed:#"Play Icon.png"] forState:UIControlStateNormal];
[playpauseButton setImage:[UIImage imageNamed:#"pause.png"] forState:UIControlStateSelected];
UIBarButtonItem *play = [[UIBarButtonItem alloc] initWithCustomView:playpauseButton];
After i defined playpauseAction method
-(void)playpauseAction:(id)sender {
if( playpauseButton.state == UIControlStateNormal ){
[sender setImage:[UIImage imageNamed:#"pause.png"] forState:UIControlStateSelected];
[audioPlayer play];
self.timer = [NSTimer scheduledTimerWithTimeInterval:11.0
target:self
selector:#selector(displayviewsAction:)
userInfo:nil
repeats:NO];
} else if (playpauseButton.state == UIControlStateSelected)
{
[sender setImage:[UIImage imageNamed:#"Play Icon.png"] forState:UIControlStateNormal];
[audioPlayer pause];
[self pauseTimer];
} else if (playpauseButton.state == UIControlStateNormal)
{
[sender setImage:[UIImage imageNamed:#"pause.png"] forState:UIControlStateSelected];
[audioPlayer play];
[self resumeTimer];}}
Some help please.
Thanks.
It's telling you that you have two declarations of playpauseButton. One is in the interface section of your class (the instance variable) and the other is a local variable.
It's not an actual error but the warning tells you that you may get results different from what you want.
Related
My program execute the buttonPressed method and go through the if else statement but the text on my button still does not changed. Is there something I had missed? I even already customized it in xib but the text of the buttonPlayMusic is remained as "Play" even it goes into the other statement.
I think I had connected the button too.
[viewDidLoad]
{
self.buttonPlayMusic = [UIButton buttonWithType:UIButtonTypeRoundedRect];
self.buttonPlayMusic.frame = CGRectMake(((scrollView.frame.size.width - 200) / 2) + cx, 360, 200, 50);
[self.buttonPlayMusic setTitle:#"Play" forState:UIControlStateNormal];
[self.buttonPlayMusic addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
self.toggleButonPlayMusic = YES; //YES to play, NO to stop music
[scrollView addSubview:self.buttonPlayMusic];
}
-(IBAction)buttonPressed:(id)sender{
NSLog(#"SSS");
if (self.toggleButonPlayMusic == YES) {
[self.buttonPlayMusic setTitle:#"Stop" forState:UIControlStateNormal];
self.toggleButonPlayMusic = NO;
NSLog(#"RRR");
}else{
[self.buttonPlayMusic setTitle:#"Play" forState:UIControlStateNormal];
self.toggleButonPlayMusic = YES;
NSLog(#"ZZZ");
}
}
no need to take extra boolian variable just put this code and it worked fine
-(void)viewDidLoad
{
[super viewDidLoad];
//Do any additional setup after loading the view, typically from a nib.
self.buttonPlayMusic = [UIButton buttonWithType:UIButtonTypeRoundedRect];
self.buttonPlayMusic.frame = CGRectMake(((scrollView.frame.size.width - 200) / 2) + cx, 360, 200, 50);
[self.buttonPlayMusic setTitle:#"Play" forState:UIControlStateNormal];
[self.buttonPlayMusic addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[self.buttonPlayMusic setSelected:TRUE]; //YES to play, NO to stop music
[scrollView addSubview:self.buttonPlayMusic];
}
-(IBAction)buttonPressed:(id)sender
{
if (self.buttonPlayMusic.selected == TRUE)
{
[self.buttonPlayMusic setTitle:#"Stop" forState:UIControlStateNormal];
[self.buttonPlayMusic setSelected:FALSE];
NSLog(#"RRR");
}
else
{
[self.buttonPlayMusic setTitle:#"Play" forState:UIControlStateNormal];
[self.buttonPlayMusic setSelected:TRUE];
NSLog(#"ZZZ");
}
}
Make sure that you are dragging a reference from the file to your viewController using ctrl drag and putting it in your #interface (sometimes you can accidentally get rid of these). This post has some methods for making sure the connection is set up properly.
Try this
-(IBAction)buttonPressed:(id)sender
{
UIButton *btn =(UIButton*)sender;
if (self.buttonPlayMusic.selected == TRUE)
{
[btn setTitle:#"Stop" forState:UIControlStateNormal];
[self.buttonPlayMusic setSelected:FALSE];
NSLog(#"RRR");
}
else
{
[btn setTitle:#"Play" forState:UIControlStateNormal];
[self.buttonPlayMusic setSelected:TRUE];
NSLog(#"ZZZ");
}
}
-(IBAction)buttonPressed:(id)sender
{
if (self.buttonPlayMusic.titleLabel.text == #"Play")
{
[self.buttonPlayMusic setTitle:#"Stop" forState:UIControlStateNormal];
[self.buttonPlayMusic setSelected:FALSE];
NSLog(#"RRR");
}
else if (self.buttonPlayMusic.titleLabel.text == #"Stop")
{
[self.buttonPlayMusic setTitle:#"Play" forState:UIControlStateNormal];
[self.buttonPlayMusic setSelected:TRUE];
NSLog(#"ZZZ");
}
}
I am created two buttons that are right next to each other to mimic a segmented control. I am doing this to customize the appearance beyond what the UIKit allows. I decided to use the selected property to keep a button pressed. I have two images that one for each state normal and selected.
The problem is that when I select a button, the button highlights and turns dark, because of the hightlight state. I decided to use the selected image for the highlight state too, but it flashes, any ideas or suggestions.
- (void)leftSegmentPressed:(id)sender
{
if ([sender isSelected]) {
[sender setSelected:NO];
}
else {
[sender setSelected:YES];
}
}
For the "selected" button, disable it and manually switch the image for the state.
- (void) viewDidLoad
{
[rightSegmentButton setImage:[UIImage imageNamed:#"unselected.png"] forState:UIControlStateNormal];
[rightSegmentButton setImage:[UIImage imageNamed:#"selected.png"] forState:UIControlStateDisabled];
[leftSegmentButton setImage:[UIImage imageNamed:#"unselected.png"] forState:UIControlStateNormal];
[leftSegmentButton setImage:[UIImage imageNamed:#"selected.png"] forState:UIControlStateDisabled];
}
- (void)leftSegmentPressed:(id)sender
{
sender.enabled = NO;
rightSegmentButton.enabled = YES;
}
- (void)rightSegmentPressed:(id)sender
{
sender.enabled = NO;
leftSegmentButton.enabled = YES;
}
Check whether the image you given is in your Bundle or check image name you given is in lower case or not. Then write like
[button1 setImage:[UIImage imageNamed:#"normal1.png"] forState:UIControlStateNormal];
[button1 setImage:[UIImage imageNamed:#"selected1.png"] forState:UIControlStateSelected];
[button2 setImage:[UIImage imageNamed:#"normal2.png"] forState:UIControlStateNormal];
[button2 setImage:[UIImage imageNamed:#"selected2.png"] forState:UIControlStateSelected];
button1.tag = 1;
button2.tag = 2;
[button1 addTarget:self action:#selector(buttonSelected:) forControlEvents:UIControlEventTouchUpInside]
[button2 addTarget:self action:#selector(buttonSelected:) forControlEvents:UIControlEventTouchUpInside]
in your button event method
-(void)buttonSelected:(id)sender {
if([sender tag] == 1) {
button1.selected = YES;
button2.selected = NO;
} else {
button1.selected = NO;
button2.selected = YES;
}
}
[button setAdjustsImageWhenHighlighted:NO];
This will prevent the flicker.
UIButton *yourButton1 = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
yourButton1.frame = CGRectMake(110.0, 360.0, 100.0, 30.0);
[yourButton1 setTitle:#"Left" forState:UIControlStateNormal];
yourButton.backgroundColor = [UIColor clearColor];
yourButton1.tag = 1;
[yourButton1 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal ];
UIImage *buttonImageNormal = [UIImage imageNamed:#"yourNormalImage.png"];// set normal image
UIImage *strechableButtonImageNormal = [buttonImageNormal stretchableImageWithLeftCapWidth:12 topCapHeight:0];
[yourButton1 setBackgroundImage:strechableButtonImageNormal forState:UIControlStateNormal];
UIImage *buttonImagePressed = [UIImage imageNamed:#"yourSelectedImage.png"];// set selected image
UIImage *strechableButtonImagePressed = [buttonImagePressed stretchableImageWithLeftCapWidth:12 topCapHeight:0];
[yourButton1 setBackgroundImage:strechableButtonImagePressed forState:UIControlStateHighlighted];
[yourButton1 addTarget:self action:#selector(leftSegmentPressed:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:yourButton1];
do same for another second button.
and in action method set bellow code..
- (void)leftSegmentPressed:(id)sender
{
UIButton *btnTemp = (UIBUtton *)sender;
if (btnTemp.tag == 1) {
[yourButton1 setSelected:YES];
[yourButton2 setSelected:NO];
}
else {
[yourButton1 setSelected:NO];
[yourButton2 setSelected:YES];
}
}
This rewind method is for rewind button as well as for play button.
-(void)rewind:(id)sender
{
[timer invalidate];
audioPlayer.currentTime = 0;
MainViewController *viewController = [[MainViewController alloc] init];
viewController.view.frame = CGRectMake(0, 0, 320, 480);
[self.view addSubview:viewController.view];
[self.view addSubview:toolbar];
[viewController release];
if([audioPlayer isPlaying])
{
[sender setImage:[UIImage imageNamed:#"Play Icon.png"] forState:UIControlStateSelected];
[audioPlayer pause];
[self pauseTimer];
[self pauseLayer:self.view.layer];
}else{
[sender setImage:[UIImage imageNamed:#"pause.png"] forState:UIControlStateNormal];
[audioPlayer play];
[self resumeTimer];
[self resumeLayer:self.view.layer];
if(isFirstTime == YES)
{
self.timer = [NSTimer scheduledTimerWithTimeInterval:11.0
target:self
selector:#selector(displayviewsAction:)
userInfo:nil
repeats:NO];
isFirstTime = NO;
}
}
}
Now in this method sender is rewind button and toggling of play/pause is happening on rewind button whereas i want this toggling on play button rather then sender which is rewind button. So i tried giving the name of the actual button on which i want toggling to happen and i defined that button as play button in my code and whose action method i have added in this rewind method.
That's how defined the play button
//Add Play Button
UIButton *playButton = [UIButton buttonWithType:UIButtonTypeCustom];
[playButton addTarget:self action:#selector(playpauseAction:) forControlEvents:UIControlEventTouchUpInside];
playButton.frame = CGRectMake(0, 0, 30, 30);
[playButton setImage:[UIImage imageNamed:#"Play Icon.png"] forState:UIControlStateNormal];
[playButton setImage:[UIImage imageNamed:#"pause.png"] forState:UIControlStateSelected];
UIBarButtonItem *play = [[UIBarButtonItem alloc] initWithCustomView:playButton];
and this is the method of play button which i added in the rewind button
-(void)playpauseAction:(id)sender
{
if([audioPlayer isPlaying])
{
[sender setImage:[UIImage imageNamed:#"Play Icon.png"] forState:UIControlStateSelected];
[audioPlayer pause];
[self pauseTimer];
[self pauseLayer:self.view.layer];
}
else
{
[sender setImage:[UIImage imageNamed:#"pause.png"] forState:UIControlStateNormal];
[audioPlayer play];
[self resumeTimer];
[self resumeLayer:self.view.layer];
if(isFirstTime == YES)
{
self.timer = [NSTimer scheduledTimerWithTimeInterval:11.0
target:self
selector:#selector(displayviewsAction:)
userInfo:nil
repeats:NO];
isFirstTime = NO;
}
}
}
now problem i m facing at this time is that when i m clicking the rewind button toggling should happen on play button than rewind button.
But to fix this problem i simply replaced sender with playButton in the rewind method like this
[playButton setImage:[UIImage imageNamed:#"Play Icon.png"] forState:UIControlStateSelected];
but then i get warning message in red that use of undeclared identifier playButton.
How can i tell rewind method to do toggling on playButton rather then on rewind button.
Please help.
Thanks.
Your "undeclared identifier playButton" is telling you that you don't have an instance variable playButton.
It is probably only visible in the method where you created it, but you need to save it into an iVar to be able to access it from the other method.
Trying to solve my Other question. This time trying this way
Added multiple methods to one button but when clicking on button it does nothing looks like those methods are not working.
//Add Play Button
UIButton *playpauseButton = [UIButton buttonWithType:UIButtonTypeCustom];
[playpauseButton addTarget:self action:#selector(playAction:) forControlEvents:UIControlEventTouchUpInside];
[playpauseButton addTarget:self action:#selector(playpauseAction:) forControlEvents:UIControlEventTouchUpInside];
playpauseButton.frame = CGRectMake(0, 0, 30, 30);
[playpauseButton setImage:[UIImage imageNamed:#"Play Icon.png"] forState:UIControlStateNormal];
[playpauseButton setImage:[UIImage imageNamed:#"pause.png"] forState:UIControlStateSelected];
UIBarButtonItem *play = [[UIBarButtonItem alloc] initWithCustomView:playpauseButton];
- (void)playAction:(id)sender{
[audioPlayer play];
self.timer = [NSTimer scheduledTimerWithTimeInterval:11.0
target:self
selector:#selector(displayviewsAction:)
userInfo:nil
repeats:NO];
}
-(void)playpauseAction:(id)sender {
if ([audioPlayer isPlaying]){
[sender setImage:[UIImage imageNamed:#"Play Icon.png"] forState:UIControlStateSelected];
[audioPlayer pause];
[self pauseTimer];
} else {
[sender setImage:[UIImage imageNamed:#"pause.png"] forState:UIControlStateNormal];
[audioPlayer play];
[self resumeTimer];
self.timer = [NSTimer scheduledTimerWithTimeInterval:11.0
target:self
selector:#selector(displayviewsAction:)
userInfo:nil
repeats:NO];
}
}
-(void)pauseTimer{
pauseStart = [[NSDate dateWithTimeIntervalSinceNow:0] retain];
previousFireDate = [[timer fireDate] retain];
[timer setFireDate:[NSDate distantFuture]];
}
-(void)resumeTimer{
float pauseTime = -1*[pauseStart timeIntervalSinceNow];
[timer setFireDate:[previousFireDate initWithTimeInterval:pauseTime sinceDate:previousFireDate]];
[pauseStart release];
[previousFireDate release];
}
It shows play button when clicking on it nothing happens. It should play the audio file, start the NSTimer, start loading views and toggle between play and pause button. Any idea what is wrong.
Thanks.
When the user taps the button, the playAction: calls [audioPlayer play].
Immediately afterwards, the playpauseAction: evaluates [audioPlayer isPlaying] to YES and does [audioPlayer pause].
I Have a UIButton and it has multiple actions associated with it
To play audio file
Toggle between play pause button
Display views
If i comment out the displayViewAction: it is working fine (i.e playing audio file and also toggling to pause button). But if I use the displayViewAction: method it is playing audio file displaying view but before displaying the view it should immediately toggle the state of the pause button, but it is not.
Code bellow for reference:
Code for UIButton:
UIButton *playpauseButton = [UIButton buttonWithType:UIButtonTypeCustom];
[playpauseButton addTarget:self action:#selector(playpauseAction:) forControlEvents:UIControlEventTouchUpInside];
[playpauseButton addTarget:self action:#selector(displayviewsAction:) forControlEvents:UIControlEventTouchUpInside];
playpauseButton.frame = CGRectMake(0, 0, 50, 50);
[playpauseButton setImage:[UIImage imageNamed:#"play.png"] forState:UIControlStateNormal];
[playpauseButton setImage:[UIImage imageNamed:#"pause.png"] forState:UIControlStateSelected];
UIBarButtonItem *playpause = [[UIBarButtonItem alloc] initWithCustomView:playpauseButton];
Code for Playpause button:
-(void)playpauseAction:(id)sender
{
if ([audioPlayer isPlaying]){
[sender setImage:[UIImage imageNamed:#"play.png"] forState:UIControlStateNormal];
[audioPlayer pause];
}
else {
[sender setImage:[UIImage imageNamed:#"pause.png"] forState:UIControlStateNormal];
[audioPlayer play];
}
}
Code for display view action:
- (void)displayviewsAction:(id)sender
{
self.view = [[[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]]autorelease];
[self.view setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
PageOneViewController *viewController = [[[PageOneViewController alloc] init]autorelease];
[self.view addSubview:viewController.view];
}
Can anyone please advise how I can make it work this way?
Attach only one action to your UIButton. When this action is called, perform various tasks depending upon the state of the application.
You can't attach many actions to your UIButton.
Why don't you attach a function which toggle play/pause, then displays the views?
[playpauseButton addTarget:self action:#selector(playpauseActionAndDisplayView:) forControlEvents:UIControlEventTouchUpInside];
then
-(void)playpauseActionAndDisplayView:(id)sender
{
[self playpauseAction:sender];
[self displayviewsAction:sender];
}
Changed the UIControlState in the playpauseAction method like this
-(void)playpauseAction:(id)sender
{
if
([audioPlayer isPlaying]){
[sender setImage:[UIImage imageNamed:#"play.png"] forState:UIControlStateSelected];
[audioPlayer pause];
} else {
[sender setImage:[UIImage imageNamed:#"pause.png"] forState:UIControlStateNormal];
[audioPlayer play];
[self displayviewsAction:sender];
}
}
and it worked.