how to check that any of the button is clicked or not in iphone application - iphone

I have six button i want that user must select any of the six button before moving to next screen if it does not select any then it should not move to next screen I have read to use BOOL flag but this show which button is clicked i want that if any of the six buttons is clicked then user can move to next screen
-(IBAction)locationOneButtonAction{
// your stuff
_flag = YES;
}
-(IBAction)locationTwoButtonAction{
// your stuff
_flag = YES;
}

I think the best solution this problem is to declare a bool for your class (let's say it is called "ClickViewController" like (so at the top)
#interface ClickViewController () {
bool wasClicked;
}
...
-(IBAction)locationOneButtonAction{
// your stuff
wasClicked = YES;
}
Then when the button that should move to the next page is clicked utilize a method like this.
-(void)nextPageClicked{
if (wasClicked){
[self performSegueWithIdentifier:#"segueIdentifier" sender: self];
} else {
// do something else here, like tell the user why you didn't move them
}
}
This means that you cannot draw the segue from the button to the next view. If you are using storyboards, this means that you can't draw the segue from the button that is supposed to move it to the next view, rather you should draw it from the view controller icon to the next view. Then select the segue, name it, and use the method above with the name.
I'm also not sure if you mean that you want each of the buttons to move to the next view, if that is the case, you can use basically the same code, but just put the code [self performSegueWithIdentifier:#"segueIdentifier" sender: self]; line in action for the button.
Hopefully this helps.

Intially add BOOL _flag; int curIndex; in .h file.
Now add intially _flag = FALSE; in viewDidLoad method as it indicate no button clicked.
Make userInteractionEnabled for each button to be able to click and add tag to each button.
Now in action method of each click of button do this:
(IBAction)locationOneButtonAction:(id)sender{
curIndex = [sender tag]; //which button clicked
// your stuff
_flag = TRUE;
}
-(IBAction)locationTwoButtonAction{
curIndex = [sender tag]; //which button clicked
// your stuff
_flag = YES;
}
........
........
Now check like this if button clicked:
if(_flag)
{
NSLog(#"Button clicked with tag: %d",curIndex);
}
else
{
NSLog(#"Not any Button clicked");
}

Related

Add Power Button to Calculator App

I am trying to create a "Power Button" for my calculator app that will turn the calculator on/off. I created the button:
-(IBAction) *Power;
Without the power button, the app starts with "0" in the LCD display, and my buttons are all working; but how can I tell my app that the LCD should display "" and the buttons cannot be manipulated until the power button is pressed? Would a simple if-then function work? Can I put -(IBAction) buttons within if-thens?
Create a BOOL property, that and check for YES in all the IBACtions.
You need to set it to YES/NO on the method Power, typically as:
Initiaze it with NO in your init or viewDidLoad, whichever is applicable.
-(IBAction) power{
self.powerOn=!self.powerOn;
}
-(IBAction) otherMethod{
if(self.powerOn){
//do your stuff
}
}
Also, method with pointer -(IBAction) *Power; is not that you need here.
1) declare a variable to track power on/off
#property(nonatomic, assign) BOOL powerOn;
in viewDidLoad,
assign _powerOn = NO; or _powerOn = YES;
2) inside your power button pressed event,
-(IBAction) Power;{
//if on, then off
if(self.powerOn){
//make display ""
self.powerOn = NO;
}else{ //if off, the on
self.powerOn = YES;
//make display "0"
}
}
3) add following line as the 1st line in all other button click events
-(IBAction) otherMethod{
if(!self.powerOn) return;
//your code
}
You Need to create IBOutlets for your Buttons, to edit behaviors of them while running the app.
These IBOutlets are created like this:
IBOutlet UIButton *myPowerButton;
You need to link them with the button in Interface Builder where you created the Outlet for.
There you can specify the 'Enabled' behavior to be YES or NO, so you can make a Power Button to set if the user can work or not.
For further information please read Apples Documentation about IBOutlets and Buttons.
UIButton Apple Documentation
I would do this way, assuming the title of the button is Power:
-(IBAction) Power
{
if(self.powerOn)
{
//make display ""
self.powerOn = NO;
for (UIView* subView in self.view.subviews)
{
if ([subView isMemberOfClass:[UIButton class]] && subView.titleLabel.text != #"Power")
subView.userInteractionEnabled = NO;
}
}
else
{ //if off, the on
self.powerOn = YES;
//make display "0"
}
}
I would disable the User interaction of all other button when powerbutton is off.

EXEC_BAD_ACCESS on becomeFirstResponder call

I have created a keyboard with some arrow keys that allows the user to navigate with arrow buttons, between all the text fields in the tableview. (2 per cell)
There are some situations where cells will be disabled, so I have a recursive call to skip them. ie. if the user presses the right arrow, and the textField is disabled, recursively call the right arrow again to skip it.
I also animate any scrolling that needs to be done myself. However, something has happened recently that is causing a EXEC_BAD_ACCESS when trying to set the firstResponder after a user navigates to a new textField, and the scrolling animation is finished.
I am able to reproduce this issue starting in the first cell and navigating down 2 times. Here is the code, in order of what is happening as best as I can display it, with a lot edited (like all the code for the other 3 arrow buttons so u only see the lines called)
//receive the arrow button pressed first
_arrowButton = arrowButton;
#try
{
switch (_arrowButton.tag)
{
case NumericKeyboardViewDownArrow:
destinationIndexPath = [NSIndexPath indexPathForRow:currentIndexPath.row + 1 inSection:currentIndexPath.section];
newTag = currentTextField.tag;
break;
default:
break;
}
// check bounds of the intended index path and return if out of bounds
if ((destinationIndexPath.row == -1) || (destinationIndexPath.row >= [_tableView numberOfRowsInSection:[destinationIndexPath section]])) {
destinationIndexPath = currentIndexPath;
return;
}
/*
Animation for scrolling
*/
CGPoint current = [_tableView contentOffset];
// if we must animate up
if (destinationIndexPath.row < currentIndexPath.row)
{
// up code here
}
// if we must animate down
else if (destinationIndexPath.row > currentIndexPath.row)
{
// always scroll animate for down
[_tableView setContentOffset:CGPointMake(0, current.y + _tableView.rowHeight) animated:YES];
}
// if we only move left or right
else
{
// left and right code here
}
//update our current location
currentIndexPath = destinationIndexPath;
#catch (NSException *e)
{
return;
}
then after the animation is finished in the did finish animating method
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
//after the animation is done, set the responder
nextTextFieldSelection = (UITextField *)[[_tableView cellForRowAtIndexPath:currentIndexPath] viewWithTag:newTag];
NSLog(#"nexttextfield%#",nextTextFieldSelection);
if (nextTextFieldSelection)
{
//CRASH OCCURS HERE [nextTextFieldSelection becomeFirstResponder];
}
[self checkTextFieldIsDisabled];
}
By the end of that method I am in a new text field and we check if disabled. In reproducing this crash, it just exits because the textFields I am moving in to are not disabled.
Ive tried resigning the first responder before calling a new becomeFirstResponder but that doesn't seem to have any effect. Its possible I wasn't resigning in the proper location though. Any ideas where the memory issue would be?
Thanks

activate ccTouchesBegan with a button in cocos2d and make it work just one time

I want to make something happen with ccTouchesBegan but i want to restrict that action to a button, meaning that the action by the button should trigger ccTouchesBegan but only one time. After the code inside ccTouchesBegan has finished, the interface should go back to normal and wait for the button to be pressed to trigger the action again.
I have made the button trigger ccTouchesBegan when its pressed but the problem is that once the button is pressed, the code inside ccTouchesBegan keeps working and doing the same thing from then on when a touch action on the simulator is done.
This is the code that i have so far.
this is a flag method that i have created so that i know the button has been pressed and i can control the actions on ccTouchesBegan.
- (void) selector{
click = true;
}
this is the button which calls the method selector.
- (void) button{
CCMenuItemImage *touchesMovedButton = [CCMenuItemImage itemFromNormalImage:#"ActionButton-Normal.png"
selectedImage:#"ActionButton-Selected.png"
target:self
selector:#selector(selector)
];
CCMenu *selectorButton = [CCMenu menuWithItems: touchesMovedButton, nil];
selectorButton.position = ccp(64, 64);
[self addChild: selectorButton];
}
and this is the method ccTouchesBegan
- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
//this part doesn't let the interface start the ccTouchesBegan code until the button has been pressed.
if (click == false) {
return;
}
// the else code starts when the button has been pressed.
else{
CCLOG(#"you have touched the interface!!");
}
}
That's the code, the interface doesn't do any action until the button is pressed but after it's pressed it keeps printing the CCLOG each time i touch the interface. i just want it to do it once and then when i press the button again it should do it just one time again.
does anyone know how to do this? or maybe point out my mistake?
You simply have the logical mistake. Use the Code below in the ccTouchesBegan It would work perfectly as you need...
if (click == false)
{
return;
}
// the else code starts when the button has been pressed.
else if(click == true)
{
CCLOG(#"you have touched the interface!!");
click=false; // You need to make false if you want to make touch single time enabled
}

UiButton keep pressed

I have one little question, I have an uibutton I want that when i pressed the button a music start but when I pressed the same button again it stopped the music how can I give two actions to a button?? or maybe change the button when the music is playing !! Thanks
a boolean as an instance variable, when you press the button change it to yes if its no, and to no if its yes. assign a different action to the different situations.
You can also change the state of the button, the button that was pressed is what is in the sender argument of your IBAction.
- (IBAction)playPauseAction:(id)sender
{
if( _isPlaying )
{
[self pause];
_isPlaying = false;
[sender setTitle:#"Play"];
}
else
{
[self play];
_isPlaying = true;
[sender setTitle:#"Pause"];
}
}
You can use the setTag: function
Inside your target selector, just detect which tag is it and change the tag according to current state

hiding and displaying uitoolbar on same button click

I am trying to hide and display a UIView on BarButtonItem click. Priviously i also posted the question regarding same but didnt find any suitable answer. I created UIView manually in IB and just placed it in view so it must be shown as soon as view is loaded but i made it hidden in viewDidLoad method by writing
myvew.hidden = YES;
secondly, when i click BarButtonItem then i set
-(IBAction)mymethod
{
myview.hidden = NO;
}
so its diplaying view but when i again click on it it must hide.. how do i do it?
Put the following statement in your button action
myview.hidden = !myview.hidden ;
So your code must be like below.
-(IBAction)mymethod
{
myview.hidden = !myview.hidden ;
}
if (myview.hidden == YES)
{
myview.hidden = NO;
}
else
{
myview.hidden = YES;
}
Check if the view is already hidden and then show, and if not hidden then hide it.
You should do in this way
-(IBAction)mymethod
{
if( myview.hidden == NO ) myview.hidden = YES;
else myview.hidden = NO;
}