Ios 文本字段中的数据未保存
这是我的.h文件Ios 文本字段中的数据未保存,ios,objective-c,Ios,Objective C,这是我的.h文件 #import <UIKit/UIKit.h> // for use in xib/storyboard: #define TAG_TEXT_FIELD 10000 #define CELL_REUSE_IDENTIFIER @"EditableTextCell" @interface EditableListViewController : UITableViewController // list data: @property (nonatomic, c
#import <UIKit/UIKit.h>
// for use in xib/storyboard:
#define TAG_TEXT_FIELD 10000
#define CELL_REUSE_IDENTIFIER @"EditableTextCell"
@interface EditableListViewController : UITableViewController
// list data:
@property (nonatomic, copy) NSArray *contents;
// methods for possible overriding:
- (void)contentsDidChange;
- (UITextField *)createTextFieldForCell:(UITableViewCell *)cell;
@end
#导入
//用于xib/故事板:
#定义标记\文本\字段10000
#定义单元格\u重用\u标识符@“EditableTextCell”
@接口EditableListViewController:UITableViewController
//列表数据:
@属性(非原子,副本)NSArray*内容;
//用于可能的重写的方法:
-(无效)内容变更;
-(UITextField*)createTextFieldForCell:(UITableViewCell*)单元格;
@结束
这是我的.m文件
#import "EditableListViewController.h"
static NSString *inactiveTextFieldHint = @"Tap to add item";
static NSString *activeTextFieldHint = @"";
static NSString *returnTappedTextFieldHint = @"~"; // HACK to mark when return was tapped
#pragma mark - Helper Categories
@interface UITextField (ChangeReturnKey)
- (void)changeReturnKey:(UIReturnKeyType)returnKeyType;
@end
@implementation UITextField (ChangeReturnKey)
- (void)changeReturnKey:(UIReturnKeyType)returnKeyType
{
self.returnKeyType = returnKeyType;
[self reloadInputViews];
}
@end
#pragma mark - EditableListViewController
@interface EditableListViewController () <UITextFieldDelegate> {
NSMutableArray *rowsContent;
}
@end
@implementation EditableListViewController
#pragma mark - Contents Assignment
- (NSArray *)contents
{
return rowsContent;
}
- (void)setContents:(NSArray *)contents
{
rowsContent = [NSMutableArray arrayWithArray:contents];
[self.tableView reloadData];
}
#pragma mark - Table Changes
- (void)deleteRow:(NSIndexPath *)indexPath
{
[rowsContent removeObjectAtIndex:indexPath.row];
[self.tableView beginUpdates];
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
[self.tableView endUpdates];
[self contentsDidChange];
}
- (void)addRow:(NSIndexPath *)indexPath text:(NSString *)text
{
if (rowsContent == nil) {
rowsContent = [[NSMutableArray alloc] initWithCapacity:1];
}
[rowsContent addObject:text];
NSIndexPath *nextRow = [NSIndexPath indexPathForRow:indexPath.row+1 inSection:indexPath.section];
[self.tableView beginUpdates];
[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:nextRow] withRowAnimation:UITableViewRowAnimationAutomatic];
[self.tableView endUpdates];
[self contentsDidChange];
}
- (void)contentsDidChange
{
}
#pragma mark - Table View Data Source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return rowsContent.count + 1; // extra one for inserting new row
}
- (UITextField *)createTextFieldForCell:(UITableViewCell *)cell
{
CGFloat padding = 8.0f;
CGRect frame = CGRectInset(cell.contentView.bounds, padding, padding / 2);
UITextField *textField = [[UITextField alloc] initWithFrame:frame];
CGFloat spareHeight = cell.contentView.bounds.size.height - textField.font.pointSize;
frame.origin.y = self.tableView.style == UITableViewStyleGrouped ? spareHeight / 2 : spareHeight - padding/2;
textField.frame = frame;
textField.tag = TAG_TEXT_FIELD;
textField.borderStyle = UITextBorderStyleNone;
textField.returnKeyType = UIReturnKeyDone;
textField.autocapitalizationType = UITextAutocapitalizationTypeSentences;
textField.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
return textField;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *reuseIdentifier = CELL_REUSE_IDENTIFIER;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
}
UITextField *textField = (UITextField *)[cell viewWithTag:TAG_TEXT_FIELD];
if (textField == nil) {
textField = [self createTextFieldForCell:cell];
[cell.contentView addSubview:textField];
}
textField.delegate = self;
if (indexPath.row < rowsContent.count) {
textField.text = [rowsContent objectAtIndex:indexPath.row];
textField.placeholder = nil;
} else {
textField.text = nil;
textField.placeholder = NSLocalizedString(inactiveTextFieldHint, nil);
}
return cell;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
switch (editingStyle) {
case UITableViewCellEditingStyleDelete: {
[self deleteRow:indexPath];
break;
}
case UITableViewCellEditingStyleInsert: {
UITableViewCell *sourceCell = [tableView cellForRowAtIndexPath:indexPath];
UIView *textField = [sourceCell viewWithTag:TAG_TEXT_FIELD];
[textField becomeFirstResponder];
break;
}
case UITableViewCellEditingStyleNone:
break;
}
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return (indexPath.section == 0);
}
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
return indexPath.section == 0 && indexPath.row < rowsContent.count;
}
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
NSString *rowContent = [rowsContent objectAtIndex:fromIndexPath.row];
[rowsContent removeObjectAtIndex:fromIndexPath.row];
[rowsContent insertObject:rowContent atIndex:toIndexPath.row];
[self contentsDidChange];
}
#pragma mark - Table View Delegate
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section != 0) return UITableViewCellEditingStyleNone;
return indexPath.row < rowsContent.count ? UITableViewCellEditingStyleDelete : UITableViewCellEditingStyleInsert;
}
- (NSIndexPath *)tableView:(UITableView *)tableView
targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath
toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath
{
return proposedDestinationIndexPath.section == 0 && proposedDestinationIndexPath.row < rowsContent.count
? proposedDestinationIndexPath
: [NSIndexPath indexPathForRow:rowsContent.count-1 inSection:0];
}
#pragma mark - UITextFieldDelegate
- (NSIndexPath *)cellIndexPathForField:(UITextField *)textField
{
UITableViewCell *parentCell = (UITableViewCell *)[[textField superview] superview];
return [self.tableView indexPathForCell:parentCell];
}
- (NSUInteger)rowIndexForField:(UITextField *)textField
{
return [self cellIndexPathForField:textField].row;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
if ([textField.text length] == 0) {
textField.placeholder = NSLocalizedString(activeTextFieldHint, nil);
}
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
textField.placeholder = returnTappedTextFieldHint;
[textField resignFirstResponder];
return YES;
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if (textField.text.length == 0) {
// if it's the last field, change the return key to "Next"
if ([self rowIndexForField:textField] == rowsContent.count) {
[textField changeReturnKey:UIReturnKeyNext];
}
}
else {
// if return button is "Next" and field is about to be empty, change to "Done"
if (textField.returnKeyType == UIReturnKeyNext && string.length == 0 && range.length == textField.text.length) {
[textField changeReturnKey:UIReturnKeyDone];
}
}
return YES;
}
- (BOOL)textFieldShouldClear:(UITextField *)textField
{
if (textField.returnKeyType == UIReturnKeyNext) {
[textField changeReturnKey:UIReturnKeyDone];
}
return YES;
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
NSIndexPath *currRow = [self cellIndexPathForField:textField];
NSUInteger cellIndex = currRow.row;
if (cellIndex < rowsContent.count) {
if ([textField.text length]) {
if (![textField.text isEqualToString:[rowsContent objectAtIndex:cellIndex]]) {
[rowsContent replaceObjectAtIndex:cellIndex withObject:textField.text];
[self contentsDidChange];
}
}
else {
[self deleteRow:currRow];
}
}
else { // new row
if ([textField.text length]) {
[self addRow:currRow text:textField.text];
[textField changeReturnKey:UIReturnKeyDone];
if ([textField.placeholder isEqual:returnTappedTextFieldHint]) { // if tapped return, go to the next field
UITableViewCell *nextCell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:cellIndex+1 inSection:currRow.section]];
UIView *nextTextField = [nextCell viewWithTag:TAG_TEXT_FIELD];
[nextTextField becomeFirstResponder];
}
}
else {
textField.placeholder = NSLocalizedString(inactiveTextFieldHint, nil);
}
}
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
@end
#导入“EditableListViewController.h”
静态NSString*inactiveTextFieldHint=@“点击添加项”;
静态NSString*activeTextFieldHint=@;
静态NSString*returnTappedTextFieldHint=@“~”;//点击回车键时点击标记
#pragma标记-辅助对象类别
@接口UITextField(ChangeReturnKey)
-(void)changeReturnKey:(UIReturnKeyType)returnKeyType;
@结束
@实现UITextField(ChangeReturnKey)
-(无效)changeReturnKey:(UIReturnKeyType)returnKeyType
{
self.returnKeyType=returnKeyType;
[自重新加载输入视图];
}
@结束
#pragma标记-EditableListViewController
@接口EditableListViewController(){
NSMutableArray*行内容;
}
@结束
@可编辑ListViewController的实现
#pragma标记-内容分配
-(NSArray*)目录
{
返回行内容;
}
-(void)setContents:(NSArray*)contents
{
rowsContent=[NSMUTABLEARRY arrayWithArray:contents];
[self.tableView重载数据];
}
#pragma标记-表更改
-(void)deleteRow:(NSIndexPath*)indexPath
{
[rowsContent removeObjectAtIndex:indexath.row];
[self.tableView开始更新];
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]with RowAnimation:UITableViewRowAnimationAutomatic];
[self.tableView endUpdates];
[自我满足改变];
}
-(void)addRow:(NSIndexPath*)indexPath文本:(NSString*)文本
{
如果(行内容==nil){
rowsContent=[[NSMutableArray alloc]initWithCapacity:1];
}
[行内容添加对象:文本];
nsindepath*nextRow=[nsindepath indexPathForRow:indepath.row+1第1节:indepath.section];
[self.tableView开始更新];
[self.tableView重新加载rowsatindexpaths:[NSArray arrayWithObject:indexath]withRowAnimation:UITableViewRowAnimationAutomatic];
[self.tableView insertRowsAtIndexPaths:[NSArray arraywhithobject:nextRow]带行动画:uitableview行动画自动];
[self.tableView endUpdates];
[自我满足改变];
}
-(无效)内容IDChange
{
}
#pragma标记-表视图数据源
-(NSInteger)表格视图中的节数:(UITableView*)表格视图
{
返回1;
}
-(NSInteger)表视图:(UITableView*)表视图行数节:(NSInteger)节
{
return rowsContent.count+1;//插入新行需要额外一个
}
-(UITextField*)createTextFieldForCell:(UITableViewCell*)单元格
{
CGFloat填充=8.0f;
CGRect frame=CGRectInset(cell.contentView.bounds,padding,padding/2);
UITextField*textField=[[UITextField alloc]initWithFrame:frame];
CGFloat spareHeight=cell.contentView.bounds.size.height-textField.font.pointSize;
frame.origin.y=self.tableView.style==UITableViewStyleGrouped?spareHeight/2:spareHeight-padding/2;
textField.frame=frame;
textField.tag=标记\文本\字段;
textField.borderStyle=UITextBorderStyleNone;
textField.returnKeyType=UIReturnKeyDone;
textField.autocapitalizationType=uitextAutocapitalizationtype句;
textField.autoresizingMask=UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
返回文本字段;
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
静态NSString*reuseIdentifier=单元格\u重用\u标识符;
UITableViewCell*单元格=[tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
如果(单元格==nil){
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
}
UITextField*textField=(UITextField*)[带标记的单元格视图:标记\文本\字段];
如果(textField==nil){
textField=[self-createTextFieldForCell:cell];
[cell.contentView addSubview:textField];
}
textField.delegate=self;
if(indexPath.rowSaving
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
// saving an NSString
[prefs setObject:@"TextToSave" forKey:@"keyToLookupString"];
// saving an NSInteger
[prefs setInteger:42 forKey:@"integerKey"];
// saving a Double
[prefs setDouble:3.1415 forKey:@"doubleKey"];
// saving a Float
[prefs setFloat:1.2345678 forKey:@"floatKey"];
// This is suggested to synch prefs.
[prefs synchronize];///////Very Important. This SAVES THE NEWLY ADDED INTO NSUserDefaults
Retrieving
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
// getting an NSString
NSString *myString = [prefs stringForKey:@"keyToLookupString"];
// getting an NSInteger
NSInteger myInt = [prefs integerForKey:@"integerKey"];
// getting an Float
float myFloat = [prefs floatForKey:@"floatKey"];
[[NSUserDefaults standardUserDefaults] boolForKey:@"yourKey"] // returns bool
if(![[NSUserDefaults standardUserDefaults] objectForKey:@"yourKey"])
{
//no object present. Set the object.
}
else
{
//retrieve the object and do what u wish
}