📜  iOS-第一个iPhone应用程序

📅  最后修改于: 2020-12-08 06:17:44             🧑  作者: Mango


创建第一个应用

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

步骤如下。

步骤1-打开Xcode,然后选择Create a new Xcode project

Xcode欢迎页面

步骤2-选择“单一视图应用程序”

建立专案

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

新项目创建选项

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

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

创建项目选择文件夹

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

Xcode项目页面

在上面的屏幕中,您将能够选择支持的方向,构建和发布设置。有一个现场部署目标,即我们要支持的设备版本,请选择4.3,这是现在允许的最低部署目标。现在,这些不是必需的,让我们集中精力运行应用程序。

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

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

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

界面生成器

在上述项目中,默认情况下,部署目标将设置为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  

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

// Interface for Appdelegate
@interface AppDelegate : UIResponder 

// 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  

// 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方法。