Thursday, April 30, 2009

The UIView class and the View XIB

One of the harder concepts to absorb when first learning the iPhone SDK is the "directionality" and control flow of UIView objects and UIViewController objects. Both can be created using the Interface Builder and both can be created entirely from code. I can't possibly go into all of that right now, but let's just say that if you stick to one technique or another, you'll "get it" pretty quickly but you won't be able to solve all the problems that arise.

One such problem is when you need to programmatically change a View (such as, refreshing 2D graphics using Quartz) but you also want a XIB for that view, making it easier to embed standard controls. I found a great answer to this question at this Web site.

In short, you create both a custom View (derived from UIView) then you point the XIB's View's Identity to that custom class. When you refresh graphics using drawRect(), it refreshes right onto the View you created with Interface Builder. Refer to the above link for code samples.

Monday, April 27, 2009

Forgetting Regular Expressions

It has been quite some time since I've used Regular Expressions. I've never been such an expert that I could make code practically wash dishes using just one line, but I used to be able to do some pretty cool tricks with minimal coding effort.

Coming to the rescue is Rubular: "A Ruby Regular Expression Editor". This is a very handy Web-based tool that allows you to enter an expression and to provide a sample string. It's all coming back to me...

Friday, April 24, 2009

Redirecting a database update using Rails

I learned a handy trick today.

Rails contains a convenience method that allows you to redirect page requests (or submissions) called "redirect_to". Rails also contains a submission method called "PUT" in addition to the "GET" and "POST" supported by HTTP.

When you want to update a record in the database, the technique for doing so is to establish that the submission type of the update method is a "PUT". However, if you want to create a delegate method in between the submission and the actual deed, you need to "POST" to your delegate method then have that method send it's redirection using "PUT".

This is *not obvious* and the solution is best explained by this blog. I've tried it and it works.

Tuesday, April 21, 2009

Using Rails scaffolding

A handy tool that comes with Rails is its ability to create fully functional, dynamic Web sites based on the database schema. Knowing that there are standard CRUD operations, scaffolding creates the controllers and helper code you need to make a site work (without all the branding and AJAX, of course...)

A problem I experienced was this error message:

The name 'TasksHelper' is either already used in your application or reserved by Ruby on Rails.

