我的第一个 iOS 程序:iTahDoodle

我的第一个 iOS 程序:iTahDoodle

实现了一个名为 iTahDoodle 的 iOS 应用。这是一个简单的任务管理程序,并通过 property list 文件保存数据。

完整代码见于:UestcXiye/Objective-C-Practice 的 iTahDoodle。

应用功能

用户在文本框输入文字,点击 Insert 按钮,将文本插入到一个任务数组中,用一个 TableView 展示任务数组。

对象图

采用 MVC 模式:

请添加图片描述

参考书过于远古,从创建应用开始就有很多不同的地方,慢慢摸索才做出来。这里把书中 AppDelegate 的大部分内容转到 ViewController 中实现。

应用委托对象

iOS 应用启动时,会创建一个 UIApplication 实例,用于控制应用的状态。此外,程序还会创建 AppDelegate 实例,并将其设置为 UIApplication 实例的委托对象。

应用启动完成后,UIApplication 实例会向其委托对象发送 application:didFinishLaunchingWithOptions: 消息。凡是需要在程序能够和用户交互前就完成的初始化工作,都在该方法内实现。这里我们只需要完成 UIWindow 对象的创建和初始化即可。

// AppDelegate.h
#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (nonatomic, strong) UIWindow *window;

@end
  
// AppDelegate.m
#pragma mark - 应用委托对象的回调方法

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    /* 凡是需要在程序能够和用户交互前就完成的初始化工作,都在该方法内完成 */
    [self createAppWindow];
    
    return YES;
}

- (void)createAppWindow;
{
    if (@available(iOS 13.0, *)) {}
    else
    {
        // 创建并设置 UIWinodw 对象
        self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
        self.window.rootViewController = [[UITabBarController alloc] init];
        // 设置 UIWindow 实例的背景颜色,并放上屏幕
        self.window.backgroundColor = [UIColor whiteColor];
        [self.window makeKeyAndVisible];
    }
}

设置视图

视图加载后,就应该设置相应的视图对象,包括:输入栏、按钮、表格视图。大致流程为:创建所有的对象,并进行相应的设置,将设置过的视图对象加入 UIWindow 对象,成为其下的子视图。

// ViewController.h
#import <UIKit/UIKit.h>

NSString *docPath(void);

@interface ViewController : UIViewController
    <UITextFieldDelegate, UIApplicationDelegate, UITableViewDelegate>
{
    UITableView *_taskTable; // 表格视图,显示所有任务
    UITextField *_taskField; // 输入框
    UIButton *_insertButton; // 插入任务按钮
}

@property (nonatomic) UITableView *taskTable; // 表格视图,显示所有任务
@property (nonatomic, retain) UITextField *taskField; // 输入框
@property (nonatomic) UIButton *insertButton; // 插入任务按钮

@end

// ViewController.m
@implementation ViewController

@synthesize taskTable = _taskTable;
@synthesize taskField = _taskField;
@synthesize insertButton = _insertButton;

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.tasks = [NSMutableArray array];
    
    // 设置 3 个对象的 frame 属性
    // NSLog(@"%lf, %lf", self.view.frame.size.width, self.view.frame.size.height);
    /* width: 393, height: 852 */
    CGRect tableFrame = CGRectMake(0, 115, self.view.frame.size.width, self.view.frame.size.height - 130);
    CGRect fieldFrame = CGRectMake(20, 80, 280, 30);
    CGRect buttonFrame = CGRectMake(310, 80, 70, 30);
    
    // 创建并设置 UITableView 对象
    self.taskTable = [[UITableView alloc] initWithFrame:tableFrame style:UITableViewStylePlain];
    self.taskTable.separatorStyle = UITableViewCellSeparatorStyleNone;
    // 需要创建新的单元格时,告诉 UITableView 对象要实例化哪个类
    [self.taskTable registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];
    
    // 创建并设置 UITextField 对象
    self.taskField = [[UITextField alloc] initWithFrame:fieldFrame];
    self.taskField.borderStyle = UITextBorderStyleRoundedRect; // 圆角
    self.taskField.placeholder = @"Type a task, tap Insert";
    self.taskField.delegate = self;
    
    // 创建并设置 UIButton 对象
    self.insertButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; // 圆角
    self.insertButton.frame = buttonFrame;
    // 为按钮设置标题
    [self.insertButton setTitle:@"Insert" forState:UIControlStateNormal];
    
    // 将 3 个 UI 对象加入 UIWindow 实例
    [self.view addSubview:self.taskTable];
    [self.view addSubview:self.taskField];
    [self.view addSubview:self.insertButton];
}

为按钮关联动作方法

按钮的回调通过目标-动作对实现。按钮的动作是按钮被按下后需要发送的消息,按钮的目标是接收这条消息的对象。

在设置按钮时,添加目标-动作对:

// 设置目标-动作对
[self.insertButton addTarget:self action:@selector(addTask:) forControlEvents:UIControlEventTouchUpInside];

这里按钮的目标是 self,也就是 ViewController;按钮的动作是 addTask: 方法,它的实现:

#pragma mark - Actions

-(void)addTask:(id)sender
{
    NSString *text = [self.taskField text];
    if ([text length] == 0)
        return;
    NSLog(@"Task entered: %@", text);
    [self.tasks addObject:text];
    [self.taskTable reloadData];
    [self.taskField setText:@""]; // 清空文本
    [self.taskField resignFirstResponder]; // 关闭键盘
}

addTask: 方法取得输入栏的文本,添加到 task 数组去,task 数组的内容会在 taskTable 中显示,要刷新一下才会展示出新插入的内容,然后清空输入栏的文本,让输入栏取消其第一响应对象状态,以此来关闭键盘。

为 UITableView 对象提供数据

UITableView 对象不包含任何数据,需要提供一个数据源对象给它。对于本程序,我们将 ViewController 实例设置为 UITableView 对象的数据源,因此 ViewController 必须遵守 UITableViewDataSource 协议。

// ViewController.h
@interface ViewController : UIViewController<UITableViewDelegate>
...
@property (nonatomic) NSMutableArray *tasks; // 任务数组
...
@end
  
// ViewController.m
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    ...
    // 将当前对象设置为 UITableView 对象的 dataSource
    self.taskTable.dataSource = self;
    ...
}

还要实现 UITableViewDataSource 协议的两个必需方法:

#pragma mark - 管理表格视图
// 根据指定的表格段索引给出相应表格段所包含的行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // 只有一个表格段,所以直接返回任务数组的对象个数
    return [self.tasks count];
}
// 根据指定的表格段索引和行索引给出相应的 UITableViewCell 对象
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 创建新的 UITableViewCell 对象
    UITableViewCell *c = [self.taskTable dequeueReusableCellWithIdentifier:@"Cell"];
    // 根据模型对象(tasks 数组)重新设置 UITableViewCell 对象
    NSString *item = [self.tasks objectAtIndex:indexPath.row];
    c.textLabel.text = item;
    // 返回设置后的 UITableViewCell 对象
    return c;
}

保存并加载任务数据

当程序进入后台运行状态时,将任务数组保存在 property list 格式的文件中;在用户重启应用时,加载保存的任务。

这里添加了一个 C 语言辅助函数,返回保存文件的路径。其实现在 #import 指令之后,@implementation 指令(类声明起始处)之前。

#import "ViewController.h"

@interface ViewController ()

@end
// 辅助函数,返回保存用户任务数据的文件路径
NSString *docPath(void)
{
    NSArray *pathList = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    return [pathList[0] stringByAppendingPathComponent:@"data.td"];
}

@implementation ViewController
...
#pragma mark - 应用委托对象的回调方法

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    NSArray *plist = [NSArray arrayWithContentsOfFile:docPath()];
    if (plist)
    {
        NSLog(@"Loading data...");
        self.tasks = [plist mutableCopy];
    }
    else
    {
        self.tasks = [NSMutableArray array];
    }
    return YES;
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    NSLog(@"Saving data...");
    // 将任务数组保存至文件
    [self.tasks writeToFile:docPath() atomically:YES];
}

在模拟器上运行程序

至此介绍完了整个程序的实现细节。用 iOS Simulator 看看程序的效果:

在这里插入图片描述

在输入栏中输入文字,点击 Insert 按钮,就能插入任务,并在下方的 TableView 中显示:

在这里插入图片描述

下载链接

GitHub:Objective-C-Practice 中的 iTahDoodle。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

UestcXiye

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值