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

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;
}

Related

How to pass UIAction without touching the UIButton

I've got a UIButton that acts as a switch.
When user taps on it, its state changes to "selected" and action "one" is called. When user taps again UIButton state changes to "not selected" and the action is no longer available.
Is there a way to set the UIButton to "selected" by taping on a completely different UIButton and have it change to "selected" and call the same action as well?
Cheers
From what I have understood from the question, I would like to give you my suggestion
if (!<your_button>.selected)
{
<your_button>.selected = YES;
// do your stuff here
}
else
{
<your_button>.selected = NO;
// do your stuff here, in your case action should be no longer available so that,
<your_button>.userInteractionEnabled = NO;
}
Enjoy Programming !
try like this,'
-(IBAction)nextbutton:(id)sender{
UIButton *btn=(UIButton *)[self.view viewWithTag:tagValue];//pass tag value of that particuler button
btn.selected=YES;
//do whatevr you want.
}
simple thing when you tap on the uibutton make an action for that and in the action for that button , you can set the button state to selected and then in the next line of code you can call that method (action)
- (IBAction)btn1Touched:(id)sender {
[_btn2 setSelected:YES];
[self btn2Touched:self];
}
- (IBAction)btn2Touched:(id)sender {
NSLog(#"btn Action called");
}
UIbutton has state called selected and it is managed via property selected
so to make a button to selected state
[buttonInstance setSelected:YES];
To make it unselected use
[buttonInstance setSelected:NO];
Kindly write a function for that button action like,
-(void)Buttonaction
{
do your action here
also set your button has selected state.
buttonname.setselected=yes;
}
call this function while you are tabbing your button.
Do This:
-(IBAction)switchButtonClicked:(UIButton*)sender
{
if(sender.isSelected)
{
.....
}
else
{
...
}
[sender setSelected:!sender.isSelected];//This will work as Switch.
}
set Selected(Switch ON) and default(Switch OFF) property (image and title) of UIButton from XIB, OR from Code as you want.
With a single button you can do you functionality with this code

how to check if any of the value is already hidden in iphone app

I have two button in iPhone app I want that if button1 is hidden and when we create PDF then we may check that button1 is already hidden then does not hide it again and also does not show it back when PDF generated because when PDF is created I hide some buttons.
you can write something like below...
if(button1.hidden){
//here button1 is hidden , so you can write code accordingly...
}else{
//here button1 is not hidden , so you can write code accordingly...
}
happy coding!!!!!
You can just use the .hidden property on the button
To hide the button, if not already hidden
if(!yourButton.hidden) //Button is not hidden
{
//hide button
yourButton.hidden = YES;
//do other magic here if required
}
To change the visibility of your button, you can use
yourButton.hidden = YES; //Set button hidden, (YES or NO, NO is by default)
you can check that UIButton is Hidden or not with its isHidden Property and also Hide that UIButton with its setHidden: method.
use this bellow logic with its method
if ([button1 isHidden]) {
//Generate your PDF here because if Button is hide then this condition true and load this code
}
else{
[button1 setHidden:YES];//we hide that button here and after load your code here
}

How to increment a label value when click a button

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

How can I determine which of two buttons is pressed by the user in the ABUnknownPersonViewController?

I have a button where if user press I am showing ABUnknownPersonViewController. Now the problem is there are two options Create New Contact & Add To Existing Contact. How can I determine which of the options is selected?
I need to know this because in my app, I am showing my contacts in another portion. Some times it causes my app to crash with the error:
Shouldn't be trying to show more than one Add to Existing Contact people picker
How can I solve this problem?
Assign tag value to the buttons and check the tag of the button clicked. Put condition according to the tag and show your views.
- (IBAction)contactsEdit_New:(id)sender
{
if ([addExitingBtn tag]==0)
{
// load ur view for exiting
}
else // here you can add if condition if required
{
// load view for creating new
}
}
Hope this hepls.
I am guessing you have 2 UIButtons?
Give each of them a tag and point them out to one method. If you're using IB just CTRL drag to your code (.m file).
Something like this:
- (IBAction)buttonPressed:(UIButton *)sender {
switch (sender.tag) {
case 0:
// load create controller
break;
case 1:
// load add controller
break;
default:
break;
}
}
You could also try and compare the text on the buttons titlelabel, though I don't really think that's good practice since the text on buttons can change during development.
- (IBAction)buttonPressed:(UIButton *)sender {
if([sender.titleLabel.text isEqualToString:#"create"]) {
// load create controller
} else if ([sender.titleLabel.text isEqualToString:#"add"]) {
// load add controller
}
}
You could also create 2 different methods for each button :)
This way you can avoid using tags since both buttons have their own method to answer to.
Good luck.

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.