First App addition


#1

I’m really new to iOS programming and just finished up the Objective-C book and loved it. I’ve had the second edition book and worked through most of it and didn’t understand some of the Objective-C stuff so took a step back and now I’m back at the iOS level to try it again with the third edition. I thought this first app was a great intro to cover a few different concepts but one thing really bugged me about it. When you load the app, you can press the “Show Answer” button and get an answer with no question displayed. So I added an if/ifelse/else statement to help this, and it was more than what the book intended for a first app but it just bugged me. Wanted to know what you guys thought and if there maybe a cleaner way to do this?

- (IBAction)showAnswer:(id)sender
{
    // First check the question field to see if an answer has been posted
    NSString *emptyQuestion = @"";
    NSString *errorMessage = [[NSString alloc] initWithFormat:@"Please press the Show Question button first"];
    
     // Look at the questionField to see if anything is in there
    if ([emptyQuestion isEqualToString:[questionField text]]) {
        
        // If there is nothing in the questionField then put in the error
        [questionField setText:errorMessage];
        [answerField setText:@"???"];
    } else if ([errorMessage isEqualToString:[questionField text]]) {
        
        // If the error is still in the questionField, keep it there
        [questionField setText:errorMessage];
        [answerField setText:@"???"];
    } else {
        
        // If there is anything other then an empty field or the error, continue as normal
        
        // What is the answer to the current question?
        NSString *answer = [answers objectAtIndex:currentQuestionIndex];
        
        // Display it in the answer field
        [answerField setText:answer];
        
    }
    
}

This way, if you push the “Show Answer” button at startup with an empty questionField the app it will tell you to press the “Show Question” button first and not display an answer. Again, more than the chapter intended but I came across the issue and it was bothering me.


#2

I like the idea and based on your comment went back and mod the code as shown below:
Basically I hide the answer button until the question button is pressed first.
Once the question button is pressed I unhide the button and set it to active.

[code]- (IBAction)showQuestions:(id)sender {

currentIndex++;


if (currentIndex == [questions count]) {
    currentIndex = 0;
    
}
  questionLable.text = [questions objectAtIndex:currentIndex];

NSLog(@"question index number is %d", currentIndex);
[showAnswerButton setHidden:NO];
[showAnswerButton setEnabled:YES];

}
[/code]

here is the View did load Method

[code]- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

if (self) {
    questions = [[NSMutableArray alloc] initWithObjects:
                 @"One", 
                 @"Two", 
                 @"Three",
                 nil];
    answers = [[NSMutableArray alloc] initWithObjects:@"1",@"2", @"3",nil];
    
}


NSString *emptyString = @"";

if (questionLable.text=emptyString) {
    
    NSLog(@"Nothing to see" );
    [showAnswerButton setEnabled:NO];
    [showAnswerButton setHidden:YES];
    
} 

}
[/code]

You need to create an iboutlet for the button so that the properties can be accessed.

[code]
#import <UIKit/UIKit.h>

@interface ViewController : UIViewController{

NSMutableArray *questions;
NSMutableArray *answers;

int currentIndex;

}
@property (strong, nonatomic) IBOutlet UIButton *showAnswerButton;

@property (strong, nonatomic) IBOutlet UILabel *questionLable;
@property (strong, nonatomic) IBOutlet UILabel *answersLable;

  • (IBAction)showQuestions:(id)sender;
  • (IBAction)showAnswers:(id)sender;
    @end[/code]

Thanks for the idea.


#3

I like that a bit better then just the “if/else” statements. I didn’t even think of hiding the button from the user. I just hated that when it first loaded you could hit that button and get an answer with no question.

Thanks!!

Jesse


#4

As far as your “if/else” statements go, a cleaner way to go about it is

- (IBAction)showAnswer:(id)sender
{
    if ([questionField.text isEqualToString:@""]) {
        // [logs] -[QuizViewController showAnswer:] no question asked yet
        NSLog(@"%s no question asked yet", __PRETTY_FUNCTION__);
        // Don't show an answer yet, so just return 
        return;
    }
    
    // Get the answer to the current question
    NSString *answer = [answers objectAtIndex:currentQuestionIndex];
    
    // Display the answer
    answerField.text = answer;
}