[First time in iOS] The first simple iOS app, first time in ios

Source: Internet
Author: User
Tags home screen

[First time in iOS] The first simple iOS app, first time in ios

This example is from iOS programming (version 4th) and describes how to compile a simple iOS application.

Function: displays a question in the view. You can click the button below the view to display the corresponding answer. You can click the button above to display a new question.

 

The procedure is as follows:

1. Create a New Xcode project Hello_iOS. For details, refer:

 

 

2. Create a View Controller file QAViewController. Note the following:

 

3. Select the QAViewController. xib file and drag four labels and two buttons from the object library to display them.

 

4. Create an association for the object. First, modify the QAViewController. m file as follows:

# Import "QAViewController. h "@ interface QAViewController () // declare the socket variable @ property (nonatomic, weak) IBOutlet UILabel * questionLabel; @ property (nonatomic, weak) IBOutlet UILabel * answerLabel; @ end @ implementation QAViewController @ end

5. Set the declared socket variables, as shown in:

 

6. re-open the QAViewController. m file and declare the actions of the two buttons:

@implementation QAViewController-(IBAction)showQuestion:(id)sender{    }-(IBAction)showAnswer:(id)sender{    }@end

 

7. Associate the declared action method and set the Target and Action (Press Control and drag or right-click to drag to File's Owner ):

 

8. After completing the XIB file of the application, you have created and set the view object required by the application, and created all required management for the view object and controller object. Create a model object. On the project navigation panel, select the QAViewController. m file and modify the Code as follows:

//// QAViewController. m // Hello_iOS /// Created by YeChao on 15/12/5. // Copyright (c) 2015 Luka. ye. all rights reserved. // # import "QAViewController. h "@ interface QAViewController () // declare the socket variable @ property (nonatomic, weak) IBOutlet UILabel * questionLabel; @ property (nonatomic, weak) IBOutlet UILabel * answerLabel; // declare an integer object and two array objects. // The integer variable is used to trace the question that the user is answering @ property (nonatomic) int currentQuestionIndex. // two arrays are used to store a series Question and Answer @ property (nonatomic, copy) NSArray * questions; @ property (nonatomic, copy) NSArray * answers; @ end @ implementation QAViewController-(instancetype) initWithNibName :( NSString *) nibNameOrNil bundle :( NSBundle *) nibBundleOrNil {// call the initialization method of the parent class implementation self = [super initWithNibName: nibNameOrNil bundle: nibBundleOrNil]; if (self) {// create two array objects to store the required questions and answers. // At the same time, point questions and answers to the question array and answer array self respectively. questions = @ [@ "you What is the name? ", @" Do you think you are handsome? ", @" What year is this year? ", @" Why do we rely on talent to clearly depend on our faces? "]; Self. answers = @ [@" can be called Ye Xiaochao. ", @" OK. ", @" 2015 AD ", @" this is a clear gap. Please try your best. "];} // Return the address of the new object return self;}-(IBAction) showQuestion :( id) sender {// enter the next question self. currentQuestionIndex ++; // Have you answered all the questions? If (self. currentQuestionIndex = [self. questions count]) {// return to the first question self. currentQuestionIndex = 0;} // retrieves the question string NSString * question = self from the Array Based on the question number being answered. questions [self. currentQuestionIndex]; // display the problem string on the tag self. questionLabel. text = question; // reset the answer string self. answerLabel. text = @ "";}-(IBAction) showAnswer :( id) sender {// What is the answer to the current question? NSString * answer = self. answers [self. currentQuestionIndex]; // display the corresponding answer self. answerLabel. text = answer;} @ end

9. If you are running the project now, you can only see a blank screen, and you cannot see the user interface created in the QAViewController. xib file. To display the user interface on the screen, you must associate the View Controller with another controller in the application-AppDelegate.

In the project navigation panel, select the AppDelegate. m file. Create a QAViewController object in application didfinishlaunchingwitexceptions: method and set it to the Root View Controller of the UIWindow object.

# Import "AppDelegate. h "# import" QAViewController. h "-(BOOL) application :( UIApplication *) application didfinishlaunchingwitexceptions :( NSDictionary *) launchOptions {self. window = [[UIWindow alloc] initWithFrame: [UIScreen mainScreen] bounds]; // Add the initialization code QAViewController * qaVC = [[QAViewController alloc] init]; self. window. rootViewController = qaVC; self. window. backgroundColor = [UIColor whiteColor]; [self. window makeKeyAndVisible]; return YES ;}

10. Press command + R to run the project. The effect is as follows:

11. If no application icon is set, it will be a whiteboard. An application icon is an image used to represent an application on the home screen. Different devices have different icon sizes.

 

12. Set the application icon: select the Images. xcassets entry in the project navigation Panel, for example, drag from the Finder to the setting block in the AppIcon area.

13. Run the project again.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.