Having more than one if statements in one IBAction in the .m file isn't working - iphone

I am making an iPhone app that will generate something based on quiz answers. I want the button for the answer to change the question text and sometimes the button label text.
Background Info:
'a' is a button
'questionNumber' is the question number (duh) and it is why the button will change the texts differently each time it is pressed.
Here is the code I am using:
-(IBAction) a
{
questionNumber == 0;
if(questionNumber == 0) {
question.text = #"How Much Do You Use Suppressed Weapons?";
}
questionNumber == 1;
if(questionNumber == 1) {
question.text = #"Do You Like Sleight of Hand?";
answerA.text = #"Yes";
answerB.text = #"No";
[answerC setHidden:YES];
[answerD setHidden:YES];
[answerButton3 setHidden:YES];
[answerButton4 setHidden:YES];
}
}
and this repeats for the other buttons (b,c, and d). I thought it should work, but it doesn't do the "do you like sleight of hand" question. It just stays at the how much do you use suppressed weapons question. Please Help! I really want to get into xcoding for the iphone.
BTW, I am not sure that the tags for this question are correct.
Related Question: How can I have an IBAction that has more than two 'if' statements?

You're not assigning to questionNumber. You're comparing.
questionNumber == 1;
That's a noop. It tests if questionNumber is 1, and then throws away the result. You want
questionNumber = 1;

Try using a switch statement with breaks.
switch (questionNumber)
{
case 0:
{
question.text = #"How Much Do You Use Suppressed Weapons?";
}
break;
case 1:
{
question.text = #"Do You Like Sleight of Hand?";
answerA.text = #"Yes";
answerB.text = #"No";
[answerC setHidden:YES];
[answerD setHidden:YES];
[answerButton3 setHidden:YES];
[answerButton4 setHidden:YES];
}
break;
}

Related

Pointer not stoping at nested if statement

