ios android 同步的备忘录,简单iOS备忘录App实现
簡單iOS備忘錄App實現
詳細內容參考《瘋狂iOS講義》--李剛編著
完整代碼放到了GitHub。LeeLom MemoDemo
一個很簡單的iOS Demo,主要用來實現一下iOS應用程序沙盒的功能。
程序功能
允許用戶自行添加,刪除數據行,并且利用沙盒實現了數據的持久化。
程序界面.png
點擊保存按鈕
實現過程
由于界面很簡單,所以所有的界面都通過變成方式完成。
添加導航欄,并在導航欄上添加三個按鈕分別為:添加,刪除,保存
//設置界面
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。
實現三個按鈕各自的方法
添加按鈕方法:addItem
這個方法實現的功能:用戶點擊添加按鈕,頁面出現一個UILabe和UITextField,同時由于程序運行過程中,沙盒已經有數據,所以還要考慮加載已經存在的數據。
用戶點擊添加按鈕
-(void)addItem:(id)sender{
[self addItem:sender content:nil];
}
利用重載方法在界面中實現數據的顯示
-(void)addItem:(id)sender content:(NSString)content{
//點擊添加后頁面出現一個新的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事件監聽器
[textField addTarget:self action:@selector(resign:) forControlEvents:UIControlEventEditingDidEndOnExit];
[self.fieldArray addObject:textField];
[self.view addSubview:textField];
nextY += 40;
i++;
}
刪除按鈕方法:removeItem
這個方法實現的功能,刪除頁面最后一個UILabel,UITextField。
-(void)removeItem:(id)sender{
//獲取最后一個元素,yuansu
UILabel* lastlabel = [self.labelArray lastObject];
UITextField* lastTextFiled = [self.fieldArray lastObject];
//從程序界面中刪除
[lastlabel removeFromSuperview];
[lastTextFiled removeFromSuperview];
//從數組中刪除
[self.labelArray removeObject:lastlabel];
[self.fieldArray removeObject:lastTextFiled];
nextY -= 40;
i--;
}
保存按鈕方法:save
這個方法的功能主要是實現用戶點擊添加或刪除后,不僅改變了頁面的數據,并將改變的數據傳遞到應用程序沙盒當中。
-(void)save:(id)sender{
NSMutableArray* array = [[NSMutableArray alloc]init];
for (UITextField* tf in self.fieldArray) {
[array addObject:tf.text];
}
//調用NSMutableArray的方法將結合數據寫入屬性列表中
[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 用戶點擊保存,激發sava方法時,程序把多條數據收集到NSArray集合中,在調用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{
//保證鍵盤能夠在輸入之后關閉
[sender resignFirstResponder];
}
至此,整個備忘錄APP設計完成。
總結
以上是生活随笔為你收集整理的ios android 同步的备忘录,简单iOS备忘录App实现的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 基于ssm高校共享单车管理系统 (源代码
- 下一篇: 金字塔数字排列方法