簡單iOS備忘錄App實現(xiàn)
詳細內(nèi)容參考《瘋狂iOS講義》--李剛編著
完整代碼放到了GitHub呻顽。LeeLom MemoDemo
一個很簡單的iOS Demo晌块,主要用來實現(xiàn)一下iOS應用程序沙盒的功能弊仪。
程序功能
允許用戶自行添加但绕,刪除數(shù)據(jù)行齿椅,并且利用沙盒實現(xiàn)了數(shù)據(jù)的持久化戈轿。
實現(xiàn)過程
由于界面很簡單症副,所以所有的界面都通過變成方式完成店雅。
-
添加導航欄,并在導航欄上添加三個按鈕分別為:添加瓦糕,刪除底洗,保存
//設置界面
UINavigationBar* navBar = [[UINavigationBar alloc]initWithFrame:CGRectMake(0, 20, 320, 44)];
[self.view addSubview:navBar];
UINavigationItem* item = [[UINavigationItem alloc]initWithTitle:@"備忘錄"];
navBar.items = [NSArray arrayWithObject:item];UIBarButtonItem* addBtn = [[UIBarButtonItem alloc]initWithTitle:@"添加" style:UIBarButtonItemStylePlain target:self action:@selector(addItem:)]; UIBarButtonItem* removeBtn = [[UIBarButtonItem alloc]initWithTitle:@"刪除" style:UIBarButtonItemStylePlain target:self action:@selector(removeItem:)]; //將添加和刪除按鈕放在導航欄左邊 item.leftBarButtonItems = [NSArray arrayWithObjects:addBtn,removeBtn, nil]; UIBarButtonItem* saveBtn = [[UIBarButtonItem alloc]initWithTitle:@"保存" style:UIBarButtonItemStylePlain target:self action:@selector(save:)]; item.rightBarButtonItem = saveBtn;
其中三個按鈕分別添加了各自的方法addItem,removeItem,save。
-
實現(xiàn)三個按鈕各自的方法
添加按鈕方法:addItem
這個方法實現(xiàn)的功能:用戶點擊添加按鈕咕娄,頁面出現(xiàn)一個UILabe和UITextField,同時由于程序運行過程中亥揖,沙盒已經(jīng)有數(shù)據(jù),所以還要考慮加載已經(jīng)存在的數(shù)據(jù)圣勒。
用戶點擊添加按鈕
-(void)addItem:(id)sender{
[self addItem:sender content:nil];
}
利用重載方法在界面中實現(xiàn)數(shù)據(jù)的顯示
-(void)addItem:(id)sender content:(NSString)content{
//點擊添加后頁面出現(xiàn)一個新的label和Textfiled
UILabel label = [[UILabel alloc]initWithFrame:CGRectMake(10, nextY, 80, 30)];
label.text = [NSString stringWithFormat:@"第%d項",i];
[self.labelArray addObject:label];
[self.view addSubview:label];UITextField* textField = [[UITextField alloc]initWithFrame:CGRectMake(100, nextY, 210, 30)]; textField.borderStyle = UITextBorderStyleRoundedRect; if (content != nil && content.length > 0) { textField.text = content; } //為textField綁定EdittingDidEndOnExit事件監(jiān)聽器 [textField addTarget:self action:@selector(resign:) forControlEvents:UIControlEventEditingDidEndOnExit]; [self.fieldArray addObject:textField]; [self.view addSubview:textField]; nextY += 40; i++; }
刪除按鈕方法:removeItem
這個方法實現(xiàn)的功能费变,刪除頁面最后一個UILabel,UITextField。
-(void)removeItem:(id)sender{
//獲取最后一個元素,yuansu
UILabel* lastlabel = [self.labelArray lastObject];
UITextField* lastTextFiled = [self.fieldArray lastObject];
//從程序界面中刪除
[lastlabel removeFromSuperview];
[lastTextFiled removeFromSuperview];
//從數(shù)組中刪除
[self.labelArray removeObject:lastlabel];
[self.fieldArray removeObject:lastTextFiled];
nextY -= 40;
i--;
}
保存按鈕方法:save
這個方法的功能主要是實現(xiàn)用戶點擊添加或刪除后圣贸,不僅改變了頁面的數(shù)據(jù)挚歧,并將改變的數(shù)據(jù)傳遞到應用程序沙盒當中。
-(void)save:(id)sender{
NSMutableArray* array = [[NSMutableArray alloc]init];
for (UITextField* tf in self.fieldArray) {
[array addObject:tf.text];
}
//調(diào)用NSMutableArray的方法將結(jié)合數(shù)據(jù)寫入屬性列表中
[array writeToFile:[self filePath] atomically:YES];
//使用UIActiongSheet提示用戶保存成功
UIActionSheet* sheet = [[UIActionSheet alloc]initWithTitle:@"保存成功" delegate:nil cancelButtonTitle:nil destructiveButtonTitle:@"確定" otherButtonTitles:nil, nil];
[sheet showInView:self.view];
}
- 其他的兩個方法
3.1 用戶點擊保存吁峻,激發(fā)sava方法時滑负,程序把多條數(shù)據(jù)收集到NSArray集合中在张,在調(diào)用NSArray的writeToFile:(NSString)filePath atomically:(BOOL)flag方法寫入屬性文件。
-(NSString ) filePath{
//獲取應用的Documents路徑
NSArray paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString documentsDirectory = [paths objectAtIndex:0];
//NSLog([NSString stringWithFormat:@"%@/myList.plist",documentsDirectory]);
return [NSString stringWithFormat:@"%@/myList.plist",documentsDirectory];
}
3.2 用戶輸入完畢矮慕,UITextField需要自動退出
-(void)resign:(id)sender{
//保證鍵盤能夠在輸入之后關(guān)閉
[sender resignFirstResponder];
}
至此帮匾,整個備忘錄APP設計完成。