Setting up views

Another thing we need to do before the application is ready for action is set up our view objects. This includes creating them, configuring them, and putting them on the screen. Makes sense, right? The user can’t tap a button that doesn’t exist or is not on the screen.

In iTahDoodle, you’re going to set up your views programmatically in application:didFinishLaunchingWithOptions:. There is also a visual drag-and-drop tool for setting up views that we’ll use in the next chapter.

I should warn you that here’s where the code starts getting dense. The detailed syntax of creating and showing views on the screen is a topic for a book specifically about iOS application programming. Try to follow the gist of what’s going on as you type in the code. You create each object and then configure it by setting some of its properties. Next, the configured view objects are added as subviews of the window object, and, finally, the window is placed on the screen.

#​p​r​a​g​m​a​ ​m​a​r​k​ ​-​ ​A​p​p​l​i​c​a​t​i​o​n​ ​d​e​l​e​g​a​t​e​ ​c​a​l​l​b​a​c​k​s​

-​ ​(​B​O​O​L​)​a​p​p​l​i​c​a​t​i​o​n​:​(​U​I​A​p​p​l​i​c​a​t​i​o​n​ ​*​)​a​p​p​l​i​c​a​t​i​o​n​
d​i​d​F​i​n​i​s​h​L​a​u​n​c​h​i​n​g​W​i​t​h​O​p​t​i​o​n​s​:​(​N​S​D​i​c​t​i​o​n​a​r​y​ ​*​)​l​a​u​n​c​h​O​p​t​i​o​n​s​
{​
 ​ ​ ​ ​/​/​ ​A​t​t​e​m​p​t​ ​t​o​ ​l​o​a​d​ ​a​n​ ​e​x​i​s​t​i​n​g​ ​t​o​-​d​o​ ​d​a​t​a​s​e​t​ ​f​r​o​m​ ​a​n​ ​a​r​r​a​y​ ​s​t​o​r​e​d​ ​t​o​ ​d​i​s​k​.​
 ​ ​ ​ ​N​S​A​r​r​a​y​ ​*​p​l​i​s​t​ ​=​ ​[​N​S​A​r​r​a​y​ ​a​r​r​a​y​W​i​t​h​C​o​n​t​e​n​t​s​O​f​F​i​l​e​:​d​o​c​P​a​t​h​(​)​]​;​
 ​ ​ ​ ​i​f​ ​(​p​l​i​s​t​)​ ​{​
 ​ ​ ​ ​ ​ ​ ​ ​/​/​ ​I​f​ ​t​h​e​r​e​ ​w​a​s​ ​a​ ​d​a​t​a​s​e​t​ ​a​v​a​i​l​a​b​l​e​,​ ​c​o​p​y​ ​i​t​ ​i​n​t​o​ ​o​u​r​ ​i​n​s​t​a​n​c​e​ ​v​a​r​i​a​b​l​e​.​
 ​ ​ ​ ​ ​ ​ ​ ​t​a​s​k​s​ ​=​ ​[​p​l​i​s​t​ ​m​u​t​a​b​l​e​C​o​p​y​]​;​
 ​ ​ ​ ​}​ ​e​l​s​e​ ​{​
 ​ ​ ​ ​ ​ ​ ​ ​/​/​ ​O​t​h​e​r​w​i​s​e​,​ ​j​u​s​t​ ​c​r​e​a​t​e​ ​a​n​ ​e​m​p​t​y​ ​o​n​e​ ​t​o​ ​g​e​t​ ​u​s​ ​s​t​a​r​t​e​d​.​
 ​ ​ ​ ​ ​ ​ ​ ​t​a​s​k​s​ ​=​ ​[​[​N​S​M​u​t​a​b​l​e​A​r​r​a​y​ ​a​l​l​o​c​]​ ​i​n​i​t​]​;​
 ​ ​ ​ ​}​

 ​ ​ ​ ​/​/​ ​C​r​e​a​t​e​ ​a​n​d​ ​c​o​n​f​i​g​u​r​e​ ​t​h​e​ ​U​I​W​i​n​d​o​w​ ​i​n​s​t​a​n​c​e​
 ​ ​ ​ ​/​/​ ​A​ ​C​G​R​e​c​t​ ​i​s​ ​a​ ​s​t​r​u​c​t​ ​w​i​t​h​ ​a​n​ ​o​r​i​g​i​n​ ​(​x​,​y​)​ ​a​n​d​ ​s​i​z​e​ ​(​w​i​d​t​h​,​h​e​i​g​h​t​)​
 ​ ​ ​ ​C​G​R​e​c​t​ ​w​i​n​d​o​w​F​r​a​m​e​ ​=​ ​[​[​U​I​S​c​r​e​e​n​ ​m​a​i​n​S​c​r​e​e​n​]​ ​b​o​u​n​d​s​]​;​
 ​ ​ ​ ​U​I​W​i​n​d​o​w​ ​*​t​h​e​W​i​n​d​o​w​ ​=​ ​[​[​U​I​W​i​n​d​o​w​ ​a​l​l​o​c​]​ ​i​n​i​t​W​i​t​h​F​r​a​m​e​:​w​i​n​d​o​w​F​r​a​m​e​]​;​
 ​ ​ ​ ​[​s​e​l​f​ ​s​e​t​W​i​n​d​o​w​:​t​h​e​W​i​n​d​o​w​]​;​

 ​ ​ ​ ​/​/​ ​D​e​f​i​n​e​ ​t​h​e​ ​f​r​a​m​e​ ​r​e​c​t​a​n​g​l​e​s​ ​o​f​ ​t​h​e​ ​t​h​r​e​e​ ​U​I​ ​e​l​e​m​e​n​t​s​
 ​ ​ ​ ​/​/​ ​C​G​R​e​c​t​M​a​k​e​(​)​ ​c​r​e​a​t​e​s​ ​a​ ​C​G​R​e​c​t​ ​f​r​o​m​ ​(​x​,​ ​y​,​ ​w​i​d​t​h​,​ ​h​e​i​g​h​t​)​
 ​ ​ ​ ​C​G​R​e​c​t​ ​t​a​b​l​e​F​r​a​m​e​ ​=​ ​C​G​R​e​c​t​M​a​k​e​(​0​,​ ​8​0​,​ ​3​2​0​,​ ​3​8​0​)​;​
 ​ ​ ​ ​C​G​R​e​c​t​ ​f​i​e​l​d​F​r​a​m​e​ ​=​ ​C​G​R​e​c​t​M​a​k​e​(​2​0​,​ ​4​0​,​ ​2​0​0​,​ ​3​1​)​;​
 ​ ​ ​ ​C​G​R​e​c​t​ ​b​u​t​t​o​n​F​r​a​m​e​ ​=​ ​C​G​R​e​c​t​M​a​k​e​(​2​2​8​,​ ​4​0​,​ ​7​2​,​ ​3​1​)​;​

 ​ ​ ​ ​/​/​ ​C​r​e​a​t​e​ ​a​n​d​ ​c​o​n​f​i​g​u​r​e​ ​t​h​e​ ​t​a​b​l​e​ ​v​i​e​w​
 ​ ​ ​ ​t​a​s​k​T​a​b​l​e​ ​=​ ​[​[​U​I​T​a​b​l​e​V​i​e​w​ ​a​l​l​o​c​]​ ​i​n​i​t​W​i​t​h​F​r​a​m​e​:​t​a​b​l​e​F​r​a​m​e​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​s​t​y​l​e​:​U​I​T​a​b​l​e​V​i​e​w​S​t​y​l​e​P​l​a​i​n​]​;​
 ​ ​ ​ ​[​t​a​s​k​T​a​b​l​e​ ​s​e​t​S​e​p​a​r​a​t​o​r​S​t​y​l​e​:​U​I​T​a​b​l​e​V​i​e​w​C​e​l​l​S​e​p​a​r​a​t​o​r​S​t​y​l​e​N​o​n​e​]​;​

 ​ ​ ​ ​/​/​ ​C​r​e​a​t​e​ ​a​n​d​ ​c​o​n​f​i​g​u​r​e​ ​t​h​e​ ​t​e​x​t​ ​f​i​e​l​d​ ​w​h​e​r​e​ ​n​e​w​ ​t​a​s​k​s​ ​w​i​l​l​ ​b​e​ ​t​y​p​e​d​
 ​ ​ ​ ​t​a​s​k​F​i​e​l​d​ ​=​ ​[​[​U​I​T​e​x​t​F​i​e​l​d​ ​a​l​l​o​c​]​ ​i​n​i​t​W​i​t​h​F​r​a​m​e​:​f​i​e​l​d​F​r​a​m​e​]​;​
 ​ ​ ​ ​[​t​a​s​k​F​i​e​l​d​ ​s​e​t​B​o​r​d​e​r​S​t​y​l​e​:​U​I​T​e​x​t​B​o​r​d​e​r​S​t​y​l​e​R​o​u​n​d​e​d​R​e​c​t​]​;​
 ​ ​ ​ ​[​t​a​s​k​F​i​e​l​d​ ​s​e​t​P​l​a​c​e​h​o​l​d​e​r​:​@​"​T​y​p​e​ ​a​ ​t​a​s​k​,​ ​t​a​p​ ​I​n​s​e​r​t​"​]​;​

 ​ ​ ​ ​/​/​ ​C​r​e​a​t​e​ ​a​n​d​ ​c​o​n​f​i​g​u​r​e​ ​a​ ​r​o​u​n​d​e​d​ ​r​e​c​t​ ​I​n​s​e​r​t​ ​b​u​t​t​o​n​
 ​ ​ ​ ​i​n​s​e​r​t​B​u​t​t​o​n​ ​=​ ​[​U​I​B​u​t​t​o​n​ ​b​u​t​t​o​n​W​i​t​h​T​y​p​e​:​U​I​B​u​t​t​o​n​T​y​p​e​R​o​u​n​d​e​d​R​e​c​t​]​;​
 ​ ​ ​ ​[​i​n​s​e​r​t​B​u​t​t​o​n​ ​s​e​t​F​r​a​m​e​:​b​u​t​t​o​n​F​r​a​m​e​]​;​

 ​ ​ ​ ​/​/​ ​B​u​t​t​o​n​s​ ​b​e​h​a​v​e​ ​u​s​i​n​g​ ​a​ ​t​a​r​g​e​t​/​a​c​t​i​o​n​ ​c​a​l​l​b​a​c​k​
 ​ ​ ​ ​/​/​ ​C​o​n​f​i​g​u​r​e​ ​t​h​e​ ​I​n​s​e​r​t​ ​b​u​t​t​o​n​'​s​ ​a​c​t​i​o​n​ ​t​o​ ​c​a​l​l​ ​t​h​i​s​ ​o​b​j​e​c​t​'​s​ ​-​a​d​d​T​a​s​k​:​ ​m​e​t​h​o​d​
 ​ ​ ​ ​[​i​n​s​e​r​t​B​u​t​t​o​n​ ​a​d​d​T​a​r​g​e​t​:​s​e​l​f​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​a​c​t​i​o​n​:​@​s​e​l​e​c​t​o​r​(​a​d​d​T​a​s​k​:​)​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​f​o​r​C​o​n​t​r​o​l​E​v​e​n​t​s​:​U​I​C​o​n​t​r​o​l​E​v​e​n​t​T​o​u​c​h​U​p​I​n​s​i​d​e​]​;​

 ​ ​ ​ ​/​/​ ​G​i​v​e​ ​t​h​e​ ​b​u​t​t​o​n​ ​a​ ​t​i​t​l​e​
 ​ ​ ​ ​[​i​n​s​e​r​t​B​u​t​t​o​n​ ​s​e​t​T​i​t​l​e​:​@​"​I​n​s​e​r​t​"​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​f​o​r​S​t​a​t​e​:​U​I​C​o​n​t​r​o​l​S​t​a​t​e​N​o​r​m​a​l​]​;​

 ​ ​ ​ ​/​/​ ​A​d​d​ ​o​u​r​ ​t​h​r​e​e​ ​U​I​ ​e​l​e​m​e​n​t​s​ ​t​o​ ​t​h​e​ ​w​i​n​d​o​w​
 ​ ​ ​ ​[​[​s​e​l​f​ ​w​i​n​d​o​w​]​ ​a​d​d​S​u​b​v​i​e​w​:​t​a​s​k​T​a​b​l​e​]​;​
 ​ ​ ​ ​[​[​s​e​l​f​ ​w​i​n​d​o​w​]​ ​a​d​d​S​u​b​v​i​e​w​:​t​a​s​k​F​i​e​l​d​]​;​
 ​ ​ ​ ​[​[​s​e​l​f​ ​w​i​n​d​o​w​]​ ​a​d​d​S​u​b​v​i​e​w​:​i​n​s​e​r​t​B​u​t​t​o​n​]​;​

 ​ ​ ​ ​/​/​ ​F​i​n​a​l​i​z​e​ ​t​h​e​ ​w​i​n​d​o​w​ ​a​n​d​ ​p​u​t​ ​i​t​ ​o​n​ ​t​h​e​ ​s​c​r​e​e​n​
 ​ ​ ​ ​[​[​s​e​l​f​ ​w​i​n​d​o​w​]​ ​s​e​t​B​a​c​k​g​r​o​u​n​d​C​o​l​o​r​:​[​U​I​C​o​l​o​r​ ​w​h​i​t​e​C​o​l​o​r​]​]​;​
 ​ ​ ​ ​[​[​s​e​l​f​ ​w​i​n​d​o​w​]​ ​m​a​k​e​K​e​y​A​n​d​V​i​s​i​b​l​e​]​;​

 ​ ​ ​ ​r​e​t​u​r​n​ ​Y​E​S​;​
}​

Running on the iOS simulator

Now that you’ve set up your views, we can build the application to see how they look. In Xcode, find the Scheme dropdown menu near the Run button. Select iPhone 5.X Simulator for the latest version of the iOS simulator:

Figure 27.6  Scheme selector

Scheme selector

Build and run the application. You’ll get a warning from the compiler that you that you haven’t implemented addTask:. Ignore that for now; you’ll implement addTask: shortly.

The simulator lets you run Cocoa Touch applications on your desktop. This is a quick and easy way to see what your program should look and act like when it runs on an iOS device.

You can see the views that you set up and laid out in application:didFinishLaunchingWithOptions:, but they can’t do anything yet. In fact, tapping the Insert button will crash the application because the button’s action method, addTask:, isn’t implemented yet. (That’s one of the reasons the compiler warned you about it.)

Figure 27.7  iTahDoodle object diagram

iTahDoodle object diagram
..................Content has been hidden....................

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