Debugged program by adding breakpoint and found pointer is not getting at nested if statement
afraid that nested if if else statement logic is correct.
- (void)bookmarkcurrentIndex {
currentIndex = [modelArray indexOfObject:contentViewController.page];
if ([contentViewController.bookmarks containsIndex:currentIndex]) // Remove bookmark
if (BookmarkState == NO) {
[contentViewController.bookmarks removeIndex:currentIndex];
}
else {
[contentViewController.bookmarks addIndex:currentIndex];
}
}
}
EDIT:
- (IBAction)bookmarkAction:(id)sender {
if (_BookmarkState != _bookmarkbtn.tag)
{
[_bookmarkbtn setImage:[UIImage imageNamed:#"Bookmark-N.png"] forState:UIControlStateNormal];
_bookmarkbtn.tag = 0;
[self setBookmarkState:NO];
}
else
{
[_bookmarkbtn setImage:[UIImage imageNamed:#"Bookmark-Y.png"] forState:UIControlStateNormal];
_bookmarkbtn.tag = 1;
[self setBookmarkState:YES];
}
_bookmarkbtn.tag = _BookmarkState;
[self bookmarkcurrentIndex];
}
When I match your opening and closing braces, they don't match up. Your outside if statement seems to be going nowhere. Take a careful look and see if I'm correct. Did you copy and paste your code directly into the form here? If so, something is wrong with your code. It should look like this:
- (void)bookmarkcurrentIndex {
currentIndex = [modelArray indexOfObject:contentViewController.page];
if ([contentViewController.bookmarks containsIndex:currentIndex]) { // Remove bookmark
if (BookmarkState == NO) {
[contentViewController.bookmarks removeIndex:currentIndex];
}
else {
[contentViewController.bookmarks addIndex:currentIndex];
}
}
}
You'll see that I reformatted it, and corrected it. You're missing the opening brace after if ([contentViewController.bookmarks containsIndex:currentIndex]). I hope this is your problem.
If BookmarkState is a boolean property, you're missing self. (Should be self.BookmarkState == NO.)
Update:
In your updated example code, you're comparing a BOOL to an INT. (if (_BookmarkState != _bookmarkbtn.tag)) which needs to be addressed.
You also need to update your if condition to use _BookmarkState or self.BookmarkState.
Second update:
You're currently checking if the _BookmarkState != _bookmarkbtn.tag but this is comparing a BOOL (YES/NO) to an INT (0/1). You should not do this. Instead, use the BOOL to determine state:
if (self.BookmarkState) { // If it's YES.
[_bookmarkbtn setImage:[UIImage imageNamed:#"Bookmark-N.png"]
forState:UIControlStateNormal];
} else { // It's NO.
[_bookmarkbtn setImage:[UIImage imageNamed:#"Bookmark-Y.png"]
forState:UIControlStateNormal];
}
self.BookmartState = !self.BookmarkState;
That's the first problem.

Trying to toggle a view on/off using tags

I am trying to toggle a view on and off which works, but what i need to know is if I am abusing tags with this method. If so is there a better way?
- (IBAction) myButton:(UIButton*)sender {
if ([myLabelText.text isEqualToString:#""])
{
// do nothing
} else {
if ( sender.tag )
{
sender.tag = 0;
[[self firstView] setHidden:YES];
} else {
sender.tag = 1;
firstView.alpha = 100;
[[self firstView] setHidden:NO];
}
}
}
You can use the tag property for anything you like, but in this case you can toggle the visibility without using it:
[self firstView].hidden = ![self firstView].hidden;
Also, the valid range for UIView's alpha property is from 0.0 to 1.0. I'm not sure what you're trying to do there.

i have a problem in UIImageView's animating

-(IBAction) change {
self.imageView.animationImages = myImages;
self.imageView.animationDuration = 2;
if(self.imageView.isAnimating == NO){
[self.imageView startAnimating];
NSLog(#"if bool = %d", self.imageView.isAnimating);
}
else {
self.imageView stopAnimating];
NSLog(#"else bool = %d", self.imageView.isAnimating);
}
}
hello, i'm studying iOS programming.
but i have a question.
i have a button and when i click the button, then this method will be called.
first i click the button, then this code will start the if statement. that's what i want.
i click the button again, i think that will execute the else statement.
but it always execute the if statement only.
why is that?
i really don't know why is that. please help me
I think setting the properties like animationImages or animationDuration will stop the animation, so that by clicking, you every time stop and then just after (re)start it in the if part. Try setting these two properties outside the action method you wrote, and just let the if/else sequence.
-(IBAction) change {
// set these two anywhere else
//self.imageView.animationImages = myImages;
//self.imageView.animationDuration = 2;
if(self.imageView.isAnimating == NO){
[self.imageView startAnimating];
NSLog(#"if bool = %d", self.imageView.isAnimating);
}
else {
self.imageView stopAnimating];
NSLog(#"else bool = %d", self.imageView.isAnimating);
}
}

switch.hidden = YES not working, outlets all correctly set up

I am having a problem that is totally confounding me. Please look at the code below, it is from the book "Beginning iPhone 4 Development" chapter 4. I'm new to this :)
- (IBAction)toggleControls:(id)sender
{
if([sender selectedSegmentIndex] == kSwitchesSegmentIndex)
{
NSLog(#"Show switches");
[self.leftSwitch setHidden:NO];
[self.rightSwitch setHidden:NO];
[self.doSomethingButton setHidden:YES];
}
else
{
NSLog(#"Hide switches");
[self.leftSwitch setHidden:YES];
[self.rightSwitch setHidden:YES];
[self.doSomethingButton setHidden:NO];
}
}
The strange thing is that it logs this correctly but the ui controls aren't hiding/showing.
I also tried this (original in book):
- (IBAction)toggleControls:(id)sender
{
if([sender selectedSegmentIndex] == kSwitchesSegmentIndex)
{
NSLog(#"Show switches");
leftSwitch.hidden = NO;
rightSwitch.hidden = NO;
doSomethingButton.hidden = YES;
}
else
{
NSLog(#"Hide switches");
leftSwitch.hidden = YES;
rightSwitch.hidden = YES;
doSomethingButton.hidden = NO;
}
}
It sounds like you may have forgotten to wire up your outlets in Interface Builder. Check the values of leftSwitch and rightSwitch when this method is called by using a break point or an NSLog.

Objective C - Help with UIButton showing variable Titles

First off I am a rookie so any help is appreciated. I have written the following code to change the title of the button every time it is initiated. When I test the code, I can see the new button label for a fraction of a second and then the button is blank again (as it had started off). I only see the first three touches, so I am thinking that there is also something wrong with my counting method. The code is as follows:
-(IBAction)pressButton:(id)sender {
static int counter = 0;
if (counter == 0) {
[[sender titleLabel] setText:#"not answered"];
}else if (counter == 1) {
[[sender titleLabel] setText:#"Pressed Once"];
}else if (counter == 2) {
[[sender titleLabel] setText:#"Pressed Twice"];
}
counter += 1;
if (counter >2) {
counter = 0;
}
}
Thank you in advance for your help!
You want to use:
[(UIButton *)sender setTitle:#"XXX" forState:UIControlStateNormal];
Setting the label directly isn't going to work because it's manipulated internally by the button logic.