2011-07-06 56 views
-1

試圖讓文本域自動保存數據並存儲它,我希望能夠返回到視圖並保留數據,直到再次編輯。保存數據到文本框和創建代理

到目前爲止,我這...在文本框

@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 

數據不保存。謝謝

回答

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]; 
} 
+0

第一機能的研究是一個委託我被告知設置調用變量...我嘗試將這些方法不過在viewWillAppear中得到一個錯誤在textField。 – Bradley

+0

請用textField實例變量名替換textfield? – adam

相關問題