ios8升级ios12教程
In this tutorial we’ll develop our first iPhone Application which displays a Hello World Label on the screen. We’re going to create a single view application that will display a string on screen.
在本教程中,我们将开发第一个iPhone应用程序,该应用程序在屏幕上显示Hello World标签。 我们将创建一个单视图应用程序,该应用程序将在屏幕上显示一个字符串。
入门 (Getting Started)
First launch XCode (you need to download it from the Mac App Store) from your launchpad. Click on create a new XCode project from the welcome screen. The following screen would appear:
首先从启动板启动XCode(需要从Mac App Store下载)。 在欢迎屏幕上单击创建一个新的XCode项目。 将出现以下屏幕:
Clicking next brings you to another screen to fill in all the necessary options for your project.
单击下一步将带您到另一个屏幕,以填写项目的所有必需选项。
Leave the unit tests and Core Data as unchecked since we’re not using them in this application.
由于未在此应用程序中使用单元测试和核心数据,因此请不要选中它们。
Clicking Next, XCode asks you to save the Hello World! project. Pick any folder on your mac. On clicking confirm the next screen looks like this :
单击“下一步”,XCode要求您保存Hello World! 项目。 选择您Mac上的任何文件夹。 单击确认后,下一个屏幕如下所示:
In the above screen, you will be able to select the supported orientations, build and release settings. On the left pane, it’s the project navigator. You can find all your project files under this area.
在上面的屏幕中,您将能够选择支持的方向,构建和发布设置。 在左窗格中,它是项目导航器。 您可以在此区域下找到所有项目文件。
The AppDelegate.h
header file is shown below.
AppDelegate.h
AppDelegate.h
头文件如下所示。
AppDelegate.h
#import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@end
Some important inferences drawn from the above snippet are listed below.
下面列出了从上述摘录中得出的一些重要推论。
- The header file UIKit provides all the UI related items 头文件UIKit提供了所有与UI相关的项目
- AppDelegate inherits from UIResponder that handles all the iOS events AppDelegate继承自处理所有iOS事件的UIResponder
- Implements the delegate methods of UIApplicationDelegate, which provides key application events like finished launching, about to terminate and so on 实现UIApplicationDelegate的委托方法,该方法提供关键的应用程序事件,例如启动完成,即将终止等
- UIWindow object to manage and co-ordinate the various views on the iOS device screen. It’s like the base view over which all other views are loaded. Generally there is only one window for an application UIWindow对象用于管理和协调iOS设备屏幕上的各种视图。 就像在其上加载所有其他视图的基本视图一样。 通常,一个应用程序只有一个窗口
- UIViewController to handle the screen flow UIViewController处理屏幕流
AppDelegate.m
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 delegates are defined here. All the methods defined above are UI application delegates and contains no user defined methods. These methods can be overridden according to our needs.
UIApplication委托在此处定义。 上面定义的所有方法都是UI应用程序委托,并且不包含用户定义的方法。 这些方法可以根据我们的需要被覆盖。
The ViewController.h
class inherits the UIViewController, which provides the fundamental view management model for the iOS applications. We’ll declare the label variable and define the method that’s invoked on button click in this header file as shown below.
ViewController.h
类继承了UIViewController,后者为iOS应用程序提供了基本的视图管理模型。 我们将声明label变量并定义在此头文件中单击按钮时调用的方法,如下所示。
ViewController.h
ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController{
IBOutlet UILabel *label;
}
-(IBAction)showLabel;
@end
Note : IBAction and IBOutlet are macros defined to denote methods and variables that can be referred to in Interface Builder.
注意 : IBAction和IBOutlet是定义为表示可以在Interface Builder中引用的方法和变量的宏。
The ViewController.m
contains the basic methods.
ViewController.m
包含基本方法。
- viewDidLoad viewDidLoad
- didReceiveMemoryWarning didReceiveMemoryWarning
Along with that we implement our own instance methods that we’d declared in the
ViewController.h
file. The ViewController.m
file is given below.
与此同时,我们实现了自己的实例方法,该方法在
ViewController.h
文件。 下面给出了ViewController.m
文件。
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
In the above code we’ve implemented the Button Action such that the label text is changed when it’s clicked.
在上面的代码中,我们实现了Button Action,以便在单击标签文本时更改标签文本。
将UI链接到代码 (Linking the UI to the Code)
Now, we’ll move on to our user interface. We need to give our users a way to interact with all of the awesome code we just wrote. Click on Main.storyboard
and you’ll see something similar to the image given below.
现在,我们将转到用户界面。 我们需要为用户提供一种与我们刚编写的所有出色代码进行交互的方式。 单击Main.storyboard
,您将看到与下面给出的图像相似的图像。
Label and buttons will be available in Object Library that’s present in the bottom right pane of the screen. Drag and drop them on the view and modify the text of the label/button from the attributes inspector that’s in the right pane of the view as shown below.
标签和按钮将在屏幕右下方窗格中显示的“对象库”中提供。 将它们拖放到视图上,然后从视图右窗格中的属性检查器中修改标签/按钮的文本,如下所示。
As you can see above, that the button text does not display full text. To wrap the width to the text press Cmd and + together. Something like this would be seen then.
如您在上面看到的,按钮文本不显示全文。 要将宽度换为文本,请同时按Cmd和+。 这样的事情将会出现。
Select the button and hold control and drag it to the golden button (View Controller) at the top and choose showLabel method. This registers that method with the current button in the view.
选择按钮并按住控件并将其拖动到顶部的金色按钮(“视图控制器”),然后选择showLabel方法。 这将使用视图中的当前按钮注册该方法。
To link the Label with the code click on the connections inspector and press control and drag it on the label text. Choose the label variable (or the equivalent name that you’d defined in the header file.
要将标签与代码链接,请在连接检查器上单击,然后按Control键并将其拖动到标签文本上。 选择标签变量(或您在头文件中定义的等效名称)。
Note: The button can be linked in this way too.
注意:按钮也可以通过这种方式链接。
So we’re ready now to build our first iOS application. Choose the type of simulator and build the project from the top left toolbar as shown below.
因此,我们现在准备构建我们的第一个iOS应用程序。 选择模拟器的类型,然后从左上方的工具栏构建项目,如下所示。
The output of the application is given below.
OOPS! We see that the view is out of the screen. The reason is we have the Use Size Classes enabled in the File inspector as shown below.
糟糕! 我们看到视图不在屏幕上。 原因是我们在文件检查器中启用了“使用大小类”,如下所示。
Disabling it would show the correct layout for the screens as shown below.
禁用它会显示正确的屏幕布局,如下所示。
Align the label center vertical and the button as center horizontal and vertical. The alignment of the label can be changed from the alignment option in the attributes inspector. Your final view controller should look something like this:
将标签中心垂直对齐,并将按钮水平和垂直对齐。 可以从属性检查器中的“对齐”选项更改标签的对齐方式。 您的最终视图控制器应如下所示:
The output of the application in action is given below.
So we’ve just build our first iOS Application. You can download the XCode Hello World Project from the link given below.
因此,我们刚刚构建了第一个iOS应用程序。 您可以从下面给出的链接下载XCode Hello World项目 。
翻译自: https://www.journaldev.com/10214/ios-hello-world-example-tutorial
ios8升级ios12教程