iOS菜鸟成长笔记(1)

前言:阳光小强最近抽时间学习iOS开发,在学习过程中发现了很多有趣的东西也遇到了很多问题,为了在学习过程中能和大家交流,记录下学习的心得和学习成果,所以就有了这一个系列文章,希望这一系列文章能形成一个系统性的东西,让和我一样刚步入iOS开发的朋友少走弯路,用最少的时间获得最大的收益。既然是学习笔记,希望大家多提意见,如果你是iOS大牛多多拍砖。

说起iOS开发很多朋友就会望而却步,有一部分朋友可能是因为设备因素,有一部分朋友可能是因为编程语言是Objective-C的原因,因为这些迟迟没有拿起的iOS当你有一天进入它的世界,你就会发现其实是我们想多了,在Xcode上面开发iOS程序是非常方便快捷的,而且苹果开发者官网为我们提供了很多关于iOS开发的文档和文章,学习起来非常方便。

转载请说明出处:

一、着手开发IOS应用程序

(官网链接:https://developer.apple.com/library/ios/referencelibrary/GettingStarted/RoadMapiOSCh/index.html#//apple_ref/doc/uid/TP40012668)

正如上图一样,这篇文章(https://developer.apple.com/library/ios/referencelibrary/GettingStarted/RoadMapiOSCh/index.html#//apple_ref/doc/uid/TP40012668)介绍了iOS开发工具Xcode的基本使用和开发的基本步骤和过程,建议都能读一下。

iOS开发中使用的是MVC模型,这个对于做过java ee和Android的朋友来说已经很熟悉了,我们需要做的是把模型、视图、控制器和工程中的文件对应起来

下面我们先来新建一个iOS的项目

1、选择 Create a new Xcode project –> iOS –> Application –> Single View Application (选择单页面模板)

2、项目结构

如上图所示,我们先来看看 Supporting Files/main.m文件,这个大家一看就知道是整个应用的入口

#import <UIKit/UIKit.h>#import "AppDelegate.h"int main(int argc, char * argv[]) {@autoreleasepool {return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));}}可以看到当我们的iOS应用运行的时候首先会执行UIApplication的UIApplicationMain方法,这个方法首先会创建UIApplication实例(这个和Android中的Application类似,是一个单例模式,整个应用程序只有一个实例,所以它的生命周期和我们的应用生命周期一致),接下来会在这里循环管理和处理应用事件。同时也会创建一个UIApplicationDelegate类的实例。该类是UIApplication的代理类,在该类中处理UIApplication委托的各种事件响应。

下面我们打开工程中的AppDelegate.h和AppDelegate.m文件

#import <UIKit/UIKit.h>@interface AppDelegate : UIResponder <UIApplicationDelegate>@property (strong, nonatomic) UIWindow *window;@end可以看到改代理类持有一个UIWindow的实例(属性)#import "AppDelegate.h"@interface AppDelegate ()@end@implementation AppDelegate- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {// Override point for customization after application launch.return YES;}- (void)applicationWillResignActive:(UIApplication *)application {// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.}- (void)applicationDidEnterBackground:(UIApplication *)application {// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case 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 background to the inactive state; here you can undo many of the changes made on entering the background.}- (void)applicationDidBecomeActive:(UIApplication *)application {// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.}- (void)applicationWillTerminate:(UIApplication *)application {// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.}@end在实现中有6个方法,分别在应用程序的各个生命周期中调用。比如didFinishLaunchingWithOptions方法在应用程序加载完成后调用。

工程中的ViewController就是MVC中的控制器,视图就是screen.xib文件和storyboard中的界面。这里的ViewController继承自UIViewController相当于Android中的Activity,用来控制视图和模型的交互。

在你生活出现失意和疲惫时能给你一点儿力量和希冀,只愿你幸福快乐。

iOS菜鸟成长笔记(1)

相关文章:

你感兴趣的文章:

标签云: