Delphi to Cocoa

July 9, 2009

Anatomy of an iPhone Application Window

Filed under: conceptual,iPhone,objects — rithban @ 12:46 am
Tags: , , , ,

An iPhone application window doesn’t have the convenient TForm found in the Delphi VCL. This article explains the constituent parts.

Frame

A frame is a rectangular region of pixels on the screen. It is represented by a CGRect struct, analogous to the Delphi TRect.

More details can be found here.

Screen Area

iPhone applications typically use most of the screen. One can get the frame for the entire screen by:

CGRect frame = [[UIScreen mainScreen] bounds];

However, this includes the status bar. The application frame, which excludes the status bar, by:

CGRect frame = [[UIScreen mainScreen] applicationFrame];

Window

A window is the low-level support needed to make use of a frame. In this context, a frame is nothing more than an idea but a window is an actual object instance that can be put to use. By itself a window isn’t very useful, but it’s the object upon which a GUI can be built.

When instantiating a window object, you provide a frame to define its boundaries. Note that the origin of the window’s frame is an offset relative to the screen.

UIWindow window = [[UIWindow alloc] initWithFrame: frame];

View

A view is the base visual component, from which visual components are built. It is conceptually analogous to the Delphi TControl.

As with a window, we use a frame to define the size of the view. Note that the origin of the view’s frame is an offset relative to the window.

UIView view = [[UIView alloc] initWithFrame: frame];

The Delphi TForm

The old Delphi TForm class is conceptually a window and view paired together. You could manually instantiate a TForm class and make it visible by

lForm := TForm.Create(nil);
lForm.Show;

The rough iPhone equivalent consists of the following steps.

  1. Get the application frame
  2. Instantiate a UIWindow using that frame.
  3. Set the frame’s origin to (0,0).
  4. Instantiate a UIView object using the modified frame.
  5. Add the view to the window.
  6. Cause the window to be shown.

Thus, one could use code similar to this:

    // Get size of application frame
    CGRect frame = [[UIScreen mainScreen] applicationFrame];
    
    // Create main window
    UIWindow window = [[UIWindow alloc] initWithFrame: frame]; 
    
    // Create main view for window
    frame.origin.y = 0.0;
    UIView view = [[UIView alloc] initWithFrame: frame];
    
    // Attach view to window
    [window addSubview: view];

    // TForm.Show
    [window makeKeyAndVisible];
Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: