Hour 6. Exploring Messaging and a Testbed App


What You’ll Learn in This Hour

Image Setting up a testbed app

Image Creating a branch to test with

Image Sending a message to an interface element


Setting Up the Testbed Apps

Now that you have a high-level understanding of Objective-C and the development environment, this part of the book helps you write and run code so that you can explore exactly how things work. Because this is no longer a world in which you can write a few lines of Hello World code to test things out, you need an app that you can experiment with. Fortunately, it is easy to create one, and that is the first thing to do in this hour.

It’s a good thing that it’s so easy to do because you’ll create two testbed apps—one for iOS and one for OS X. Objective-C is the same on both operating systems. Cocoa on OS X and Cocoa Touch on iOS are very similar, but they are different. So in order to have a testbed app ready for testing, you need to either create both of them or pick the one that you’re most interested in. For many people, it is easier to implement just one of these apps and then come back to the other one when they want to explore it.

Before starting, however, remember that you are creating an app that contains thousands of lines of code. Your fingers will not get sore, though, because that code is packaged into the various frameworks that you use in your app. Both of the testbed apps are very simple: They put up a window.

This is a very simple task, but it is the prototype for a large number of tasks that you need to perform. If you want to experiment with setting the color of an object, add it to your window and write the coloring code. Then put that app aside and experiment with changing the font in a text field. Once you have the testbed app in place, you’ll have a platform for exploring the Objective-C syntax in the rest of this book.

One easy way to reuse your testbed app is to use a Git source code repository, which, as you saw in Hour 4, “Using Xcode 5,” you can create and manage through Xcode. You can create the basic project that just puts up the empty window on OS X or iOS. Then, each time you want to experiment with something, just create a new branch to test that code out. In this case, for example, the first branch is the branch that adds a text field and colors the text blue. Later on, you can add other branches with other adventures. The technical term for such a base for experimentation is testbed, and that is what this project is called.

Adding a Text Field and Connecting It to Your Code

The first testbed experiment you’ll conduct involves coloring the text in a text field. To do so, you need to create a text field in the interface and connect it to your code. The techniques are comparable but slightly different on iOS and OS X. The text field is a type of view on both iOS and OS X. All that you need to know at this point is that views are contained within windows. There are really two parts to an interface element such as a text field:

Image There is a graphical representation of the text field in the interface that you can move around with the graphical Interface Builder editor.

Image There is a code object that you manipulate with Objective-C code.


Windows and Views in Cocoa and Cocoa Touch

On iOS devices, there is a single window for an app and there is no menu bar. On OS X, you can have multiple windows. In both environments, you can place views inside windows; you also can create a hierarchy of views within views. The topmost view in an OS X view hierarchy is located within a window. The topmost view in an iOS view hierarchy is located within the app’s single window. You normally don’t deal with the app window in iOS. All this means is that when you start with a blank canvas in either environment, that blank canvas represents the app’s single window on iOS or one of the several possible windows on OS X. You then add views to the window. It’s really very much the same in both environments, but on iOS the window is generally not mentioned because there is only one.


Adding the Text Field

The first task is to add the text field on iOS and/or OS X.

Now that you have the text field added to the window, you need a way to reference it in your code. With Xcode 5 and the declared properties that were added to Objective-C 2.0, the process is very simple. Once again, you are not writing code; instead, you are using a graphical editor. But do not worry; as soon as the text field is added to the project, you begin typing away to change the color of the text with your first Objective-C code.

Connecting the Text Field to the Code

The other part of this task is to connect the text field to your Objective-C code.

Sending a Message to the Text Field

Now that the property myTextField is linked to the interface element, you can send a message to it. This might seem like a lot of setup but, in fact, after you have been through the sequence of adding an interface element, connecting it to your code, and automatically generating the property, you will be able to do it in a matter of seconds. After you have the connection made, you can use the property to communicate with the object.

Remember that in Objective-C you are not calling methods of objects; rather you are sending messages to the objects. The structure of the message is basically the following:

[receiverName message];

You have the first part of the code: receiverName is myTextField. It is a property of the class in which it is declared, and you need to reference it as self.myTextField.

GO TO Image Hour 9, “Declaring Properties in an Interface File,” p. 127, for more information on working with properties of a class.

The brackets are essential for a message, so the code starts to look like this:

[self.myTextField message];

