在文本字段中保存数据并创建委托

时间:2011-07-06 08:20:03

标签: iphone objective-c xcode

尝试获取文本字段以自动保存数据并存储它,我希望能够返回到视图并保留数据直到再次编辑。

到目前为止,我有这个...

@interface TableDatabaseViewController : UIViewController <UITextFieldDelegate>
{

    Tables*table;
    NSArray * tables;
    IBOutlet UITextField * surname;
    IBOutlet UITextField * seatNo;
    IBOutlet UISegmentedControl * occupied; 
    IBOutlet UITextField * numberLabel;
    int tapCount;
    id <TableInfoDelgate> modTable;

}

@property (nonatomic, retain) Tables*table;
@property (nonatomic, retain) NSArray * tables;
@property (nonatomic, retain) IBOutlet UITextField * surname;
@property (nonatomic, retain) IBOutlet UITextField * seatNo;
@property (nonatomic, retain) IBOutlet UISegmentedControl* occupied;
@property (nonatomic, retain) id <TableInfoDelgate> modTable;
@property (nonatomic, retain) IBOutlet UITextField * numberLabel;
@property int tapCount;

-(id)initWithTables:(Tables*)t;
//-(void)myFunction;


@end




@synthesize table, numberLabel,tables,tapCount, modTable, seatNo,surname, occupied;


- (void)dealloc
{
    self.numberLabel =nil;
    self.surname = nil;
    self.table = nil;
    self.seatNo = nil;
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.

-(void)myFunction
{
    AddTableViewController * createController = [[AddTableViewController alloc] initWithNibName:@"AddTableViewController" bundle:nil];

    [self.navigationController pushViewController:createController animated:YES];

    [createController release];


}

- (void)viewDidLoad
{
    UIBarButtonItem * theButton = [[UIBarButtonItem alloc] initWithTitle:@"Order" style:UIBarButtonItemStyleBordered target:self action:@selector(myFunction)];

    self.navigationItem.rightBarButtonItem = theButton;
    [theButton release];

    self.tables = [TableListBuilder getTables];

    self.numberLabel.text = [NSString stringWithFormat:@"%@", self.table.number];

    self.seatNo.text = self.table.seats;
    self.surname.text = self.table.surname;

//    self.occupied.text = self.table.occupied;



    [super viewDidLoad];

}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:NO];

    [self.modTable modifyTableatIndex: nil andsurname:self.surname.text andseatNo:self.seatNo.text andoccupied: self.occupied];

}

I also created a delegate which is:


@protocol TableInfoDelgate <NSObject>

-(void) modifyTableatIndex: (int) index andsurname: (NSString*) surname andseatNo: (NSString*) seatNo andoccupied: (BOOL) occupied;

@end

文本字段中的数据未保存。感谢

1 个答案:

答案 0 :(得分:0)

这个功能有什么作用?

-(void) modifyTableatIndex: (int) index andsurname: (NSString*) surname andseatNo: (NSString*) seatNo andoccupied: (BOOL) occupied;

要存储简单值,请查看NSUserDefaults

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [textField setText:[[NSUserDefaults standardUserDefaults] objectForKey:@"storedTextValue"];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [[NSUserDefaults standardUserDefaults] setObject:textField.text forKey:@"storedTextValue"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}