How to increment a label value when click a button - iphone

In my iPad app.i want to increment a label value when click a button.
actually label values are sending from previous view. so now i clicked a button but the last label value was incremented. see the below picture.
in the above picture i want to increment the label value in between the minus(-) and plus(+) buttons. but when i clicked plus button in first view but the label value is incremented on the third view.
** the above three views shown on the picture are sub viewed the scroll view **
i'm using this code......
-(IBAction)plusbutton:(id)sender
{
val = [sender tag];
NSLog(#"the_tag %d",val);
itemref.countVal++;
[self createOrderView];
}

According to you
fist give same tag value for - and + button and label in that single view so when click on the button
in button action
NSArray *subviews=[self.scrollview subviews];
for(UIView *sb_local in subviews)
{
if(sb_local.tag==[sender tag])
{
if([sb_local isKindOfClass:[UILabel class]])
{
UILabel *new_label=(UILabel *)[sb_local viewWithTag:pictag];
new_label.text = #"your value";
//[new_label.text intValue]you get int value from that label.Increment or decrement that int value according your (+,-)button actions and assign again it to that label.
}
}
}
Please place this code in ibaction + and - buttons please give different actions for + and - buttons in each view

Related

iOS Determine button that was programmatically created

I have made a horizontal scroll view with images and buttons in it using a for loop, what i want to do now is when a button is pressed, open the image it corresponds to full screen. The issue im having is determining which button has been pushed. I am using :
[button addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
then :
-(void)buttonPressed:(UIButton *)sender {}
What can i do to fix this? Thanks
Which button has been pushed
Create a property to store a tag. In the buttonPressed method set the property to sender.tag
Now you can know which was the button who got pressed last.
The sender is the button that the user has tapped, so when you create the buttons you could use the tag property and set it to an index of an array where you hold your images. But this could be a bit unstable if you change the order or amount of images or buttons for example so be prepared to check for that.
one way to do so is to keep references of your button (with a property, an attribute in your class) and test if it is the good one in your buttonPressed method
- (void)buttonPressed:(UIButton *)sender {
if (sender == self.myButton) {
// DO YOUR WORK HERE
}
}
you can also create a method for this and only this one button
by the way it is better to say
- (IBAction)buttonPressed:(UIButton *)sender
you then can set the target of your button in the interface builder interface
set tag for each button in the for loop but.tag=i;
-(void)buttonPressed:(UIButton *)sender {
if (sender.tag==1){
//display image 1
}
else if ....
}

How to determine if a cell contains a button

I've created a cell named CELL .
My first task is add a button on the cell, which I have successfully added.
However, while retrieving the CELL I need to know if the cell is the reusable cell or not. If not, then create the cell and add the button, but if the cell exists and button does not, I need to add the button.
In my XIB I added the button in the cell. In some cells I need to show the button and in some I don't.
How can I determine if the cell contains a button? And if it does contain a button can I tag it?
Use -
for( int i =0 ; i < [cell.subviews count]; i++) {
if ([[cell.subviews objectAtIndex:i] isKindOfClass:[UIButton Class]] ) {
//Button is found, do whatever you want
UIButton *button = [cell.subviews objectAtIndex:i];
int tag = button.tag;
}
}
One approach is to iterate though subview which I personally think is bad idea.
The other approach is to set the tag of the cell say 100 for containing button.
Then you can check
if (cell.tag == 100) {
// Cell with button
} else {
// Cell without button
}

How to identify which button was clicked

I have a Button, Once the user submits a form the button image should change to DONE.PNG, or else it will remain as SUBMIT.PNG.
I need to know the following;
1.) How can we write a method, to know which button the user clicked. (If he clicked the button when it has the DONE.PNG or SUBMIT.PNG image on it)
my button click event is -(void)buttonClicked : (id)sender {}
Normally you would set the tag of the UIButton.
-Interface or storyboard you do it under the info about the UIButton element.
-Programatically you do it like this : myButton.tag = 23;
Then in the buttonClicked you do this:
UIButton* senderButton = (UIButton*) sender;
if(senderButton.tag==23) {
// It's the button as submit
// Set button image
senderButton.tag = 5;
}
if(senderButton.tag==5) {
// Button is done
}
Hope you get it working :)
As you are using a single button and changing its image only, then there are two simple ways that you can follow:
Check the name of image of the button if its DONE.PNG then do what is required and change the image else vis-versa.
Have a variable either you can take integer(it will help you if you have even more number of image changes on same button) to track button state/image.
For example we will typedef buttonState and check against it for suitable case.
In .h file
//Before interface declaration..
typedef enum
{
ButtonStateDone = 1,
ButtonStateSubmit,
//any other state that it may have.
} ButtonState
//in interface declaration..
ButtonState buttonState; //its a class level variable that we will use to track button state.
In .m file
Initially set buttonState as you show it on initial view. suppose button shows DONE.PNG
so buttonState = ButtonStateDone;
Now, in button action you will change this
if(buttonState == ButtonStateDone)
{
//do something and change button state.
}
if(buttonState == ButtonStateSubmit)
{
//do something and change button state.
}
Here, I used typedef, it is useful if you need to have more than 2 states for button else you can simply use a BOOL variable.
You can add a tag to the button i.e. NSInteger buttonTag = [sender tag] and then have conditional statements to check the tag values to determine which button was clicked.

UITabBar Action in a loop

I have created a view loading to it toolbar with few 7 buttons.
I want to add action for each - thus each one will perform different action all of them will load view but according to the user selection of the button I will load to the view different screen i.e buttons etc..
Here is my loop:
int i =0;
for (NSString *items in array)
{
//Set button style
[barButton setStyle:UIBarButtonItemStyleBordered];
//Set the button title
[barButton setTitle:[array objectAtIndex:i]];
[barButton setAction:#selector(tmp:)];
//Add the bar button to the array - later will load the array to the tool bar items list.
[itemsArray insertObject:barButton atIndex:i];
i++;
}
As you see setAction is now activate tmp method - I want to take it from the name of the method from the NSString value of the array - ideas?
You can use NSSelectorFromString function. See here :
http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Miscellaneous/Foundation_Functions/Reference/reference.html#//apple_ref/c/func/NSSelectorFromString

How to count and display user taps in the bar button item?

I am developing a project in which the user can add the object to favorite list that is table View. here I adding the object through rightBarButtonItem. I wanna show a message if User tap the right BarButton more than one. message is nothing but a UILabel that contains text like "Object already exist". please help me to solve this problem. thanks....
You might have a method that is executed when the button is tapped where you add the item to your table view. Lets say the method is called didClickButton. Have a Bool variable say isItemAdded indicating the status of the item. Set it to NO initially. when button is pressed check if its NO. If NO then proceed and set it to YES else display "Alert already added"
-(void)didClickButton
{
if(!isItemAdded)
{
//code to add to tableview
isItemAdded = YES;
}
else
{
//code to show message or alert
}
}
Initiaize a global counter and use it to count the taps by incrimienting it inside the method that is called by your rightBarButtonItem.
int tapsCtr = 0;
Put this code inside your method:
tapsCtr++;
if(tapsCtr > 1){
NSLog(#"User tapped more than once");
tapsCtr = 0;
}