I have multiple UIButtons - redButton, greenButton, yellowButton, etc. When you tap one of these buttons, another UIImageView will change its image accordingly to, say, redImage, greenImage, yellowImage.
How would I implement this? I've tried this:
[self.redButton addTarget:self action:#selector(changeColour:) forControlEvents:UIControlEventTouchUpInside];
[self.greenButton addTarget:self action:#selector(changeColour:) forControlEvents:UIControlEventTouchUpInside];
[self.yellowButton addTarget:self action:#selector(changeColour:) forControlEvents:UIControlEventTouchUpInside];
And in changeColour:
-(void)changeColour:(id)sender{
// How do I know which button was pressed?
}
All I'd like to know is which button was pressed. Preferrably, I would like it so when a button is tapped, it changes an int variable called colour. So, something like:
if greenButton was tapped, change int = 1. If redButton was tapped, change int = 2.
I've searched StackOverflow and Google and the results say to include a method in the #selector of action:. So what would I have to put in changeColour to recognise which button called changeColour?
-(void)changeColour:(id)sender
{
if (sender == self.redButton) {
// redButton action
} else if (sender == self.greenButton) {
// greenButton action
} else if (sender == self.yellowButton) {
// yellowButton action
}
}
to do these kinds of identification the sender attribute was introduced.
You can use tag property of UIView.
i.e.
self.redButton.tag = 1;
self.yellowButton.tag = 2;
In the chnageColor method, you can retrieve the same.
-(void)changeColour:(id)sender
{
// How do I know which button was pressed?
UIButton *btn = (UIButton *)sender;
if(btn.tag == 1)
//Red is pressed..
//and you can check for other tags...
}
Tap on the UIButton in the Interface Builder, and change the tag property of it in Attributes Inspector. Then access this tag property programmatically in your view controller to get to know which button was pressed.
Related
I need to implement the uitableview selection with the design similar to the underseen design.
Please let me know how to accomplish this.
The best way for implementing checkbox is, you can take button and toggle the image.UIButton is a subclass of UIController,which has a selected property. You can use this property to toggle the on/off behaviour of a checkbox
Create a custom class of UITableview cell
Add number of buttons you wants on the cell
Give class name of UITableViewCell to the cell of tale view
Connect IB_Outlates of Buttons
In the delegate cellForRowAtIndexpath
myButton.tag = indexPath.row
[cell.myButton addTarget:self action:#selector(shippingBtnAction:) forControlEvents:UIControlEventTouchUpInside];
- (void)shippingBtnAction:(id)sender
{
UIButton* btn = (UIButton*)sender;
btn.selected = ! btn.selected;
if (btn.isSelected)
{
_isDefaultShipping = 1;
[self.shippingBtn setImage:[UIImage imageNamed:#"check"] forState:UIControlStateNormal];
}
else
{
_isDefaultShipping = 0;
[self.shippingBtn setImage:[UIImage imageNamed:#"uncheck"] forState:UIControlStateNormal];
}
}
I am new to Objective-C so i am mostly using Storyboard, Someone Please tell me how to create a checkbox using Xcode in iOS.
UISwitch is the standard control used in iOS for indicating an on/off, selected/unselected state. Why not use that?
UISwitch is the standard control used in IOS applications for making binary choices but if you want to use checkbox you can create a UIButton
#property (weak, nonatomic) IBOutlet UIButton *CheckBox;
- (IBAction)CheckBoxClick:(id)sender
and change its background image on click event of UIButton
- (IBAction)CheckBoxClick:(id)sender {
if(!checked){
[_CheckBox setImage:[UIImage imageNamed:#"checked.png"] forState:UIControlStateNormal];
checked = YES;
}
else if(checked){
[_CheckBox setImage:[UIImage imageNamed:#"unchecked.png"] forState:UIControlStateNormal];
checked = NO;
}
}
also there are more detailed answers to this question at
How to create a simple checkbox in iOS?
and if you dont want to use images you can check the following
Checkbox in iOS application
CheckBox is not available in object library. You can use third party library for that purpose or you can create it by your self.
There is the working code for checkbox.
create a class level variable and property of button in #inteface
#interface testViewController (){
BOOL checkBoxSelected;
}
#property (weak, nonatomic) IBOutlet UIButton *checkBox;
#end
in viewdidload set images for the button states.
[_checkBox setBackgroundImage:[UIImage imageNamed:#"checkBoxUnChecked.png"]
forState:UIControlStateNormal];
[_checkBox setBackgroundImage:[UIImage imageNamed:#"checkBoxChecked.png"]
forState:UIControlStateSelected];
and after create a button action in that button Action.
checkBoxSelected = !checkBoxSelected; /* Toggle */
[_checkBox setSelected:checkBoxSelected];
Hope it helps
1) Create Prototype cell in UITableView.
2) Add one button inside the cell and set button style to Custom.
3) Set the image for checkbox.
ios language doesn't use checkbox controller.
but you are use checkbox that you are inport two image select & unselect
Step 1> Create button and set image.
Step 2> Create button touch object method and put if condition for check & uncheck.
for example:
- (IBAction)btnLogin:(id)sender {
UIButton *btn = (UIButton *)sender;
if (btn.tag == 0) {
btn.tag = 1;
//set image checked.
}
else{
btn.tag = 0;
//set image unchecked.
}
}
You shouldn't need to subclass the UIButton class. By design, Objective-C favors composition over inheritance.
UIButton is a subclass of UIControl, which has a selected property. You can use this property to toggle the on/off behaviour of a checkbox, just the same way a UISwitch does.
You can attach an action to the button's touched up inside event, and perform the toggling in there, something like this:
// when you setup your button, set an image for the selected and normal states
[myCheckBoxButton setImage:nonCheckedImage forState:UIControlStateSelected];
[myCheckBoxButton setImage:nonCheckedImage forState:UIControlStateNormal];
- (void)myCheckboxToggle:(id)sender
{
myCheckboxButton.selected = !myCheckboxButton.selected; // toggle the selected property, just a simple BOOL
}
I have a view with 12 buttons. I want to be able to perform the action's button just once when i tap it and then set that specific button disabled. When i tap on another button i want the same thing to happen + set the other buttons enabled. Is there a solution?
You can give to each button a tag, and when you tap on a button using method viewWithTag you can enable and disable each button:
When you create UIButton, you can add:
UIButton *button0 = ...
button0.tag = 0;
UIButton *button1 = ...
button1.tag = 1;
//and so on
In every action of the buttons, you must pass the id object like this:
-(void)tapButtonOne:(id)sender {
//with this sender you can retrive the tag of the button clicked
UIButton *button = sender;
int buttonTag = button.tag
//now you can check every button and enable the other that haven't the same buttonTag
//with the first tag = 0 plus 12 the last tag will be 11 so i<12
for (int i = 0; i<12; i++) {
//self.view if you have added the buttons on self.view, otherwise you must write your view
UIButton *buttonTemp = (UIButton *)[self.view viewWithTag:i];
if(buttonTemp.tag != buttonTag) {
buttonTemp.enabled = YES;
}
else {
buttonTemp.enabled = NO;
}
}
}
Instead of creating an outlet, create an IBOutletCollection.
When you drag from the interface builder to your app, you can change the "Outlet" selection to "Outlet Collection".
Once you've done that for the first button, control drag all your buttons and link them to the same collection.
Connect all buttons to the same IBAction and make sure it specifies the sender (this is the default).
Then, in your action:
for (UIButton* button in self.yourOutletCollection){
if (button == sender){
// this is the button that was tapped
} else {
// all the other buttons go here
}
}
Enjoy!
I have 4 buttons. When they are taped they each create a UIView underneath it, they expand under the buttons. I want the view to go back to its original when the buttons are taped a second time. How is this done?
Set an instance BOOL and flip it accordingly. In the IBAction the button is tied to check the BOOL and call the appropriate method to adjust the view.
You can use the selected property of your button, something like:
-(void)yourButtonIsTapped:(UIButton*)button {
if(button.selected) { //first time
//expand the view
button.selected = NO;
}
else { // second time
//hide view
button.selected = YES;
}
}
You can link the buttons from the IB th this method for the touchUpInside event but you will have to change the return type from void to IBAction.
And I think there are several other solutions for this case but this is the faster one and the easiest to explain.
Set every tag button to 0, change it to the opposite value when you press the button. Here is what you'll get:
First time that you press the button: button's tag = 0, expand the view and turn it to 1.
Second time you press the button: button's tag = 1, collapse it and turn it to 0.
So you haven't to create a bool for every button but you can use your button's tag variable.
Code should be like this.
- (void)handleButton:(UIButton *)button{
if(button.tag == 0) { <expandButton> }
else { <collapseButton> }
button.tag = !button.tag;
}
You can remove a view simply by calling
[viewUnderButton removeFromSuperview];
I hope I can ask this question clearly, I have a viewController that I build programmatically (not with NIB), this view controller has two buttons I draw on the lower portion of the view
"prev" and "next" what I'd like to do is, when I've reached the "end" I'd like to only draw the "prev" button, and not the "next", and vice-versa, when I'm at the beginning, I'd like to only draw the "next" and not the prev.
Any general ideas on how to approach this ?
Thanks,
uba
You can use "hidden" property of UIButton:
if (page == firstPage) {
self.myButtonPrev.hidden = YES;
} else {
self.myButtonPrev.hidden = NO;
}
if (page == lastPage) {
self.myButtonNext.hidden = YES;
} else {
self.myButtonNext.hidden = NO;
}
The first thing to do is to addTarget for your button to listen to touch events
[yourButtonNameHere addTarget:self action:#selector(yourCallBackFunctionNameHere:) forControlEvents:UIControlEventTouchUpInside];
What this does is whenever your button is pressed it calls the yourCallBackFunctionNameHere function. Do this for the other button too.
The semi colon after the function indicates that the function has to send the information of the UIElement that caused the event to occur.
Assign tags to both the buttons.
youButtonNameHere.tag=yourTag;
In the function check which button is sending the UIcontrolEvent by comparing the tags
- (void)yourFunctionNameHere:(id)sender {
UIButton *yourButton =(UIButton *)sender;
if(yourButton.tag==501){
// logic to check if this is the first or last page and act accordingly
yourButton.hidden = YES / NO based on what you want to do.
}else{
// logic to do otherwise.
}