iOS第一款iPhone应用程序


创建第一个应用程序

现在我们将创建一个将在iOS模拟器上运行的简单单一视图应用程序(空白应用程序)。

步骤如下。

步骤1 - 打开Xcode并选择 Create a new Xcode project

Xcode欢迎页面

第2步 - 选择 单一视图应用程序

创建项目

步骤3 - 输入产品名称,即应用程序的名称,组织名称,然后输入公司标识符。

新项目创建选项

步骤4 - 确保 选中“ 使用自动引用计数” ,以便在超出范围时自动释放分配的资源。点击下一步。

步骤5 - 选择项目的目录,然后选择创建。

创建项目选择文件夹

第6步 - 您将看到如下屏幕 -

Xcode项目页面

在上面的屏幕中,您将能够选择支持的方向,构建和发布设置。有一个字段部署目标,我们要支持的设备版本,让我们选择4.3,这是现在允许的最小部署目标。目前,这些都不是必需的,让我们专注于运行应用程序。

步骤7 - 现在,在“运行”按钮附近的下拉菜单中选择iPhone模拟器,然后选择运行。

第8步 - 就是这样; 您已成功运行第一个应用程序。您将获得如下输出 -

现在让我们改变背景颜色,只是为了从界面构建器开始。选择ViewController.xib。选择右侧的背景选项,更改颜色并运行。

Interface Builder

在上面的项目中,默认情况下,部署目标已设置为iOS 6.0,并且将启用自动布局。为了确保我们的应用程序在iOS 4.3以上的设备上运行,我们已经在创建此应用程序的开始时修改了部署目标,但我们没有禁用自动布局。

要禁用自动布局,我们需要取消选中每个笔尖的文件检查器中的自动布局复选框,即xib文件。Xcode项目IDE的各个部分如下图所示(礼貌:Apple Xcode 4用户文档)。

Xcode 4工作区

如上所示,可以在检查器选择器栏中找到文件检查器,并且可以在那里取消选中自动布局。当您只想定位iOS 6设备时,可以使用自动布局。此外,如果将部署目标提升到iOS 6,您将能够使用许多新功能,例如存折。现在,让我们坚持使用iOS 4.3作为部署目标。

第一个iOS应用程序的代码

您将找到为您的应用程序生成的五个不同文件。它们列出如下 -

  • AppDelegate.h
  • AppDelegate.m
  • ViewController.h
  • ViewController.m
  • ViewController.xib

AppDelegate.h

// Header File that provides all UI related items.
#import <UIKit/UIKit.h>

// Forward declaration (Used when class will be defined /imported in future)
@class ViewController;  

// Interface for Appdelegate
@interface AppDelegate : UIResponder <UIApplicationDelegate>

// Property window
@property (strong, nonatomic) UIWindow *window;

// Property Viewcontroller

 @property (strong, nonatomic) ViewController *viewController;
//this marks end of interface
@end

代码中的重要项目 -

  • AppDelegate继承自处理iOS事件的UIResponder。

  • 实现UIApplicationDelegate的委托方法,该方法提供关键应用程序事件,如完成启动,即将终止等。

  • UIWindow对象,用于管理和协调iOS设备屏幕上的各种视图。它就像加载所有其他视图的基本视图。通常,应用程序只有一个窗口。

  • UIViewController处理屏幕流程。

AppDelegate.m

// Imports the class Appdelegate's interface
import "AppDelegate.h"

// Imports the viewcontroller to be loaded
#import "ViewController.h"

// Class definition starts here
@implementation AppDelegate


// Method to intimate us that the application launched successfully
- (BOOL)application:(UIApplication *)application
 didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
   self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

   // Override point for customization after application launch.
   self.viewController = [[ViewController alloc]
   initWithNibName:@"ViewController" bundle:nil];
   self.window.rootViewController = self.viewController;
   [self.window makeKeyAndVisible];
   return YES;
}

- (void)applicationWillResignActive:(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:. */
}

- (void)applicationWillTerminate:(UIApplication *)application {
   /* Called when the application is about to terminate. Save data if appropriate.
   See also applicationDidEnterBackground:. */
}
@end

代码中的重要项目 -

  • UIApplication委托在这里定义。上面定义的所有方法都是UI应用程序委托,并且不包含用户定义的方法。

  • UIWindow对象被分配用于保存已分配的应用程序。

  • UIViewController被分配为窗口的初始视图控制器。

  • 要使窗口可见,将调用makeKeyAndVisible方法。

ViewController.h

#import <UIKit/UIKit.h>

// Interface for class ViewController
@interface ViewController : UIViewController

@end

代码中的重要项目 -

  • ViewController类继承了UIViewController,它为iOS应用程序提供了基本的视图管理模型。

ViewController.m

#import "ViewController.h"

// Category, an extension of ViewController class
@interface ViewController ()

@end

@implementation ViewController  

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

- (void)didReceiveMemoryWarning {
   [super didReceiveMemoryWarning];
   // Dispose of any resources that can be recreated.
}
@end

代码中的重要项目 -

  • 这里实现的两个方法在基类UIViewController中定义。

  • 在视图加载后调用的viewDidLoad中进行初始设置。

  • 在内存警告的情况下调用didReceiveMemoryWarning方法。