Using XCode 3.2, What I'm trying to do is have the user input data and then they have three choices of what to do with that data, i.e. there are three different options or three buttons that I want to have in the same window. I can't figure out to have that coded...
right now i have one button that is activated using the
-(IBAction)buttonPressed
{ (formula)
}
how do I do this with multiple buttons?
I've looked for answers but they are a bit different than what Im looking for. Thank you so much!
while creating buttons set mybutton.tag=0;like this.change 0 to ur own and compare them in buttonpressed function
use sender to
-(IBAction)buttonPressed:(id)sender
{
UIButton *temp=(UIButton*)sender;
if([temp tag] == 0)
(formula)
//button 0
}
if([temp tag] == 1)
(formula)
button 1
}
}
Button press code should look like this:
- (IBAction)buttonPressed:(id)sender {
UIButton *senderButton = (UIButton *)sender;
if (senderButton == btnOK) {
// this is the OK button
} else if (senderButton == btnCancel) {
// this is the Cancel button
}
}
A better way, though, is probably to write three separate methods, and hook each one up to its corresponding button in IB.
Related
I am trying to add a checkmark button when user select a button, so I create two IBOutletColletion both for buttons and check mark image :
The next step is when user select each of of buttons a checkmark image appears on above of each image,So in this part I have a problem which is checkmarks all show and all hide ! not when user select the specific button . Here is my code :
- (void)viewDidLoad {
//Hide checkmarks when app opens
for (UIImageView*checkMark in _checkMarkArray) {
checkMark.alpha = 0;
}
}
- (IBAction)button1:(id)sender {
for (UIButton*button in _ButtonsArray) {
if (sender == button) {
for (UIImageView*checkMark in _checkMarkArray) {
checkMark.alpha = 1;
}
}
}
}
Use Tag (Where Button and Above image tag are same)
- (IBAction)button1:(id)sender {
for (UIImageView*checkMark in _checkMarkArray) {
if(sender.tag == Checkmark.tag && sender.isSelected)
{
checkMark.alpha = 1;
}
if(sender.tag == Checkmark.tag && !sender.isSelected)
{
checkMark.alpha = 0;
}
}
}
}
better solution
create Two image
one with only Heart and another image is heart With Checkmark
set Only heart image in button's Normal state (deSelected)
and set combined image in selected state
Another solution I would suggest is to create a custom UIButton class, which contains two UIImageViews. In the button action method make the checkmark image hidden or visible accordingly. In this case you just need two images, one heart image and one tick image.
Your CustomButton.h will look something like this :-
#property (nonatomic, strong) UIImageView *checkMarkImageView, *backgroundImageView;
Give proper frames for the ImageViews in CustomButton.m file. Now in the view or view controller where you are planning to give the button action, change the checkmark image view's state accordingly. So the button action will look something like this :-
-(void)buttonClicked:(UIButton *)sender {
if (sender.checkMarkImageView.hidden == YES)
sender.checkMarkImageView.hidden = NO;
else
sender.checkMarkImageView.hidden = YES;
}
Is it possible to wrap several controls/buttons into a single button where all buttons react (highlight/unhighlight) to presses on any of the elements, and all share the same action?
I'd like to have multiple "parts" (mainly labels and icons) of a button that I can control the layout for individually, but I want them to all behave as a single button and highlight all of the parts when tapped.
Perhaps I could track the pressed state of each button and make all the other components have the same state?
You can drag the little dot to multiple buttons in your view and all those buttons will run the same IBAction code.
If you want to differentiate between them, you can use tags in interface builder and then call upon those tags with [sender tag]
So your IBAction could look a little like this:
- (IBAction)button_myButtonWasPressed:(id)sender {
if ([sender tag] == 0) {
// This will run for button tag 0
}
if ([sender tag] == 1) {
// This will run for button tag 1
}
}
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.
}
In my project I must control action of 40 buttons, but I don't want to create 40 IBAction, can I use only a IBAction, how?
If you're using interface builder to create the buttons, simply point them at the same IBAction in the relevant class.
You can then differentiate between the buttons within the IBAction method either by reading the text from the button...
- (IBAction)buttonClicked:(id)sender {
NSLog(#"Button pressed: %#", [sender currentTitle]);
}
...or by setting the tag property in Xcode and reading it back via [sender tag]. (If you use this approach, start the tags at 1, as 0 is the default and hence of little use.)
-(IBAction)myButtonAction:(id)sender {
if ([sender tag] == 0) {
// do something here
}
if ([sender tag] == 1) {
// Do something here
}
}
// in Other words
-(IBAction)myButtonAction:(id)sender {
switch ([sender tag]) {
case 0:
// Do something here
break;
case 1:
// Do something here
break;
default:
NSLog(#"Default Message here");
break;
}
Set all the buttons to use that one action. Actions generally have a sender parameter, which you can use to figure out which button is calling the action. One popular way to tell the difference between buttons is to assign a different value to each button's tag property. So you might have 40 buttons with tags ranging from 1 to 40. (0 probably isn't a great choice for a tag since that's what the default value is, and any button for which you forget to set the tag will have 0 as the tag value.)
This technique is most useful when all the buttons do approximately the same thing, like the buttons on a calculator or keyboard. If each of the buttons does something completely different, then you still end up with the equivalent of 40 methods, but you substitute your own switch statement for Objective-C's messaging system. In that case, it's often better just to spend the time to create as many actions as you need an assign them appropriately.
Sure. Just connect all buttons to the same action method in Interface Builder. Use the method's sender argument (possibly in conjunction with the buttons' tag property) to identify which button is sending the event.
Just use one IBAction and assign it to all your buttons.
Just used the above method myself , had a selection of buttons but converted them all and used switch case instead
-(IBAction)buttons:(id)sender
{
switch ([sender tag])
{
case 0 :
}
}
Seems like you are getting all the answers you need, but I wanted to add to everyone else's answers.
Whether you want to use one IBAction or 40 actions is up to what you want the buttons to do. If all the buttons do completely different things, you need all separate IBActions, but if you want all of them to do the same thing, you can use just one. I need more details of those buttons and action, but you probably have title for each button, so you can use that to differentiate each button and create a message or something that's being customized by the particular button pressed. Here is the example. Each time a button is pressed, a label displays a message that say "i.e.title of the button" pressed.
By doing it this way, you don't need to do switch case with all 40 patterns. You can still display or do something that's individualized by the button pressed with just 2-3 lines of code.
- (IBAction)button_Clicked:(UIButton *)sender {
//Get the buttons' titles.
NSString *title =[sender titleForState:UIControlStateNormal];
//Construct a message that includes the *title.
NSString *plainText=[NSString stringWithFormat:#"%# button pressed.", title];
//Assigns the *plainText to the label.
self.Label.text=plainText;
}
#end