A little Googling revealed that this is a "bug" that appeared in recent Rails releases (I'm currently using v2.2). Here is a link to the most useful post I found. Pay particular attention to the poster who says to rename the helper files. It's kludgy but better than creating all that code manually.

Tuesday, April 7, 2009

Kudos to Apple

Sometimes things just click... and it's really nice when they do. I needed to set up my MacPro (Leopard) as a development machine running Ruby on Rails and MySQL. At first I estimated 2 to 4 hours.

I had already installed MySQL plus a GUI front end called CocoaMySQL (Note that Google has since purchased CocoaMySQL and renamed its successors to SequelPro. This is good news for Mac heads like me because it shows a growing commitment to both MySQL and Macs.) so I wasn't worried about that.

I guess I didn't read the release notes too carefully, but Ruby and Rails are both pre-installed on Leopard. I opened a terminal window and typed "ruby --version" and "rails --version" and got the expected responses.

Auto-generating a Rails project seems to point ActiveRecord to the SQLite database engine, but editing the database.yml file is a simple thing to correct.

That was it! Even a developer environment using a local Web server "just worked"! I think the whole process took me less than 20 minutes and that's because I spent most of that time incredulous at how easy it was... Kudos to Apple.

Using Delegates in Objective-C

I was following a tutorial on implementing UIPickerViews. For those who have seen or used an iPhone, it's the control that looks like a collection of spinning wheels. The approach Apple takes with the implementation of that particular control is that you must specify a Delegate and a Datasource for the control to function properly. In code, it looks like this:
@interface MyController : UIViewController <UIPickerViewDelegate, UIPickerViewDataSource> { ... } @end

The tutorial continued to walk me through NIB creation and, most importantly, connecting the NIB objects to the class. Spoiler alert: What I'm writing about is that I failed to realize three important concepts:

  1. the delegate and datasource declarations were standard components of the framework,
  2. those declarations provide a clue to the NIB that you can "control-drag" to specify the delegate class,
  3. you must "override" certain methods to guarantee that the code compiles properly

When I was running through the tutorial, I was under the preconception that the delegate class the authors were referring to was a *custom* class so I was already behind the 8-ball on point #1. I got point #2 easily enough but then failed to understand where the methods in point #3 came from. This is definitely one of those "you had to have been there" moments. I'm sure you're yawning by now...

Suffice it to say that I found my answer when I looked up UIPickerViewDataSource and UIPickerDelegate in the Apple documentation. The instance methods described should be overridden with custom treatment.

Friday, April 3, 2009

"Privatizing" your WHOIS record

When you register a domain for your own personal use, you are asked to provide your home address, phone number, and email address. Unless you have a valid yet anonymous identity most of us would choose to use our (truthful) personal information.

This is problematic because the WHOIS database can be scrubbed for such information and used for telemarketing purposes... or worse. Many domain hosting sites allow you to anonymize your information (for a fee) which is my recommended course of action.

When transferring a domain, however, be sure that you turn this off. Otherwise, the transfer authentication emails go to some bogus email address and restarting the process can be more effort than the initial transfer request. I have learned this from experience... I'll keep you posted.

Free UML modeler for the Mac -- ArgoUML

For those of us who are "Windows-challenged", being Mac users, there are few alternatives to Microsoft's Visio. Two very good open-source applications are StarUML and ArgoUML. The former, however, is not available for the Mac. Too bad. For you Windows users looking for UML depiction that is superior to Visio, I encourage you to check it out because it actually is a pretty good tool.

For those of you who are Enterprise-minded (e.g., you have the budget or you need Visio-compatibility) you may want to check out ConceptDraw. I haven't used it, but it appears to be more fully featured than ArgoUML and therefore very similar to Visio, which is targeted to a larger audience than just those interested in UML.

I've used ArgoUML for 2 hours now to create Class Diagrams. I have only experienced a couple of annoyances so far but those were not deal killers given the (did I say free?) price.

First, annotation tools are too basic. If there is a way to create a generic arrow (as opposed to an Association) I couldn't find it. With a growing trend towards MVC you'd think there would be a way to incorporate visual elements into a Class Diagram. For example, you can't insert a picture (screenshot, icon, etc.)

Second, there is no copy/paste functionality... for anything! If you have 5 similar Operations, forget about copying, pasting, and editing the differences. You have to create each one. This is most annoying with annotations.

GDB: Program Loaded.

This is the cryptic error I see when I forget to create a connection between a NIB object (say, a UIView object) and the associated class. This was difficult to track down because the application compiles and runs, but the moment you interact with the interface, it goes into a locked state with this (very uninformative) message.

Transferring Domains

Increased awareness of the value, and advantages, of personal branding has led many to create their own Web sites. It is easiest to use MySpace or Facebook but I'm a little more old school and decided to have my own domain and to create my own Web pages. Once that choice is out of the way, the question becomes, "how do I my manage my domain?"

I originally created a domain for a friend using Yahoo's Business services. However, I quickly discovered that the Yahoo Web site is a labyrinth that requires a good memory or strategic use of bookmarks. Heaven help you if you lose the bookmark. While I give credit to Yahoo for being a leader with such types of offerings, their Web site is horribly outdated and is not prone to revisions or improvements once deployed to the public.

I next created my own domain using old, yet trustworthy, Network Solutions. I hearken from the old days when NS didn't offer hosted Web sites or e-commerce solutions so one can imagine my surprise when I discovered that I can not only manage multiple domains using NS but that they satisfy *most* of my Internet needs. I created an e-commerce solution for another friend and became familiar with their interface and shopping cart tools. While the NS administrative site could use some polish, it's organized and easy to navigate. Kudos.

Transferring my domain managed by Yahoo to Network Solutions was a piece of cake. In the NS Web site, in a menu off the home page, is Transfer Domain Names. You enter one or more names and provide an "authorization code". To get that code, open your Yahoo Domain Control Panel and click on "View your authorization code". The resulting page includes instructions for transferring the domain as well as the authorization code you'll need. You pay $19 for the honor but when all is said and done, your domain will be in NS's much-easier-to-use domain management interface.