Where do you find the messages that can be sent to a text field? Highlight UITextField in the property declaration that was created for you. If necessary, show the utility area and choose Quick Help at the top to see the documentation (see Figure 6.17).

Image

FIGURE 6.17 Show Quick Help for UITextField.

From there, you can click on the class reference and see what messages are available to you (see Figure 6.18).

Image

FIGURE 6.18 Browse the available messages.


Note

As the messages are part of the frameworks, you should explore them on your own because you will need them as you write your code. This is outside the scope of this book, which focuses on the Objective-C language and not the frameworks.


To set the color, you need an NSColor (OS X) or UIColor (iOS). You can use the class method blueColor (or yellowColor or blackColor and so forth) to get a color. The basic code is as follows:

[NSColor blueColor];

Remember, that for a class method, the recipient is the class and not an instance of the class. Putting it all together, you can add the line of code shown in Listing 6.1 to ViewController.m for iOS.

LISTING 6.1 Set a Color Using a Color from a Class Method on iOS


- (void)viewDidLoad
{
    [super viewDidLoad];
  // Do any additional setup after loading the view, typically from a nib.

  [self.myTextField setTextColor: [UIColor blueColor]];
}


On OS X, add the code to AppDelegate.m, as shown in Listing 6.2.

LISTING 6.2 Set a Color Using a Color from a Class Method


– (void)applicationDidFinishLaunching:
    (NSNotification *)aNotification
{
  // Insert code here to initialize your application
  [self.myTextField setTextColor: [NSColor blueColor]];
}


Build and test the app, and you see that the text is blue or whatever other color you have chosen.

Reviewing the Message Syntax

Messaging is at the heart of Objective-C, so it is worthwhile to review the syntax involved—the Objective-C syntax as well as the English-language syntax used to discuss it. Here is a quick review of the syntax and what happens:

Image Messages are enclosed in square brackets. At a minimum, they specify a recipient and a message. The message specifies the method the recipient should perform.

Image The recipient can be a class or an instance of a class. Methods are declared differently if they are class or instance methods.

Image Methods can return values. Thus, you can use a class method to return a specific color:

Click here to view code image

myColor = [UIColor textColor];

Image An instance method may return a value; often it is a property:

Click here to view code image

myColor = [self.myTextField textColor];

Image A message may take arguments that are preceded by a colon and identifier:

Click here to view code image

[self.myTextField textColor = myColor;]

Image A message can have multiple arguments. Each is preceded by a colon and an identifier:

Click here to view code image

[self.myTextField insertSubview:myView aboveSubView:baseview];

Note that this is actually a method on UIView; because UITextField is a subclass, it inherits this method.

Image Instead of returning a value, a message can simply perform an action and return nothing:

[myTextField myMessage];

Image The selection of a specific method to execute occurs at runtime. It is based on the recipient as well as on the method specified in the message. This enables you to write code such as the following. Because id refers to any type, from the code there is no way of knowing what type of object will be asked to respond to the message:

id myObject;
[myObject copy];

Because you may not know what type of object will be responding, you should be very careful with syntax like this. At the least, use respondsToSelector: to find out if you will crash. (respondsToSelector: is discussed in Hour 12, “Routing Messages with Selectors.”)

Summary

This hour extended the discussion of messaging from Hour 3, “Using Object-Oriented Features in Objective-C.” You have built a testbed to use for experiments and testing, and you have built up a very simple message to manipulate an interface element.

Q&A

Q. What is the point of a testbed project?

A. You can create the shell of a project and then do your testing in separate branches, which can be isolated from the rest of the testbed. In other words, you minimize the chances that one of your experiments breaks the whole testbed.

Q. How do you call a method?

A. You send a message to an instance of the class or, in the case of class methods, to the class itself.

Workshop

Quiz

1. When do you use the Assistant Editor?

2. How do you add an interface element to your interface with Interface Builder?

Quiz Answers

1. Use the Assistant Editor when you need to view two files at once. For example, if you need to control-drag from an interface object to a header file, you need both files open at the same time.

2. Find the object you want to add in the object library and drag it onto the Interface Builder canvas.

Activities

If you have not done so already, modify the code in Listing 6.1 to change the text color in the text field. Use Quick Help in the Organizer to review the UITextField class reference and modify the code to change other attributes of the text field.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.226.96.155