UIButton state selected - objective-c

I've seen a lot of info on changing the button image for selected but being a new I'm having a bit of trouble implementing a simpler version of it.
When the button is pressed it goes dark and I would like it to stay that way once it's been selected. So there are a few questions.
Do I create IBOutlet for the button and then and IBAction to change the state with something like button.state = SELECTED.
Sorry for the complete lack of any code to look at.
Edit: (id)sender is the button object right?
-(IBAction)journalEntryViewControllerClick: (id)sender
{
UIButton *button = (id)sender;
[button setSelected:YES];
}

You can set separate image for button's selected state (UIControlStateSelected) and in button action you can toggle its state:
- (void) btnAction:(UIButton*)button{
button.selected = !button.selected;
}

Yes that could be one way of doing it. If you want a "stateswitch" kind of look.
However you set the button.selected = YES;

You probably want a UISegmentedControl instead. UIButton's are meant to be momentary.

Related

UIButton remove style selected state

I would like to remove the selected state effect from the UIButtons.
When pressed a blue capsule appears next to the UIButton.
UPDATE with new problem
UIButton with type:system selects the targeted button. After changing the UIButton type to custom. This effect was removed. Other buttons were pressed that are located in the same view.
This is part of the UIButton function, if pressed it should run the code in this IBAction. Two UIButtons are connected to this function. With type system it selected the right UIButton, with type custom it seems to select at random.
- (IBAction) buttonAction:(id)sender
{
UIButton *btn = sender;
btn.selected = !btn.selected;
if([sender tag] == 1){
// run code UIButton 1
}
if([sender tag] == 2){
// run code UIButton 2
}
}
I hope this is clear.
Select the button from the xib file and change its type to custom in attributes inspector.
I found the solution to the second problem I described.
The UIButton type: custom doesn't have the selected trait as default.
The traits that were selected are: Button and User Interaction Enabled.
After selecting: selected it worked.
traits

NSPushOnPushOffButton Not Staying "Pushed In"

I'm trying to add a button to a view programmatically and I'd like it to stay "pushed in" when clicked, until it is clicked again. From what I've read it seems like the button to use for this would be NSPushOnPushOffButton. Here's Apple's discription for it:
The first click both highlights and causes the button to be “pushed
in” if the button is bordered; a second click returns it to its normal
state. This option is called “Push On Push Off” in Interface Builder’s
Button Inspector.
It wasn't working in my program so I made a SSCCE, which I'll include below. My problem is the button isn't staying in when clicked, but rather popping back out like a normal button. I'm not sure if I'm missing something about how this button is supposed to perform or if I'm simply implementing it incorrectly (This is the first time I've tried to do a UI programmatically), but other button types such as NSToggleButton work so unless there is something special about NSPushOnPushOffButton I don't see where I went wrong. if someone could suggest a better solution or explain where I went wrong that would be great. Thanks!
#import "AppDelegate.h"
#implementation AppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
NSRect buttonFrame = NSMakeRect(0,0,200,200);
NSButton *button = [[NSButton alloc]initWithFrame:buttonFrame];
[self.window.contentView addSubview:button];
[button setButtonType:NSPushOnPushOffButton];
}
#end
It seems that you have to set a "bezel style", for example
[button setBezelStyle:NSRegularSquareBezelStyle];
After adding that to your sample code, it worked as expected.

In Xcode, make a button look like a text field or a textfield look like a button

