UIButton remove style selected state - objective-c

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

Related

change a backgroundcolor of button inside a view that has a UITapGestureRecognizer and buttons

I have a view with a UITapGestureRecognizer(numberoftapsrequired = 1),and this view also has some buttons.
I want to change background-color of a button which is tapped, select only one to change.
I don’t know how to get this button to change its background-color. Like this figureexample
Get rid of the gesture
Create a IBOutletCollection called buttons with your buttons in it
Add the following code to your VC.
- (IBAction) didTapButton:(UIButton*) sender {
sender.backgroundColor = [UIColor redColor]; // or whatever
// create a IBOutletCollection called buttons with your buttons in it
for (UIButton *b in self.buttons) {
if (b != sender) {
b.backgroundColor = [UIColor blueColor]; // or whatever
}
}
}
Set this action to the "touch up inside" for each button.
If I got your question correctly, you are adding the tap gesture in order to recognize the click event. No need to give tap gesture in the entire view. Give actions for each of the buttons and change their background colors in the action method

Objective-C and Cocoa change title of the button with tag

Can someone please tell me how can I change the title of the button ? I have 10 buttons with tags from 1 to 10 and I want to change the title of the clicked button when I click on it. It always change the title of the last button with tag 10.
Buttons are connected by this:
#property (assign,nonatomic) IBOutlet NSButton *clickButton;
I also added synthesize in *.m file.
In *.m file I have:
NSLog(#"Button pressed: %i", (int)[sender tag]);
[clickButton setTitle:[NSString stringWithFormat:#"%c",'o']];
The method is also connected to the button:
(IBAction)startGame:(id)sender;
I do not want to make switch or make 10 buttons with no tags. Can I do this by any function to directly show text on the button which is clicked ?
The sender argument to your -startGame: method will be the NSButton that was clicked. So you could just do this:
- (IBAction)startGame:(id)sender
{
[sender setTitle:#"Title string"];
}
You need to connect all button action with same Action by doing so you well get the action at a common place with the clicked button object and by accessing the button you can change the title. :)

Obj C - Checking Current State of a Disclosure Arrow

I'm trying to differentiate between a disclose arrow being 'On' (pointed down) or 'Off' (pointed up). The posted code is completely wrong and just the general idea of what I want it to do.
- (IBAction)disclosureArrow:(NSControl *)sender{
if (disclosureArrow currentState:ON) {
//action
}
IBOutlet id discloseArrow is attached to the disclose button in interface builder and it receives a command from IBAction that is also attached to the button. How do I check the button's current state ? (In regards to disclosure arrows.)
I believe:
BOOL isOn = ([sender state] == NSOnState);
Should do the trick.
So you have set a custom image as button as your accessory view of a tableViewCell? Is that correct? Are you using static cells? How do you change the arrows?
The most easy way is probably to change the tag of your button regarding to its state. When assigning the arrow up, you give it the tag 1. Else you give it the tag 2.
Than you could read it like that:
- (IBAction)disclosureArrow:(NSControl *)sender
{
if (sender.tag == 1)
{
}
}

UIButton state selected

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.

How to make a custom tableView cell accessory

I have not yet found any really good examples on how to do this. There is an image that I want to use as the accessory button and when I put it in and click on it doesn't work. So it looks correct but doesn't work...
Here is my code:
[cell setAccessoryType:UITableViewCellAccessoryDetailDisclosureButton];
cell.accessoryView = [[UIImageView alloc]
initWithImage:[UIImage imageNamed:#"TableView_Green_Disclosure.png"]];
So how do I get my UIImageView to call accessoryButtonTappedForRowWithIndexPath whenever it is tapped?
A thorough reading of accessoryView and accessoryType would reveal that they are mutually exclusive ways to customize a cell.
Setting the accessoryType will cause the table view delegate method to be called when it is tapped.
Setting the accessoryView will ignore the setting of accessoryType and give you something to display. If you want to receive a callback from the custom view you've put in place, it should be a control that is wired up to do so. (Or any view with a gesture recognizer.)
If you use a button, and set its action to accessoryTapped:, you will receive the button as the "sender" argument. You can walk up the view hierarchy until you find a table view cell, and then ask your table view what the indexPath of that cell is. This will then get you an index into your model objects and you be able to act on it appropriately.
Alternate to the button, you can enable interaction on the UIImageView above, and add a gesture recognizer to it.
To make the button actually do something, you'll need to implement - (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath from UITableViewDelegate.
When an accessory button is tapped in a row, this method will be called and you'll have the chance to act appropriately using the passed in index path to determine which row's accessory was tapped.
Check the blog post hdr->cmdline for creating custom accessory view for UITableView.
The author used UIButton objects with images for custom accessory view.
To make use of the accessoryView - you would need to set the cell's accessoryType to UITableViewCellAccessoryNone deposit a UIButton (with associated image) into the cell and then wire it up to receive user touches. You might use something like the code below as the IBAction response to the cell's UIButton being touched:
- (IBAction) accessoryButtonPressed:(id) sender
{
NSUInteger pathInts[] = { 0,0 };
pathInts[1] = self.currentselectedrow; // ivar set when tableview row last selected
NSIndexPath* indexPath = [NSIndexPath indexPathWithIndexes:pathInts length:2];
[self tableView:mytableview accessoryButtonTappedForRowWithIndexPath:indexPath];
}
The UIButton would be wired to execute this glue code by way of a line inside your tableview's "cellForRowAtIndexPath:" function
[thecell setButtonTarget:self action:#selector(accessoryButtonPressed:)];
One thing I noticed is that the UIButton seems to want a 'swipe right' versus a simple 'tap' touch in order to trigger the event - but it could be my beta iOS that's the problem. Note that I had added a UIButton* object named 'cell_accessoryButton' to the Custom Cell source.
In the cell's source you'd support the 'setButtonTarget' call with code like this:
- (void) setButtonTarget:(MyViewController*)inTarget action:(SEL) inAction
{
[self.cell_accessoryButton addTarget: inTarget
action: (SEL) inAction
forControlEvents: UIControlEventTouchUpInside];
}
It's so much easier to just use the accessoryType reference and let iOS do the heavy lifting - but, if you want a custom graphic, etc - this is another path that works.