iOS菜鸟成长笔记(1)——第一个iOS应用

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

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

转载请说明出处:http://blog.csdn.net/dawanganban

一、着手开发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 应用程序显示内容的空间很有限,因此视图控制器提供了所需要的基础结构,可让您移除一个视图控制器的视图,替换为另一个视图控制器中的视图。通过让视图控制器文件与串联图中的视图进行通信,可以定义应用程序中的交互方式。方法是通过 Action 与 Outlet 来定义串联图与源代码文件之间的连接。” 

下面通过一个简单的实例,我们就可以看到Action和Outlet的使用了。

二、拖放UI组件

打开Main.storyboard向其中拖放组件元素。

技术分享

上面用到了 UITextField、UILabel、UIButton,有关这些组件的详细配置我觉得对于做过Android开发的朋友真心没必要花过多时间。

三、监听Button事件

ViewController.h  (第6行代码)

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

//声明一个方法来监听按钮点击 IBAction === void
- (IBAction)btnOnClick;

@end
ViewController.m  (第15-17行代码)

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

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

#pragma mark 监听按钮点击
- (void)btnOnClick{
    NSLog(@"按钮被人点击了");
}

@end
技术分享

按住control键,拖动Button到右边的 -(IBActon)btnOnClick处建立连接。

command+R运行观察结果,点击按钮会发现控制台打印出了Log

2015-03-14 11:10:11.820 第一个iOS程序[12810:142497] 按钮被人点击了
2015-03-14 11:10:12.004 第一个iOS程序[12810:142497] 按钮被人点击了

四、获取文本框对象

我们要获取文本对象,先要将文本对象作为ViewController的一个属性(第9、10行)

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

//声明一个方法来监听按钮点击 IBAction === void
- (IBAction)btnOnClick;

//声明两个属性用来保存2个文本输入框
@property (nonatomic, weak) IBOutlet UITextField *num1;
@property (nonatomic, weak) IBOutlet UITextField *num2;

@end
用同样的方法(按住control键拖动我们的文本对象建立连接),这样我们就可以取得文本对象的内容了

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

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

#pragma mark 监听按钮点击
- (void)btnOnClick{
    NSLog(@"按钮被人点击了");
    
    UITextField *textField1 = self.num1;
    UITextField *textField2 = self.num2;
    
    NSString *text1 = textField1.text;
    NSString *text2 = textField2.text;
    
    NSLog(@"文本1 = %@, 文本2 = %@", text1, text2);
    
}

@end
command + R ,在文本框内输入后点击按钮

技术分享

控制台输出:

2015-03-14 11:33:07.342 第一个iOS程序[14531:166271] 按钮被人点击了
2015-03-14 11:33:07.343 第一个iOS程序[14531:166271] 文本1 = 12, 文本2 = 34

五、完成计算功能

用同样的方法添加UILabel的属性

#pragma mark 监听按钮点击
- (void)btnOnClick{
    NSLog(@"按钮被人点击了");
    
    UITextField *textField1 = self.num1;
    UITextField *textField2 = self.num2;
    
    NSString *text1 = textField1.text;
    NSString *text2 = textField2.text;
    
    NSLog(@"文本1 = %@, 文本2 = %@", text1, text2);
    
    double num1 = [text1 doubleValue];
    double num2 = [text2 doubleValue];
    
    double sum = num1 + num2;
    
    NSLog(@"文本和 = %f", sum);
    
    UILabel *resultLabel = self.result;
    
    resultLabel.text = [NSString stringWithFormat:@"%f", sum];
    
}
技术分享

我们会发现输出结果后面是三个点,这个是因为我设置了长度超出省略,可以设置Line Breaks 为 Clip(截断)

技术分享

最后再提一句:Objective-C是iOS的基石(核心)所以希望深入研究iOS开发的朋友建议加深对OC的学习,可以参考我的另外一个系列博客《Objective-C基础笔记

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。