The Art of Programming Without Programming

As I have been working on Debtor, my iOS app, I’ve come to realize that half of the work of programming is done away from the computer.

It’s a cycle, and it goes like this:

I spend many minutes, perhaps hours, coding and reviewing API documentation to build my application. I get stuck on a certain problem, and all the documentation, googling, and fiddling around in the code is not going to solve the issue. I need to stop and THINK.

So I step away from the computer, and go do something else.

While I am away, my programming problem is still there in the back of my mind, and if I’m not actively engaged in some other activity, I will go back and think of different ways I could possibly solve it. Meanwhile I’m in the shower, walking up the stairs, driving in my car, whatever. And then all of a sudden, a solution will come to me.

I can’t wait to get back to my computer to try out my proposed solution. Often times it is the correct solution, and I just needed time to process the problem in my mind before I could find a solution.

So if you find yourself stuck on an issue with your code, and you don’t know how to solve it, just walk away, find something else to do, and your brain will continue to process your problem, and solve it for you while you go about your daily business.

Concrete example:

Problem: I have a tableView with a textField inside each cell. I need to find a way to update the data source for my table with the data entered in the textField. How does the textField know which cell it is in? Do I have to keep a separate array around to keep track of the textFields? Should I just loop through the tableView cells when I really need the data later, or should I capture the data as the user enters it?

Solution: Go take a shower. In the shower, I remembered that each view can get an array of subviews, so there must be a way to get a superview of each view as well!

UIView * parentView = textField.superview;

This gets the parent view of the textField. Then cast the UIView to a UITableViewCell, which I know it is, because that is the only thing that has a textField in my class. The text field delegate method

-(void)textFieldDidEndEditing:(UITextField *)textField

is the perfect place to get the textField’s superview, because at that point the user is done editing the textField. I can grab the text in the textField at that time, and update the proper array element in the table’s data source. But then how do I get the array index that I need to update? From the UITableViewCell, which is the superview of the textField, I can get the index path from the table view by sending it the indexPathForCell:(UITableViewCell *)cell message. From the index path, I can get the row, and send the data source array the objectAtIndex:[indexPath row] message to get the correct entry in my data source array. Then I just update the data source with the textField.text, and my data and view are in sync!

Advertisements

Author: barryjbro

A gamer since 1986. Photographer. iOS Developer. Web Developer and Designer.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s