This is the final installment of our 4 part series of creating a Todo list for the iPhone. In this tutorial, I will detail how to add and delete new todo objects from the SQLite database. Make sure that you have completed the following tutorials before you begin this one:
- iPhone Programming Tutorial – Creating a ToDo List Using SQLite Part 1
- iPhone Programming Tutorial – Creating a ToDo List Using SQLite Part 2
- iPhone Programming Tutorial – Creating a ToDo List Using SQLite Part 3
When you have completed this tutorial, you should have a main screen that looks something like this:
Let’s get started…
The first thing we need to do is add the UIBarButtonItem items to the NavigationBar so that we get the “Edit” and “Add” button. Open up RootViewController.m and add the following code to the viewDidLoad method.
The first thing we see is the line that sets the leftBarButtonItem to self.editButtonItem. This automatically adds the “Edit” button to the NavigationController. Also, it sets up the functionality that allows the “delete” buttons to be displayed when the button is pressed. You can see this functionality if you do a “Build and Go” at this step. Next, I have manually created a UIBarButtonItem and added it to the navigationbar. This can be done in Interface Builder, but I wanted to show you how to do it manually and assign an action to it (I’m sure you will require this functionality in a future program). Here is a break down of the parameters:
- initWithTitle – The text to be displayed on the button
- style – How the button will look
- target – The class object that handles the messages sent from this button
- action – The method to be called when the button is passed. We can use @selector and give it the name of the function to call.
Finally, we assign this button to the rightBarButtonItem. If you do a Build and Go, it should error since we haven’t created the addTodo method. We will do that in a bit. Now, let’s create a method inside of our Todo object that will add new Todos to the database.
Open up Todo.h and add the following code:
So in addition to the insertNewTodoIntoDatabase method, we also see the deleteFromDatabase method signature. I have just added this so I don’t have to come back to it later. We will be implementing this when I show you how to delete todos from the database. One thing to note about the insertNewTodoIntoDatabase method is it has a “+” rather than a “-” sign. This means that it is a static method. Static methods are associated with the class not the instance meaning we can call this method without instanciating this class. So we can do stuff like Todo.insertNewTodoIntoDatabase. Now we will implement this method.
Before we can do this, we must declare a few more static sqlite3_statement’s. Add the following statements to the top of Todo.m
Nothing new here…Now implement the following method:
This is similar to our update method. Notice that we are inserting default values into the database. This is so we don’t run into any problems with null or nil values. The most important part of this method is the fact that it returns the primary key of the newly created todo object. This will be used later so we can immediately transition to the todo when the “Add” button is pressed. The last thing we need to do to the todo object is update the dehydrate method so that the todoText gets saved if it gets changed. Update the dehydrate method to look like this:
There are only a few minor changes here. First we see the “text = ?” part added to the sql statement. This is simply so we can update the text of the todo. The other change is we bound the self.text property to the 1st question mark in the sql statement. One thing to notice is we call [self.text UTF8String]. This is because sqlite3_bind_text takes a (char *). This will convert an NSString to an acceptable format.
Now we need to add a method inside of our RootViewController to add a todo. This is the method that will be called when the user presses the “Add” button. Inside of RootViewController.m add the following code:
First, we get a reference to the appDelegate object. This is because we need to call its addTodo method. Next, we instantiate the TodoViewController if it has not already been instantiated. We need this around because we will push it on to the view stack and transition to it after we create our new todo object. After this is done, we call the addTodo method of the appDelegate. It will return the newly created todo object and the view will be transitioned to its detail screen in order to update its details. Now we need to implement the method addTodo inside of our appDelegate. Open up todoAppDelegate.h and add the following code to create the method signature.
Now, let’s implement this method. Open up todoAppDelegate.m and add the following code:
First, we are calling the insertNewTodoIntoDatabase method of the Todo object. Notice that we are simply calling the method without first building an instance of a todo object. As I said in tutorial 3, this is because that method is static and gets called without building an instance of the class. Next, we insatiate the todo object that was just created by calling its initWithPrimaryKey method. This will give us reference to the new todo object. Finally, we append this todo to the end of our todos array. Since our UITableView is updated with this array, it will automatically include the new todo object. The last line just returns this todo object.
Remember is the last tutorial we made it so the users could update the status and the priority of a todo? Well, now we also need to give them the ability to update the text of the todo. So open up TodoViewController.h and add the following code:
Ok, so I’m guessing you are wondering why the UITextView for the todoText object has been changed to a UITextField. Well, I will tell you. UITextView doesn’t have the methods that we need to save the text with our current design. We will also be changing this on our Interface inside of Interface Builder. So for now, just believe me and anywhere it says UITextView, change it to UITextField. The only additional code we added here is the method signature for the updateText method. It’s an IBAction that will get called when the user presses the “Done” button on the keyboard after setting the text for the todo. Next, we need to implement this method. Open up TodoViewController.m and add the following code:
All this does is update the text of the todo to the text that the user entered inside of the UITextField. The last thing we need to do in order to add a todo is to replace the UITextView with a UITextField and connect it to our updateText method. Double click on your TodoViewController.xib file to open it in Interface Builder.
Now click on the UITextView on your interface and press the delete key on your keyboard to delete it. Now, drag a UITextField from the library and drop it onto your interface. Resize it to fit. When you have completed that, your interface should look something like this:
Now we need to connect this component. Make sure it is selected and click Tools -> Connections Inspector to open up the connections inspector. Drag from the circle next to the method “Did End On Exit” to the “File’s Owner” object. The words udpateText should pop up. Click on them to make the connection. Next, click in the circle next to “New Referencing Outlet” and drag it to the “File’s Owner” object. Select todoText when it pops up. The Connections Inspector should look like this:
Now we are done with Interface Builder. Go ahead and close it. We are now able to add todos. The last thing we need to do is give the ability to delete todos from the list as well as our database. This is all done in code, and we won’t need interface builder for this.
Let’s start by adding the methods to the appDelegate to handle the deletion of todos. Open up todoAppDelegate.h and add the following code:
All we see here is a signature for the removeTodo method. Also, be sure to add a #import “Todo.h” statement to the top of this file so that we can interface with the todo objects. Now let’s implement the removeTodo method. Open up todoAppDelegate.m and add the following code:
The first line looks up the todo in the todos NSArray. It returns the index in the array of the todo to be deleted. Then, we call the deleteFromDatabase method on the todo object and then remove it from the todos array. Since the UITableView is updated via this array, it will automatically remove the todo without any additional code on our part.
Now, let’s create the removeTodo method for the todo object. We have already written the method signature in Todo.h in a previous step, so open up Todo.m and add the following code:
Remember the delete_statement variable is a static sqlite3_stmt that we declared in a previous step. First, we check to see if it is nil. If it is we compile the statement using the sqlite3_prepare statement. Next, we bind the primary key of the current todo to the “?” in the sqlite3 statement. Next, we just step the statement to execute it and reset it. The last thing we need to do to delete todos from the database is to specify what happens when the user presses the “delete” button. Open up RootViewController.m and add the following code:
The first step (like the first step of many functions) is to get a reference to the appDelegate. Next, we check to see if we are currently editing. If so, call the removeTodo method on appDelegate. The next line, removes the row from the UITableView at the given indexPath.
Now click Build and Go! You should now be able to add and delete todo items from the database. This concludes our four part series of creating a todo list for the iPhone. As always, if you have any questions feel free to leave them in the comments section. If you get lost at any time you can download the sample code here.
Happy iCoding!















