Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 10-09-2011, 12:19 PM   #1 (permalink)
Registered Member
 
Join Date: May 2011
Posts: 36
DrJones is on a distinguished road
Default Problems with note taking system

Hi, im currently developing an app where the user can take notes. I use UITableViewCells to set up an titleCell and a noteCell. That works great. I use NSUserDefaults to save the notes, and that works fine. The problem occurs when i return to the note view controller. The text is moved around and i can't edit it. If i go back and return again, the text is gone.

I use this code to save:

PHP Code:
- (IBAction)saveNote:(id)sender
{
    
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    [
prefs setObject:titleTextField.text forKey:@"titleCellName"];
    [
prefs setObject:noteTextView.text forKey:@"noteCellText"];
    [
prefs synchronize];
    
    [
self dismissModalViewControllerAnimated:YES];

when i retrieve the loaded data, i do as following:

PHP Code:
- (void)viewDidLoad
{
    [
super viewDidLoad];
    
// Do any additional setup after loading the view from its nib.
    
    
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    
    
NSString *titleText = [prefs objectForKey:@"titleCellName"];
    
NSString *noteText = [prefs objectForKey:@"noteCellText"];

    
titleCell.textLabel.text titleText;
    
noteCell.textLabel.text noteText;

can anyone help me figure out what is wrong?
DrJones is offline   Reply With Quote
Old 10-09-2011, 05:22 PM   #2 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by DrJones View Post
Hi, im currently developing an app where the user can take notes. I use UITableViewCells to set up an titleCell and a noteCell. That works great. I use NSUserDefaults to save the notes, and that works fine. The problem occurs when i return to the note view controller. The text is moved around and i can't edit it. If i go back and return again, the text is gone.

I use this code to save:

PHP Code:
- (IBAction)saveNote:(id)sender
{
    
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    [
prefs setObject:titleTextField.text forKey:@"titleCellName"];
    [
prefs setObject:noteTextView.text forKey:@"noteCellText"];
    [
prefs synchronize];
    
    [
self dismissModalViewControllerAnimated:YES];

when i retrieve the loaded data, i do as following:

PHP Code:
- (void)viewDidLoad
{
    [
super viewDidLoad];
    
// Do any additional setup after loading the view from its nib.
    
    
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    
    
NSString *titleText = [prefs objectForKey:@"titleCellName"];
    
NSString *noteText = [prefs objectForKey:@"noteCellText"];

    
titleCell.textLabel.text titleText;
    
noteCell.textLabel.text noteText;

can anyone help me figure out what is wrong?
You've got the wrong idea on how to use table views.

You should have a data source that uses an ordered list of information. Frequently, one or more NSArray objects does the job. You implement a cellForRowAtIndexPath method that takes data from your ordered list at the specified row (and possibly section) and uses it to populate a cell when the system asks you for that data.

As the user scrolls your list, he/she will expose new cells, and the system will call your cellForRowAtIndexPath method for those new rows/sections. The user might scroll a row off the screen, then scroll it back. You use an ordered list (like an NSArray) to keep the data and create cells to display that data as needed.

When you read your data back from disk, your read it into your ordered list data structure (again, usually one or more NSArrays) and then call the table view's reloadData method. The table view then asks you to create cells for some number of indexPath values.

Your viewDidLoad method is simply plugging data into fixed cells (which probably don't exist yet.) That's not how it works.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 10-10-2011, 07:28 AM   #3 (permalink)
Registered Member
 
Join Date: May 2011
Posts: 36
DrJones is on a distinguished road
Default

Quote:
Originally Posted by Duncan C View Post
You've got the wrong idea on how to use table views.

You should have a data source that uses an ordered list of information. Frequently, one or more NSArray objects does the job. You implement a cellForRowAtIndexPath method that takes data from your ordered list at the specified row (and possibly section) and uses it to populate a cell when the system asks you for that data.

As the user scrolls your list, he/she will expose new cells, and the system will call your cellForRowAtIndexPath method for those new rows/sections. The user might scroll a row off the screen, then scroll it back. You use an ordered list (like an NSArray) to keep the data and create cells to display that data as needed.

When you read your data back from disk, your read it into your ordered list data structure (again, usually one or more NSArrays) and then call the table view's reloadData method. The table view then asks you to create cells for some number of indexPath values.

Your viewDidLoad method is simply plugging data into fixed cells (which probably don't exist yet.) That's not how it works.
ahh thanks, it makes sense now. I think i will rethink a new way without tableviews and use a SQLite database to store the data aswell.

Thanks for the help!
DrJones is offline   Reply With Quote
Reply

Bookmarks

Tags
iphone sdk, note, nsuserdefaults

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 398
18 members and 380 guests
13dario13, 7twenty7, eski, EvilElf, glenn_sayers, HemiMG, jarv, LunarMoon, morterbaher, n00b, pbart, Pudding, QuantumDoja, sacha1996, Sami Gh, UMAD, VinceYuan
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,673
Threads: 94,122
Posts: 402,906
Top Poster: BrianSlick (7,990)
Welcome to our newest member, morterbaher
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 05:22 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0