Why UIButton showing selected state when turning page over - iphone

I would like to change the state of a UIButton, only when it appears on a certain page (with currentIndex). But when navigating to the next page, my UIButton shows the same selectedState rather than changing.
Here is my implemented code:
- (IBAction)bookmarkState:(id)sender {
currentIndex = [modelArray indexOfObject:contentViewController.page];
if (_BookmarkState != _bookmarkbtn.tag) {
[_bookmarkbtn setImage:[UIImage imageNamed:#"Bookmark-N.png"]
forState:UIControlStateNormal];
[self setBookmarkState:NO];
[contentViewController.bookmarks removeIndex:currentIndex];
}
else {
[_bookmarkbtn setImage:[UIImage imageNamed:#"Bookmark-Y.png"]
forState:UIControlStateNormal];
[self setBookmarkState:YES];
[contentViewController.bookmarks addIndex:currentIndex];
}
}
How can i fix this.
Thanks for help.

Here is what is wrong with your code:
You define currentIndex but neither read again it nor change the array where it comes from.
You are changing an array with bookmarks, but you never read it.
You compare the tag of an object called _bookmarkbtn, presumably a button, with another object that looks like some weird kind of class (initial capital letter) but is probably a constant.
Therefore, you are not using currentIndex, and not comparing anything that refers to the sender of the button action.
Maybe you intended to compare the right element of your bookmarks array to the tag of the button that was pressed? Your code does not do this.
Maybe a straight-forward way is to simply set the correct the image of the button whenever the page appears and just toggle it whenever the button is pressed.

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

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

Linking multiple buttons to one method with different jobs

I have a huge crazy scene in my story board that has 36 different buttons, and each one means something different when clicked on. I really don't want to go about creating 36 different methods, so how could I reference a button title or button name in a method that is called when one of the 36 buttons is pushed.
This is probably a simple question, but I'm new to iOS and Objective C...
Thanks!
You can create a single method, like so:
- (IBAction)buttonTapped:(id)sender{
// The button that was tapped is called "sender"
// This will log out the title of the button
//NSLog(#"Button: %#", sender.titleLabel.text);
//Edit: You need a cast in the above line of code:
NSLog(#"Button: %#", ((UIButton *)sender).titleLabel.text);
}
Then, you can use Interface Builder to connect to all of the buttons. You can have some sort of if/else logic to test which button was tapped.
You can check the titleLabel property, or you can assign an IBOutlet to each button and check for that.
For example:
if([sender isEqual:buttonOutlet1]){
//If this button is attached to buttonOutlet1
//do something
}
Alternatively, you can simply use the label of each button, not worrying about outlets.
A third option would be to generate and lay out the buttons in code, and then access them as elements of an array of buttons.
A fourth option would be to add tags to the buttons and check for the button's tag in your function.
Give each button a unique tag value. in the IBAction, sender.tag tells you which button was tapped.
The IBAction routine you set up to handle the button presses has a sender parameter. Examine that to decide.
-(IBAction) buttonPress: (id) sender {
UIButton *pressedButton = (UIButton *)sender;
NSString *buttonTitle = [pressedButton currentTitle];
if ([buttonTitle isEqualToString: #"SomeTitle"]) {
//do work for that button.
}
}
You can use a variety of NSString methods to compare or filter which button was pressed and handle it through if's or switches.
That's quite simple, but since you're new, here's an answer.
(According to Stanford cs193p course, 2010-2011 fall (that's what they did with the calculator app)) make a method that receives an argument, which is the UIButton.
for example:
- (IBAction) someMethodThatDoesSomething:(UIButton *)sender;
Then make if statements according to the sender.titleLabel.text
I don't know if there are any other solutions. Hope this helps!
-(IBAction)myButtonAction:(id)sender {
if ([sender tag] == 0) {
// do something here
}
if ([sender tag] == 1) {
// Do some think here
}
}
// in Other words
-(IBAction)myButtonAction:(id)sender {
NSLog(#"Button Tag is : %i",[sender tag]);
switch ([sender tag]) {
case 0:
// Do some think here
break;
case 1:
// Do some think here
break;
default:
NSLog(#"Default Message here");
break;
}

Objective C Syntax: addTarget on UISwipeGestureRecognizer

I'm currently working on an app where the user has the option to either swipe through data, or use a button to go through the data. I'm having trouble understanding how to combine two bits of code.
Here is the code I'm using for swiping:
- (void)swipeRight:(UISwipeGestureRecognizer*)recognizer
{
if ([questions hasPrevQuestion] == YES) {
[self vorige:nil];
}
}
(the [self vorige:nil]; is calling the method for the button, so the swiping and the button have the same behavior)
and I need to somehow incorporate this code which applies to the button:
-(void)animationDidEndOnAnswer {
[vorigeButton addTarget:self action:#selector(newQuestion:) forControlEvents:UIControlEventTouchUpInside];
}
I think it's pretty simple, but I just cannot for the life of me figure out how to call the swiping method place of the button here...I'm thinking it's simple because I found this example in the UIGestureRecognizer class reference:
- (void)addTarget:(id)target action:(SEL)action
but being new to objective-c, I don't really know what to do with this. any help is very appreciated.
- (void)addTarget:(id)target action:(SEL)action is the code equivalent of binding a button action to a method like you do when you ctrl-drag from a button to an IBAction in Interface Builder.
So if your method is called vorige: and you want it to be called when the button is tapped, you would say:
[vorigeButton addTarget:self action:#selector(vorige:) forControlEvents:UIControlEventTouchUpInside];
But I don't know why you would want to do that as a result of an animation - you normally would set the button action once when the view is loaded, not change it during an animation.
Nick's solution is good.
if you want to call the same method for the swiping & the tap on your button, you can tweak your swipeRight like this:
- (void)goThrougtData:(id)sender
{
if( [sender isKindOfClass:[UISwipeGestureRecognizer class]] ) {
// swipe specific code
}
else if( [sender isKindOfClass:[UIButton class]] ) {
// tap specific code
}
if ([questions hasPrevQuestion] == YES) {
[self vorige:nil];
}
}
and in your init method, you add
[mySwipeRecognizer addTarget:self action:#selector(vorige:)];
[vorigeButton addTarget:self action:#selector(vorige:) forControlEvents:UIControlEventTouchUpInside];

UIButton need clicks two times for firing its action

I have two round rect buttons. I have implemented its touch up inside action. It works fine but when my click second button just after clicking the first button, my first click has no effect. This is happening with both the buttons. I am actually trying to change background image of buttons on each click to get the effect of check box (checking/unchecking). Below is the code for for one of the buttons:
-(IBAction) checkButton2: (id) sender
{
self.checkButton2 = sender;
if ( isCheck == NO)
{
[self.checkButton2 setImage: [UIImage imageNamed:#"check.png"] forState:UIControlStateNormal];
isCheck = YES;
}
else
{
[self.checkButton2 setImage:[UIImage imageNamed:#"uncheck.png"] forState:UIControlStateNormal];
isCheck = NO;;
}
}
This is solved. Actually I was using isCheck BOOL type variable for two actions(One is given above) for two buttons and both actions were setting this variable separately which shows deviation from expected behavior.