Reputation: 25
Good day! I just want to know if how can i make a default text for my UItextField
?
for example, i typed 12345 in my text field then after i press "return
" or "Done
" key on my keyboard it should stay there forever even i close the app, or power off my device.
Upvotes: 1
Views: 237
Reputation: 11
You can use NSCoding class two methods
- (void) encodeWithCoder:(NSCoder *)encoder {
[encoder encodeObject:_title forKey:kTitleKey];
[encoder encodeFloat:_rating forKey:kRatingKey];
}
- (id)initWithCoder:(NSCoder *)decoder {
NSString *title = [decoder decodeObjectForKey:kTitleKey];
float rating = [decoder decodeFloatForKey:kRatingKey];
return [self initWithTitle:title rating:rating];
}
to save objects
Upvotes: 0
Reputation: 23271
saving the Integer
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
// saving an NSInteger
[prefs setInteger:12345 forKey:@"integerKey"];
[prefs synchronize];
Retrieve the integer
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
// getting an NSInteger
NSInteger myInt = [prefs integerForKey:@"integerKey"];
Upvotes: 1
Reputation: 8147
You should save the contents somewhere, then. See NSUserDefaults for example, it is good for storing simple data. If you need to store more information, writing it to a file (or Core Data, or using another database) would be a better idea.
Pressing the button can be captured by implementing UITextField
's textFieldShouldReturn:
delegate method.
Upvotes: 2