iOS Hello World示例教程

时间:2020-02-23 14:45:54  来源:igfitidea点击:

在本教程中,我们将开发第一个iPhone应用程序,该应用程序将在屏幕上显示Hello World标签。
我们将创建一个单视图应用程序,该应用程序将在屏幕上显示一个字符串。

入门

首先从启动板启动XCode(您需要从Mac App Store下载)。
在欢迎屏幕上单击创建一个新的XCode项目。
将出现以下屏幕:

单击下一步将带您到另一个屏幕,以填写项目的所有必需选项。

由于我们未在此应用程序中使用单元测试和核心数据,因此请勿选中它们。

单击"下一步",XCode要求您保存Hello World!项目。
选择您Mac上的任何文件夹。
单击确认后,下一个屏幕如下所示:

在上面的屏幕中,您将能够选择支持的方向,构建和发布设置。
在左侧窗格中,它是项目导航器。
您可以在此区域下找到所有项目文件。

AppDelegate.h头文件如下所示。
AppDelegate.h

#import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@end

下面列出了从上述摘录中得出的一些重要推论。

  • 头文件UIKit提供了所有与UI相关的项目
  • AppDelegate继承自处理所有iOS事件的UIResponder
  • 实现UIApplicationDelegate的委托方法,该方法提供关键的应用程序事件,例如启动完成,即将终止等
  • UIWindow对象用于管理和协调iOS设备屏幕上的各种视图。
    就像加载所有其他视图的基本视图一样。
    通常,一个应用程序只有一个窗口
  • UIViewController处理屏幕流

AppDelegate.m

#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

UIApplication委托在此处定义。
上面定义的所有方法都是UI应用程序委托,并且不包含用户定义的方法。
可以根据我们的需要重写这些方法。

ViewController.h类继承了UIViewController,后者为iOS应用程序提供了基本的视图管理模型。
我们将声明标签变量,并定义在此头文件中单击按钮时调用的方法,如下所示。

ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController{
IBOutlet UILabel *label;
}
-(IBAction)showLabel;

@end

注意:IBAction和IBOutlet是定义为表示可以在Interface Builder中引用的方法和变量的宏。

ViewController.m包含基本方法。

  • viewDidLoad
  • didReceiveMemoryWarning

同时,我们实现了自己在ViewController.h文件中声明的实例方法。
下面给出了" ViewController.m"文件。

ViewController.m

#import "ViewController.h"

@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.
  }
-(IBAction)showLabel{
  label.text = @"Hello World";
}

@end

在上面的代码中,我们实现了Button Action,以便在单击标签文本时更改标签文本。

将UI链接到代码

现在,我们将转到我们的用户界面。
我们需要为用户提供一种与我们刚编写的所有出色代码进行交互的方式。
点击" Main.storyboard",您将看到类似于下面给出的图像。

标签和按钮将在屏幕右下窗格中的"对象库"中提供。
将它们拖放到视图上,然后通过视图右窗格中的属性检查器修改标签/按钮的文本,如下所示。

从上面可以看到,按钮文本不会显示全文。
要将宽度换为文本,请同时按Cmd和+。
这样的事情将会出现。

选择按钮并按住控件并将其拖动到顶部的金色按钮("视图控制器"),然后选择showLabel方法。
这将使用视图中的当前按钮注册该方法。

要将标签与代码链接,请在连接检查器上单击,然后按Control键并将其拖动到标签文本上。
选择标签变量(或者您在头文件中定义的等效名称)。

注意:按钮也可以通过这种方式链接。

因此,我们现在准备构建我们的第一个iOS应用程序。
选择模拟器的类型,然后从左上方的工具列构建项目,如下所示。

该应用程序的输出如下。

哎呀!我们看到视图不在屏幕上。
原因是我们在文件检查器中启用了"使用大小类",如下所示。

禁用它会显示正确的屏幕布局,如下所示。

将标签中心垂直对齐,将按钮对齐为水平中心和垂直对齐。
可以从属性检查器中的"对齐"选项更改标签的对齐方式。