In Xcode (iOS), I have a screen with a combination of buttons and text fields. However, they look different. Is there a way that I can have a button look like a textfield? Or is there a way I can make a textfield act like a button (perform some action when pressed and don't bring up the keyboard)?
Thanks
You can try one of these:
place a transparent (custom with no image) button on top of the text field and attach the desired action to it
show your view in the implementation of the textFieldShouldBeginEditing: UITextFieldDelegate protocol method
Subclassing is also an option, but it will probably complicate your task too much.
If You want to perform an action and do not bring up the keyboard when pressed on UITextField You can do it like this:
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
// do here everything you want
NSLog(#"Pressed on TextField!");
[self.view endEditing:YES]; // Hide keyboard
return NO;
}
This function will be called every time when pressed on textField.
** Don't forget to delegate UITextField.
Note: With this You will face the problem: if keyboard is shown it will not hide, so You need also add -endEditing:YES.
You need to custom one of them ... or both
Exemple : create a class CustomTextField : UITextField
in this class use :
#import <QuartzCore/QuartzCore.h>
Play with style
Late answer, but nevertheless. I did this to make a UIButton look like a UITextField
#property (weak, nonatomic) IBOutlet UIButton *resolutionButton;
And in viewDidLoad:
dispatch_async(dispatch_get_main_queue(), ^{
[[self.resolutionButton layer] setBorderColor: [UIColor colorWithWhite:0.6f alpha:0.6f].CGColor];
self.resolutionButton.layer.cornerRadius = 5.0f;
self.resolutionButton.layer.borderWidth = 0.5f;
}

Playing songs with button in UITableView

I'm creating a custom table that has a button which allows a user to preview a song when pressed. Most of my code works but I haven't figured out how to pass the player a particular song corresponding to the row in which the button was pressed.
For instance: if I have two rows and #1 says Jay Z and #2 says Red Hot Chili Peppers, I want to press the button in #1 to play Jay and to press the button in #2 for the Peppers. Simple. My code is flawed and no matter which row's button I press I can only get the same song to play.
I know why that's happening, but I don't know how to solve it. I'm just wondering if anyone could hit me with a few lines that could point me in the right direction.
I can't use didSelectRowAtIndexPath because I want something else to happen when the row itself is selected.
Will I need to create a method for this or is there something I've overlooked?
Thanks!
You could also set the tag property of each button you create during tableView: cellForRowAtIndexPath:, then when your buttonTapped event is called, look up the sender and find its tag. The tag property of UIView was provided for just this sort of problem.
If you need more information than that, you could create a UIButton subclass that stores any or all information needed about the associated song. Once again, you set that information during cellForRowAtIndexPath, to be retrieved when the button is tapped.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
// Dequeue a cell and set its usual properties.
// ...
UIButton *playButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[playButton addTarget:self action:#selector(playSelected:) forControlEvents:UIControlEventTouchUpInside];
// This assumes you only have one group of cells, so don't need to worry about the first index. If you have multiple groups, you'll need more sophisticated indexing to guarantee unique tag numbers.
[playButton setTag:[indexPath indexAtPosition:1]];
// ...
// Also need to set the size and other formatting on the play button, then make it the cell's accessoryView.
// For more efficiency, don't create a new play button if you dequeued a cell containing one - just set its tag appropriately.
}
- (void) playSelected:(id) sender;
{
NSLog(#"Play song number %d", [sender tag]);
}
Something like
- (void)buttonTapped:(UIView *)sender;
{
CGPoint pointInTableView = [sender convertPoint:sender.bounds.origin toView:self.tableView];
NSIndexPath *tappedRow = [self.tableView indexPathForRowAtPoint:pointInTableView];
// get song that should be played with indexPath and play it
}
something like in tableView: cellForRowAtIndexPath: give your button tag as index.row and bind the function below to button's touchup inside event
-(void)button_click:(UIView*)sender
{
NSInteger *index = sender.tag;
//play song on that index
}
I think this will help you!

How to prevent a UIButton from changing when touched?

I have a button that pretty much takes an entire xib so that I can push another viewController on the screen if any part of the original view is touched. I've created an IBOutlet to this button and it works great, however I don't want the entire window to turn "blue" when it is touched (similar to when a small button is pressed). I've tried the following two methods that I found in a similar SO post: How can I prevent a UIButton from highlighting when pressed?, but neither method works. Is there something else I need to set up?
- (void)viewDidLoad
{
[super viewDidLoad];
self.entireWindowButton.adjustsImageWhenHighlighted = NO;
[self.entireWindowButton setImage: [self.entireWindowButton imageForState:UIControlStateNormal] forState:UIControlStateHighlighted];
}
I realise this is old question but if you set the UIButtons type to custom then "adjustsImageWhenHighlighted = NO" will work and it is set to NO by default.