iPhone Quiz App Tutorial


 

iPhone Quiz App Tutorial

iPhone Quiz App Tutorial, In this simple iPhone quiz application tutorial, we are going to learn how to create simple quiz in iphone.

iPhone Quiz App Tutorial, In this simple iPhone quiz application tutorial, we are going to learn how to create simple quiz in iphone.

iPhone Quiz App Tutorial

In this simple iPhone quiz application tutorial, we are going to learn how to create simple quiz in iphone.

Creating a simple quiz in iPhone is really easy..Just follow the given steps to create the quiz application.

Steps to Create a Simple Quiz Application in iPhone

Step 1
Create a new project "ViewBased application" in Xcode and name the application "iphone-quiz-app". Once you create it .. just click on the NIB Files and open iphone_quiz_appViewController.xib

In this ".xib" file we are going to take few Buttons and Labels, which is required to ask question with few options. See the given image and name your labels and buttons as given in it.

Step 2

Now, we are going to create Class Actions and Class Outlets for the buttons and labels. To do the same..open Inspector form the tool bar of the Interface builder and choose "Main View identity" as given in the image. Name the class as "MainView" and add button actions in "Action" class and Outlet names in class "Outlet". Remember that Action type should be "id" and outlet type should be UILable"

As you know the "Action" that we are going to create is a button action. You can find the code of button action in ".h" file. like....

- (IBAction)a;

And the Outlet that you will create will be like...

IBOutlet UILabel *Question;

Now choose the "Main VIew Connections" in "Inspector" and switch the actions and outlets to it's appropriate buttons and labels as given in the image.

Now, to save the application just click on the File->Save and then again File->WriteClass File->save, then check the given option in the pop up file and then save.
Finally this save all the changes made into the .xib file.

After creating our view in interface builder ...now we need to make some changes in to the "MainView" .h and .m file.
Main view is the name that we have given to our file while saving into the interface builder.

In Xcode cut and paste both the "MainView" .h and .m file in the classes folder.

Open the MainView.h file and make the given changes. It should look like the given code..

#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>

@interface MainView : UIView {
    IBOutlet UILabel *Question;
    IBOutlet UILabel *answerA;
    IBOutlet UILabel *answerB;
    IBOutlet UILabel *answerC;
    IBOutlet UILabel *answerD;
}
@property (nonatomic, retain) IBOutlet UILabel *Question;
@property (nonatomic, retain) IBOutlet UILabel *answerA;
@property (nonatomic, retain) IBOutlet UILabel *answerB;
@property (nonatomic, retain) IBOutlet UILabel *answerC;
@property (nonatomic, retain) IBOutlet UILabel *answerD;

- (IBAction)a;
- (IBAction)b;
- (IBAction)c;
- (IBAction)d;
- (IBAction)next;
@end

And MainView.m

#import "MainView.h"

@implementation MainView
@synthesize Question, answerA, answerB, answerD, answerC;

- (IBAction)a {
   
    Question.text = @"Wrong Answer. Try Again!";
    Question.textColor = [UIColor redColor];
}

- (IBAction)b {
   
    Question.text = @"Wrong Answer. Try Again!";
    Question.textColor = [UIColor redColor];
}

- (IBAction)c {
    Question.text = @"Correct";
    Question.textColor = [UIColor orangeColor];
   
}

- (IBAction)d {
    Question.text = @"Wrong Answer. Try Again!";
    Question.textColor = [UIColor redColor];
   
}

- (IBAction)next {
  
}

@end

On running the application you will see the following output...

If you will click on the buttons it'll show if the selected answer is correct or wrong. As in the given image...

Here, "Question.text" is used to set the desired text on to the label and " Question.textColor " will change the color of text.
You can also show the next question to the user if he is choosing the correct answer. To do this we'll use the if(..) statement. As in the given code....

if(Question.text == @"Correct")
    {
               
        Question.text =@"The longest river in the world is...";
        Question.textColor = [UIColor blackColor];
     
        answerA.text=@"Ganges";
        answerB.text=@"Mississippi";
        answerC.text=@"Nile";
        answerD.text=@"Amazon";
    }

You can copy and paste the above given code into the next button action...
- (IBAction)next{
}

No clicking the Next button it will take the user to next question, if the given condition will be true. As given in the image...



Download Code of "iPhone Quiz Application Tutorial"


Ads