I have just successfully set up somewhat of a music player. It queries the user's iTunes and plays songs based on the query. For some reason, the button, won't change when the Playback state changes. I have both and NSLog and Label and they both reference it but when it comes to the button, nothing happens. Here's the code:
- (void) handle_PlaybackStateChanged: (id) notification {
MPMusicPlaybackState playbackState = [self.player playbackState];
if (playbackState == MPMusicPlaybackStatePaused) {
self.textLabel.text = #"play";
[PlayButton setTitle:#"STOP" forState:UIControlStateNormal]; // To set the title
NSLog (#"This is paused");
self.playBarButton.title = #"Play";
self.PlayButton = PlayButton;
[self.PlayButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
} else if (playbackState == MPMusicPlaybackStatePlaying) {
self.textLabel.text = #"pause";
[PlayButton setTitle:#"Pause" forState:UIControlStateNormal];
self.PlayButton = PauseButton;
NSLog (#"This is playing");
} else if (playbackState == MPMusicPlaybackStateStopped) {
self.textLabel.text = #"Play";
self.PlayButton = PlayButton;
[self.player stop];
}
}
Any ideas?
I figured this out. I just needed to add the code below in viewDidLoad
if ([musicPlayer playbackState] == MPMusicPlaybackStatePlaying) {
[playPauseButton setImage:[UIImage imageNamed:#"pauseButton.png"] forState:UIControlStateNormal];
} else {
[playPauseButton setImage:[UIImage imageNamed:#"playButton.png"] forState:UIControlStateNormal];
}
Related
if( [[favbutton imageForState:UIControlStateNormal] isEqual:[UIImage imageNamed:#"wishlwhite.png"]])
{
[favbutton setImage:[UIImage imageNamed:#"wishlistwhite.png"] forState:UIControlStateNormal];
// other statements
}
else
{
[favbutton setImage:[UIImage imageNamed:#"wishlwhite.png"] forState:UIControlStateNormal];
// other statements
}
You really shouldn't be checking the image in the button to determine state. Declare a separate property to test, e.g.
#property (nonatomic, assign) BOOL isWishList; // or something
-(IBAction) toggleButton: (id) sender {
if (self.isWishList) {
[favbutton setImage:[UIImage imageNamed:#"wishlistwhite.png"] forState:UIControlStateNormal];
// other statements
} else {
[favbutton setImage:[UIImage imageNamed:#"wishlwhite.png"] forState:UIControlStateNormal];
// other statements
}
self.isWishList = !self.isWishList
}
I have a UITableView with CustomCell. Whenever UITableView is in editing mode i have following code in CustomCell.
- (void)willTransitionToState:(UITableViewCellStateMask)state{
[super willTransitionToState:state];
if (state == UITableViewCellStateShowingEditControlMask)
{
self.delBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[self.delBtn setFrame:CGRectMake(10, 15, 25, 25)];
[self.delBtn setImage:[UIImage imageNamed:#"noSelection.png"] forState:UIControlStateNormal];
buttonCurrentStatus = YES;
[self.delBtn addTarget:self action:#selector(delBtnPressed:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:self.delBtn];
}
else
{
if(self.delBtn)
{
[self.delBtn removeFromSuperview];
self.delBtn = nil;
}
} }
- (void)delBtnPressed:(id)sender {
if (buttonCurrentStatus == NO)
{
buttonCurrentStatus = YES;
[self.delBtn setImage:[UIImage imageNamed:#"noSelection.png"] forState:UIControlStateNormal];
}
else
{
buttonCurrentStatus = NO;
[self.delBtn setImage:[UIImage imageNamed:#"selection.png"] forState:UIControlStateNormal];
} }
Now how can i get indexPath from CustomCell of UITableview ?
You can use UITableView indexPathForCell:.
Obviously, for that you'll need a reference to the table inside the cell class. If you want to make it a property of your cell, be sure to make it weak to avoid a reference cycle.
I have found the best way of doing this is to create Delegate/Protocol on cell. Make the delegate the ViewController and pass it in. Then you can call indexPathForCell on the tableview at that point. Example below.
- (void)willTransitionToState:(UITableViewCellStateMask)state{
[super willTransitionToState:state];
if (state == UITableViewCellStateShowingEditControlMask)
{
self.delBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[self.delBtn setFrame:CGRectMake(10, 15, 25, 25)];
[self.delBtn setImage:[UIImage imageNamed:#"noSelection.png"] forState:UIControlStateNormal];
buttonCurrentStatus = YES;
[self.delBtn addTarget:self action:#selector(delBtnPressed:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:self.delBtn];
}
else
{
if(self.delBtn)
{
[self.delBtn removeFromSuperview];
self.delBtn = nil;
}
} }
- (void)delBtnPressed:(id)sender {
if (buttonCurrentStatus == NO)
{
buttonCurrentStatus = YES;
[self.delBtn setImage:[UIImage imageNamed:#"noSelection.png"] forState:UIControlStateNormal];
}
else
{
buttonCurrentStatus = NO;
[self.delBtn setImage:[UIImage imageNamed:#"selection.png"] forState:UIControlStateNormal];
}
if ([self.delegate respondsToSelector:(customCelldelBtnPressed:)]) {
[self.delegate customCelldelBtnPressed:self];
}
}
// of course you will need to actually create the delegate or protocol and implement it on the view controller.
I need to programmatically click a button: [play sendActionsForControlEvents:UIControlEventTouchUpInside];
But when I do that the void was called twice! I need to start a AVPlayer (in ViewDidLoad), but the music started twice too. Please help :).
The play code:
- (IBAction)playStream:(id)sender {
NSLog(#"%#", #"PlayStream clicked: ", sender);
if(clicked == 0) {
clicked = 1;
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[self becomeFirstResponder];
[play setImage:[UIImage imageNamed:#"pause"] forState:UIControlStateNormal];
[playerr play];
}
else {
[playerr stop];
[play setImage:[UIImage imageNamed:#"Start"] forState:UIControlStateNormal];
[audio setActive:NO error:nil];
clicked = 0;
}
}
How about:
#implementation MyClassName{
BOOL musicPlaying;
}
- (IBAction)playStream:(id)sender {
if(musicPlaying){
[self pauseMusic];
}else{
[self playMusic];
}
}
-(void)playMusic{
musicPlaying= YES;
[play setImage:[UIImage imageNamed:#"pause"] forState:UIControlStateNormal];
[playerr play];
}
-(void)pauseMusic{
musicPlaying= NO;
[playerr stop];
[play setImage:[UIImage imageNamed:#"Start"] forState:UIControlStateNormal];
[audio setActive:NO error:nil];
}
Now instead of tapping the button programmatically, just call playMusic or pauseMusic.
I have a button on my custom camera overlay that invokes this method:
- (void) changeFlash:(id)sender
{
//UIImagePickerControllerCameraFlashModeOff = -1,
//UIImagePickerControllerCameraFlashModeAuto = 0,
//UIImagePickerControllerCameraFlashModeOn = 1
NSLog(#"before %d", self.picker.cameraFlashMode);
switch (self.picker.cameraFlashMode) {
case UIImagePickerControllerCameraFlashModeAuto:
[(UIButton *)sender setImage:[UIImage imageNamed:#"flashOn"] forState:UIControlStateNormal];
self.picker.cameraFlashMode = UIImagePickerControllerCameraFlashModeOn;
break;
case UIImagePickerControllerCameraFlashModeOn:
[(UIButton *)sender setImage:[UIImage imageNamed:#"flashOff"] forState:UIControlStateNormal];
self.picker.cameraFlashMode = UIImagePickerControllerCameraFlashModeOff;
break;
case UIImagePickerControllerCameraFlashModeOff:
[(UIButton *)sender setImage:[UIImage imageNamed:#"flashAuto"] forState:UIControlStateNormal];
self.picker.cameraFlashMode = UIImagePickerControllerCameraFlashModeAuto;
break;
}
NSLog(#"after %d", self.picker.cameraFlashMode);
}
self.picker.cameraFlashMode is always 0, before and after changing it. What am I doing wrong?
Also, the flash never works, even though it's supposedly stuck in UIImagePickerControllerCameraFlashModeAuto.
self.picker is nil. This is a common mistake, and you need to check this often, because, unlike Java and other languages, it's valid to send a message to nil in Objective-C.
After i implemented a code for give an check image for a button but i want to give check image for no of buttons.
note: if i click on one button check image can be displayed and the same time i click on another button check image displayed on that particular button and previous button comes normal position.
i implement the code for single button here like this.
-(void) setChecked:(BOOL) check
{
_checked = check;
if( _checked )
{
UIImage* img = [UIImage imageNamed:#"btn_check_on.png"];
[self setImage:img forState:UIControlStateNormal];
}
else
{
UIImage* img = [UIImage imageNamed:#"bread_Wheat_rectangle.png"];
[self setImage:img forState:UIControlStateNormal];
}
}
The above code is executed successfully but how to use this code for no of buttons.
please suggest any tutorial regarding my problem
This is how I have implemented it for one button. You can use it for more buttons too.
-(IBAction)ButtonAction
{
if (Flag==0)
{
Flag=1;
[myButton setImage:[UIImage imageNamed:#"checkbox-filled.png"] forState:UIControlStateNormal];
}
else
{
[myButton setImage:[UIImage imageNamed:#"checkbox.png"] forState:UIControlStateNormal];
Flag=0;
}
}
Note : If you want only one button to get checked Just set all other buttons image as checkbox.png and the selected one's checkbox-filled.png.
EDIT
You can make a class for checkbox and then use it. Here is the code...
CheckButton.h
#import <Foundation/Foundation.h>
#interface CheckButton : UIButton {
BOOL _checked;
int chkButtonClickVal;
}
#property (nonatomic, setter=setChecked:) BOOL checked;
-(void) setChecked:(BOOL) check;
-(int)chkButtonClickVal;
#end
CheckButton.m
#import "CheckButton.h"
#implementation CheckButton
#synthesize checked = _checked;
-(id) init
{
if( self=[super init] )
{
chkButtonClickVal=0;
self.checked = NO;
[self addTarget:self action:#selector(OnCheck:) forControlEvents:UIControlEventTouchUpInside];
}
return self;
}
-(void) awakeFromNib
{
self.checked = NO;
[self addTarget:self action:#selector(OnCheck:) forControlEvents:UIControlEventTouchUpInside];
}
-(void) dealloc
{
chkButtonClickVal=0;
[super dealloc];
}
-(void) setChecked:(BOOL) check
{
_checked = check;
if( _checked )
{
UIImage* img = [UIImage imageNamed:#"checkbox-checked.png"];
[self setImage:img forState:UIControlStateNormal];
chkButtonClickVal=1;
}
else
{
UIImage* img = [UIImage imageNamed:#"checkbox.png"];
[self setImage:img forState:UIControlStateNormal];
chkButtonClickVal=2;
}
//NSLog(#"%d",chkButtonClickVal);
}
-(int)chkButtonClickVal
{
return chkButtonClickVal;
}
-(void) OnCheck:(id) sender
{
self.checked = !_checked;
}
#end
I have done it in same way. Try you'll be able to achieve it.
Good Luck :)
After long practicing this problem we can use switch cases it can be done very easily
switch (currenttagvalue) {
case 1:
[level1 setImage:[UIImage imageNamed:#"one_time_selected.png"] forState:UIControlStateNormal];
[level2 setImage:[UIImage imageNamed:#"ic_launcher.png"] forState:UIControlStateNormal];
[level3 setImage:[UIImage imageNamed:#"bread_sourdough_rectangle.png"] forState:UIControlStateNormal];
}
in this level is "IBOutlet UIButton level1;"
And then i implement so more buttons