Introduction: Sunshine Xiao Qiang recently took the time to learn iOS development, in the learning process found a lot of interesting things also encountered a lot of problems, in order to learn in the process can communicate with you, record the learning experience and learning results, so there is this series of articles, I hope this series of articles can form a systematic thing, Let the same as I just entered the development of iOS friends less detours, with the least amount of time to get the maximum benefit. Since it is the study notes, I hope that you have more comments, if you are the iOS Daniel lot to shoot bricks.
Speaking of iOS development many friends will be deterred, some friends may be because of device factors, some friends may be because the programming language is objective-c reason, because these slow to pick up the iOS when you have a day into its world, you will find actually we think more, Developing iOS programs on Xcode is quick and easy, and the Apple Developer website provides us with a lot of documentation and articles on iOS development that are very handy to learn.
Reprint please indicate source: Http://blog.csdn.net/dawanganban
First, start developing iOS apps
(Website link: https://developer.apple.com/library/ios/referencelibrary/GettingStarted/RoadMapiOSCh/index.html#//apple_ ref/doc/uid/tp40012668)
As in this article (https://developer.apple.com/library/ios/referencelibrary/GettingStarted/RoadMapiOSCh/index.html#// apple_ref/doc/uid/tp40012668) introduces basic steps and procedures for basic use and development of the iOS development tools Xcode, and it is recommended to read them.
The MVC model used in iOS development is already familiar to friends who have done Java EE and Android, and what we need to do is to map the models, views, controllers, and files in the project
Now let's start with a new iOS project.
1. Select Create a new Xcode project----IOS---Application single View application (select one page template)
2. Project Structure
As shown, let's take a look at the supporting FILES/MAIN.M file, which everyone knows is the portal to the application.
#import <UIKit/UIKit.h> #import "AppDelegate.h" int main (int argc, char * argv[]) { @autoreleasepool { Return Uiapplicationmain (argc, argv, Nil, Nsstringfromclass ([Appdelegate class]); }}
You can see that when our iOS app first executes the UIApplication Uiapplicationmain method, this method first creates the UIApplication instance (this is similar to the application in Android, is a singleton pattern, the entire application has only one instance, so its life cycle is consistent with our application lifecycle, and the next step is to manage and process application events here. An instance of the Uiapplicationdelegate class is also created. The class is a proxy class for uiapplication, which handles various event responses for UIApplication delegates in that class.
Below we open the AppDelegate.h and APPDELEGATE.M files in the project
#import <UIKit/UIKit.h> @interface appdelegate:uiresponder <UIApplicationDelegate> @property (Strong, nonatomic) UIWindow *window; @end
You can see that the change agent class holds an instance of UIWindow (attribute)
#import "AppDelegate.h" @interface appdelegate () @end @implementation appdelegate-(BOOL) Application: (UIApplication *) Application didfinishlaunchingwithoptions: (Nsdictionary *) launchoptions {//Override point for customization after app Lication launch. return YES;} -(void) Applicationwillresignactive: (uiapplication *) application {//Sent when the application was about to move from a Ctive to inactive state. This can occur for certain types of temporary interruptions (such as a incoming phone call or SMS message) or when the US Er quits the application and it begins the transition to the background state. Use the This method to the pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should with this method to pause the game.} -(void) Applicationdidenterbackground: (uiapplication *) application {//Use the method to release shared resources, SA ve user data, invalidate timers, and store enough application state information to restore your application To It is terminated later. If your application supports background execution, this method is called instead of Applicationwillterminate:when the User quits.} -(void) Applicationwillenterforeground: (uiapplication *) application {//called as part of the transition from the back Ground to the inactive state; Here's can undo many of the changes made on entering the background.} -(void) Applicationdidbecomeactive: (uiapplication *) application {//Restart any tasks this were paused (or not yet STA rted) While the application was inactive. If the application is previously in the background, optionally refresh the user interface.} -(void) Applicationwillterminate: (uiapplication *) application {//called when the application was about to terminate. S Ave data if appropriate. See also Applicationdidenterbackground:.} @end
There are 6 methods in the implementation that are called in each lifecycle of the application. For example, the Didfinishlaunchingwithoptions method is called after the application has completed loading.
The Viewcontroller in the project is the controller in MVC, and the view is the interface in the Screen.xib file and storyboard. The Viewcontroller inherited from Uiviewcontroller is equivalent to activity in Android, which controls the interaction of views and models.
The view controller is not part of the view layer, it is not an element in the interface, he manages the object of the view layer, and provides interactive behavior, and if there are multiple interfaces, we need to define a view controller for each interface.
Excerpt from the Official document: "You can also use a view controller to transform various types of content. Because the IOS app has a limited amount of space to display content, the view controller provides the infrastructure needed to remove the view from one view controller and replace it with a view from another view controller. By having the view controller files communicate with the views in the inline diagram, you can define how the application interacts. The method is to define the connection between the concatenation diagram and the source code file through Action and Outlet . ”
With a simple example below, we can see the use of action and outlet.
Second, drag and drop UI components
Open the Main.storyboard to drag and drop the component element.
It uses Uitextfield, UILabel, UIButton, and detailed configuration of these components I think it's really not necessary to spend too much time on a friend who has been a developer of Android.
Third, monitor the button event
ViewController.h (6th line of code)
#import <UIKit/UIKit.h> @interface viewcontroller:uiviewcontroller//Declare a method to listen button click ibaction = = void-(ibaction) Btnonclick; @end
VIEWCONTROLLER.M (第15-17 Line code)
#import "ViewController.h" @interface Viewcontroller () @end @implementation viewcontroller-(void) viewdidload { [ Super Viewdidload]; Do any additional setup after loading the view, typically from a nib.} #pragma mark Listen button click-(void) btnonclick{ NSLog (@ "button was clicked");} @end
Hold down the control key and drag the button to the right-(Ibacton) to establish the connection at Btnonclick.
Command+r run observation, click the button will find the console print out the log
2015-03-14 11:10:11.820 First iOS program [12,810:142,497] button was clicked on 2015-03-14 11:10:12.004 first iOS program [12,810:142,497] button was clicked by someone
Iv. Getting a text box object
We want to get the text object, we first want to use the text object as a property of Viewcontroller (9th, 10 lines)
#import <UIKit/UIKit.h> @interface viewcontroller:uiviewcontroller//Declare a method to listen button click ibaction = = void-(ibaction) btnonclick;//declaration Two attributes are used to hold 2 text input boxes @property (nonatomic, weak) Iboutlet Uitextfield *num1; @property (nonatomic, weak) Iboutlet Uitextfield *num2; @end
Use the same method (control-Drag our text object to establish a connection) so that we can get the contents of the text object
#import "ViewController.h" @interface Viewcontroller () @end @implementation viewcontroller-(void) viewdidload { [ Super Viewdidload]; Do any additional setup after loading the view, typically from a nib.} #pragma mark Monitor button click-(void) btnonclick{ NSLog (@ "button was clicked"); Uitextfield *textfield1 = self.num1; Uitextfield *textfield2 = self.num2; NSString *text1 = Textfield1.text; NSString *text2 = Textfield2.text; NSLog (@ "Text 1 =%@, text 2 =%@", Text1, Text2); } @end
Command + R, enter in the text box and click the button
Console output:
2015-03-14 11:33:07.342 First iOS program [14,531:166,271] button was clicked 2015-03-14 11:33:07.343 first iOS program [14,531:166,271] Text 1 = 12, text 2 = 34
Five, complete the calculation function
Add the Uilabel property in the same way
#pragma mark Monitor button click-(void) btnonclick{ NSLog (@ "button was clicked"); Uitextfield *textfield1 = self.num1; Uitextfield *textfield2 = self.num2; NSString *text1 = Textfield1.text; NSString *text2 = Textfield2.text; NSLog (@ "Text 1 =%@, text 2 =%@", Text1, Text2); Double NUM1 = [Text1 doublevalue]; Double num2 = [Text2 doublevalue]; Double sum = num1 + num2; NSLog (@ "Text and =%f", sum); UILabel *resultlabel = Self.result; Resultlabel.text = [NSString stringwithformat:@ "%f", sum]; }
We will find that the output is followed by three points, this is because I set the length beyond the ellipsis, you can set the line Breaks to Clip (truncate)
One last word: Objective-c is the cornerstone of iOS (CORE) so I would like to delve into the development of iOS friends to deepen the study of OC, you can refer to my other series of blog "Objective-c Basic Notes"
iOS rookie growth Note (1)--the first iOS app