94 Comments
Hi. Thanks for a great tutorial.
Could you please help me with something?
I got ‘syntax error before ‘Todo’ in the AppDelegate.h file. 2x
In the insertNewTodoIntoDatabase: method, I believe the line “static char *sql …” be “const char *sql …”. Correct me if I’m wrong.
i have a question my tableView show 3 colums ,if i want to
show 3 colum in each label in the next UI ‘how to do’
[self.todoView.todoText setText:todo.text]
[self.todoView.todoPriority setText:todo.text];
it appear that 2 label show the same lable i do not want. ^^
Dear Brandon,
Thanks so much for this tutorial! I would like to use such a program to have a dictionary on my iPhone, but I don’t know how to import the database (Excel or Acess) into xCode. I haven’t found a way to do it using SQLite or PListEdit Pro… Do you have a hint on how to do this?
Thanks!
Oliver
Thank you so much for these tutorials! A couple questions:
1. In the viewDidLoad method (in RootViewController.m), doesn’t “btn” need to be released? If not, why not?
2. Is it possible to draw a strike-through on completed items? I’ve looked through Apple’s documentation and can’t figure out how to alter the display of the text to have a strike-through.
I’ve got it running.
But when I edit or add a new todo, the text doesn’t appear in the screen that lists them. And I don’t think it’s getting saved in my database because when I reopen the app, it crashes. (Deleting the app, and database, fixes this.)
I saw the comments that mention adding an update and setting dirty for the text. But making those changes didn’t seem to fix anything.
Hi Brandon,
First, quite appreciated all your tutorials. I have learned a lot from them.
But something was weird. In addtion to the bug on updateText without “dirty = YES”, I found that, without that fix, the update still happened sometimes. That’s because the the “updatePriority” method was called when a todo item was tapped. I did set a breakpoint to trace it though. It did not happen everytime. I tested the downloaded code without modification. Can’t figure it out why.
hello i like your tutorial very much. i have a little problem
please solve my problem.
as you added custom cell i added a button on cell.now the problem is how do i delete the cell and the data from the database with that button’s click event.i am unable to figure it out how the method should i wrote
Great information, I’ve been playing around with this and it’s been very helpful. I have one question though. I’m working on an app that has a tab bar controller and the third tab is the one that makes the calls to sqlite. If I set up the third tab to be a table view controller, and essentially set up this same configuration on that tab, my tableview is not being updated until I terminate the application. I can delete a todo fine and that works, but if I add a todo or change anything in the todo, it doesn’t get updated. I’m stuck, and I feel like it’s probably something very simple.
Hey Brandon,
I have been following your tutorials on this todo list and I am having a problem. I followed through and the program builds, but it unexpectedly terminates when I try and tap any of the items. How can I find out what’s doing this is De-bugger isn’t giving em warnings?
Great tutorial. Especially the UITableViewCell part.
Thanks a lot for sharing your knowledge with us.
Hi!
great tutorial !
I found a little bug.
When i delete a todo thing and create a new one just after (not closing the app) that last todo is not save.
I remove the “return -1″ in the todo.m class in “insertNewTodoIntoDatabase”.
Then i got a warning because th function didn’t have a return, but it works.
If someone has a better way to solve the bug…
finally, it didn’t solve the bug.
So leave the return -1.
@Chris Allen
I was having the same problem. The code was throwing an exception as soon as the RootViewController tried to push the todoViewController on to the stack. After scanning a lot of code, I finally just deleted the todoViewController.xib file and regenerated it by hand.
After that the problem went away. My guess (since it worked in part 3) is that something went awry when I changed the UITextView to a UITextField and fouled up the XML underlying the view.
Thanks for the tutorial Brandon. At first I was really alienated by this language. Your tutorial helped me understand some aspects of objective-c. Btw, I’m coming from actionscript 3.0 background.
However, I noticed that when I run the completed “To do app” and check the leak by doing (Run>Start with Performance Tool>Leaks). I encounter some heaps on the leak graph.
I just want to know if it’s fine or bad for the application.
Hope for your prompt response.
and keep the tutorials coming.
more power
Hello Brandon,
First of all thank you very much for your wonderfull tutrial, because of that i was able to do the application i wanted.
But unfortunatly I having a big big issue,
Basically when i try to deploy your application(downloaded from this site) or my application in the device itself(my iphone is 2G, jailbreak and running on 2.2 firmware) i simple does not open the application on the device, while in the simulator it works fine.
I think that this problem might be related to the database…
So my question is, were you able to make your application work on a real device?
thanks a lot and hope to hear back since my whole application is based on a database.
Hello Brandon,
I just made further tests about the error i`m getting running the app on my real iphone device and received the error message found below.
It`s saying that it was not able to find my application document directory, and because of that it cannot create the database.
To install the application on my iphone(jailbreaked and i do not have iphone developer sign code yet) I generated the .app file(compiled it) send it to my //Applications/ folder in iphone via ssh and then i executed the $ ldid -S /Applications/YourApp.app/YourApp and the$ killall SpringBoard from my mac terminal.(more details can be found here http://adeem.me/blog/2009/03/29/bypass-code-signature-published-your-application-on-cydia/
After that my application just started showing on the iphone but when i run it i get the error message below.
Do yo have any ideias of what could i do to make it work?
thanks!
Jun 19 18:02:15 iphone sshd[91]: USER_PROCESS: 91 ttys000
Jun 19 18:04:21 iphone /Applications/DrillDownApp.app/DrillDownApp[95]: open on /var/mobile/Documents/todo.sqlite: No such file or directory
Jun 19 18:04:21 iphone DrillDownApp[95]: *** Assertion failure in -[eMenuAppDelegate createEditableCopyOfDatabaseIfNeeded], /Users/fnunes/Documents/eMenu/Classes/eMenuAppDelegate.m:108
Jun 19 18:04:21 iphone DrillDownApp[95]: *** Terminating app due to uncaught exception ‘NSInternalInconsistencyException’, reason: ‘Failed to create writable database file with message ‘Operation could not be completed. No such file or directory’.’
Jun 19 18:04:21 iphone DrillDownApp[95]: Stack: (\n 808221155,\n 806100816,\n 808035743,\n 812495815,\n 8899,\n 8391,\n 816116592,\n 816153144,\n 812205481,\n 807837071,\n 807834407,\n 827735648,\n 816119480,\n 816158236,\n 8343,\n 8244\n)
Jun 19 18:04:21 iphone UIKitApplication:com.yourcompany.DrillDownApp[0xe85e][95]: terminate called after throwing an instance of ‘NSException’
Jun 19 18:04:22 iphone ReportCrash[96]: Formulating crash report for process DrillDownApp[95]
Jun 19 18:04:23 iphone com.apple.launchd[1] (UIKitApplication:com.yourcompany.DrillDownApp[0xe85e][95]): Exited abnormally: Abort trap
Jun 19 18:04:23 iphone SpringBoard[39]: Application com.yourcompany.DrillDownApp activate: animated deactivate: exited abnormally with signal 6: Abort trap
Jun 19 18:04:23 iphone ReportCrash[96]: Saved crashreport to /var/mobile/Library/Logs/CrashReporter/DrillDownApp_2009-06-19-180422_iphone.plist using uid: 0 gid: 0, synthetic_euid: 501 egid: 0
Jun 19 18:04:23 iphone com.apple.SpringBoard[39]: CoreAnimation: timed out fence 2
Hello,
after I follow the tutorial, it done
but i have some question on delete and insert
for example
at now the list have four “todo” item.
now, i delete any one of them.
and restart the application (only have three item now)
but if I remove the application from the simulator
and reinstall it
I will get four “todo” item.
how can i solve this problem?
i guess it because of “commit sql” but how can i commit it?
Hi,
Nice tutorial(s)! Thanks for the same.
I would like to know if the information created in SQlite will stick around for multiple versions of the same app and across firmware/OS upgrades. Any pointers would be of immense help!
Appreciate your time!
I was wondering if there was a way to reorder list in the todo’s and have the SQL database save it?
Hey Brandon,
I found a bug in your source code that you have available for download. I can change the names of the todo items, and they will save, except for the very first one. In the case for your app, if I try to change the “Take out trash” item, and then exit the app, and then re-open it, the name doesn’t change!
Thanks for the awesome tutorial! Really helped me get started with iPhone development.
Hi Brandon,
I have been through all your tutorials on SQLite3 and others. Its all great with good explanations for those whom are new to xcode.
Im a slow learner, I can understand your other tutorials. But this one is a little harder to understand. Can you email me and tell me what the individual class suppose to do? I will try to take it from there on my own.
Thanks
Melvin
lxlionhartxl@yahoo.com.sg
Along with todo, I would like to set the target time to complete. In case, if I didn’t change the status (or not meeting the date), I want it to show alert at that particular time and date even if some other app is running at that time.
Can some one tell me how to implement such functionality like alarm….?
–satyam
good yarrrrrrrr
I am trying to add an additional UITextField. I want to use the first one as the Todo Title. And the second one to be notes about the todo. I am having trouble linking it back to data. Some clues would be appreciated.
شبكة الهنوف – صور سيارات – العاب بنات – يوتيوب – المنتدى الاسلامي – تحميل القرآن الكريم – تحميل محاضرات اسلامية – تحميل اناشيد اسلامية – منتدى الصور – صور حول العالم – العاب صور – خلفيات الشاشة – منتدى صور الاطفال – منتدى العجائب والغرائب – منتدى الخواطر – منتدى القصص و الروايات – منتدى حواء
منتدى الدوري الإماراتي – منتدى الدوري الإسباني – منتدى الدوري الإنجليزي – منتدى الدوري الإيطالي –
Hi Brandon,
but I’m back on my feet thanks to your tutorials!! Thank you!!
First of all, I want to thank and congratulate you on this great series of tutorials!! Awesome work man!!
I have to do a tiny app for work and bought 2 books (keep titles out for now) and although 1 of them is not so bad, it’s so deep and complicated to follow that almost made me quit on the project. I found your tutorials and followed them all up to the ToDo Part 1 (I have 2 through 4 to continue tomorrow
Now, a conceptual question. What’d be the best option to sync a SQLite DB with another DB in a webserver or FTP site? WOuld you have SQLite handle the sync having a MySQL on the other side? Or you’d wrap all changes since last sync in an xml or other format file and send it to “the other side” to be read and processed by a script?
I can’t find any lead on Google and my app will need exactly that. The skeleton will be similar to your “ToDo app” with some added Tabs in one of the views, but then once all is working on the iPhone, I need to send it to a “main DB” where multiple iPhones will sync with. That DB will likely to be SQLite or MySQL, and I can’t figure out what best practices for that are….any clue will be greatly appreciated.
And once again, THANK YOU!! Great tutorials!!
Thanks, Brandon.
I like following your tutorials.
They are like a magic revealed.
Regards
Shay
Nice work on this one. Problem…
If you click add, then go back without entering anything, it adds a BLANK Todo item. How can we stop this from happening?
I’ve tried changing some of the code adding if (todoText = @”") Dont do anything… But it only works half way.
Any help? Thanks in advance, and GREAT tutorials!
Hey Brandon,
Excellent tutorials! I love the way they are guiding the development and making you type in the code!
Unfortunately I have a bug in my code where an unhanded excception occurs on the line:
[self.navigationController pushViewController:self.todoView animated:YES];
in RootViewController.m
I have tried debugging it but cannot seem to find what the problem is. I have even gone as far as downloading your code (which runs) and comparing every line of the code with mine…. I can see no differences! I have also resorted to copying across your sqlite3 database and still no luck. All I can think of is that there is an error in my XIB when I deleted the textview and added the textField. I have gone over the step many times but maybe I am missing something. I have searched for any textView references that I have missed but cannot seem to find any!
I appreciate that you are probably extremely busy but is there a chance I could send you my project to see if you can work out where I am going wrong?
Cheers
AJ
Great tutorials. Learned a lot from them.
Hi people !
Thanks for this great tutorial,
I have problem ,when i try to implement delete something in my datebase (my own app), i found this error:
‘NSInternalInconsistencyException’, reason: ‘Error while creating update statement. ‘no such column: idJunta”
what can i do?
thanks!
Great tutorial! All steps are fun and worthwile to do!
Also added the change of Wil. Now the todo-Text is also saved.
Merry X-Mas!
Hey Brandon,
First of all, I’d like to thank you for these awesome tutorials! I’ve started working with the iPhone about two months ago, and i can tell it is much easier with your tutorials!
But i’m still a rookie (no wonders:) and i’d need a little advice.
I made a function for posting the todos to an online database. It’s called when a button is pressed on the toolbar i added. It looks like this:
- (void) send:(id)sender {
//NSString * udid = [[UIDevice currentDevice] uniqueIdentifier];
todoAppDelegate *appDelegate = (todoAppDelegate *)[[UIApplication sharedApplication] delegate];
unsigned count = [appDelegate.todos count];
Todo *todo;
while (count–) {
todo = (Todo *)[appDelegate.todos objectAtIndex:count];
NSData *returnData;
NSURLResponse *response;
NSError *error;
NSString *url = [NSString stringWithFormat:@"http://some.php?text=%@&priority=%i&status=%i",
todo.text,todo.priority,todo.status];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
}
}
But it only sends the first todo in the array of todos, and i don’t have a clue what did i do wrong. Could you help me out with this one?
Thanks very much in advance!
Árpi
File todo.m, property “text” is “retained”. Doesn’t that leak? I can’t see a release.
I have an application I created after working through this tutorial. It is similar to this app, but has a few more text fields.
The interesting thing is that when I go to edit one of the items in my list of items, I click it, it takes me to the item view and shows all the values for that item, but when I change the value of one of the fields, say the date, it never updates on the original tableview on the main screen. I set up some NSLogs in the debugger console and saw something interesting:
My Code:
- (IBAction)updateText:(id) sender {
NSLog(@”%@”,self.milemarker.logdate);
NSLog(@”%@”,self.milemarkerLogDate.text);
self.milemarker.logdate = self.milemarkerLogDate.text;
NSLog(@”%@”,self.milemarker.logdate);
NSLog(@”%@”,self.milemarkerLogDate.text);
[self.milemarker updateText];
}
THe debugger output:
2010-02-04 17:51:41.602 MileMarker[2754:207] (null)
2010-02-04 17:51:41.603 MileMarker[2754:207] 11-11-1111
2010-02-04 17:51:41.604 MileMarker[2754:207] (null)
2010-02-04 17:51:41.605 MileMarker[2754:207] 11-11-1111
I would expect to see the first line that is null to have the CURRENT value from the date textfield in my app, I would expect the second null to have 11-11-1111 in it due to the assignment I am making (self.milemarker.logdate = self.milemarkerLogDate.text;).
Any ideas why I am not picking up my current values? When I put a breakpoint in it says out of scope for self.milemarker.logdate
I know I am not showing all of the details here, but if anyone needs to see any of the source code files, I will be glad to provide them. This one has me scratching my head since I followed the examples in Brandon’s demo very closely.
I probably missed something simple though…
Thanks
Jeff
When I put the same breakpoint in this tutorial’s code, here is what I get, the expected behavior:
- (IBAction) updateText:(id) sender {
NSLog(@”%@”,self.todo.text);
NSLog(@”%@”,self.todoText.text);
self.todo.text = self.todoText.text;
NSLog(@”%@”,self.todo.text);
NSLog(@”%@”,self.todoText.text);
[self.todo updateText];
}
Debugger Console output:
2010-02-04 21:35:02.545 todo[3326:207] DIGG this tutorial
2010-02-04 21:35:02.547 todo[3326:207] REDDIT this tutorial
2010-02-04 21:35:02.548 todo[3326:207] REDDIT this tutorial
2010-02-04 21:35:02.548 todo[3326:207] REDDIT this tutorial
Interesting?
Hi Brandon,
How do you hide the keyboard when you are ready to save the new note??
HEY BRNDON,THE SOURCE CODE DOWNLOAD LINK IS NOT WORKING!!
PLZ HAVE A LOOK….
for updating the todo list after editing a TODO item we can override viewWillAppear to be like this:
- (void)viewWillAppear:(BOOL)animated {
[self.tableView reloadData];
[super viewWillAppear:animated];
}
I had the issue mentioned in other posts in which the updateText method was not being called. I fixed my code by copying the code within updateText and pasting into viewWillDisapper (of TodoViewController.m). This guarantees the text field to be updated when you navigate back to RootViewController.
Great set of tutorials!
Also, for iOS4, you might need to add:
[todos makeObjectsPerformSelector:@selector(dehydrate)];to applicationDidEnterBackground in TodoAppDelegate.m.
Great tutorial! Thanks! I used the idea in my app I am working on now. I have problems with icon badge number though. In appDelegate.m in section applicationDidFinishLaunching: I inserted code
int rows = [coffeeArray count];
[UIApplication sharedApplication].applicationIconBadgeNumber = rows;
- it works fine until the point the UITableview is empty. The badge number still shows 1. How do I manage the code to display on the icon how many ToDo’s (rows in UITableView) I have correctly? Thank you for advices!
One Trackback
[...] iPhone Programming Tutorial – Creating a ToDo List Using SQLite Part 4 | iCodeBlogTutorial de cómo crear un programa "To Do" para iPhone usando SQLite parte 4. [...]