Setting up views

One thing that you need to do before the application is ready for the user is set up your view objects: the text field, the button, and the table view. This means creating them, configuring them, and putting them on the screen.

In iTahDoodle, you are going to set up your views programmatically in application:didFinishLaunchingWithOptions:. Xcode also has a visual drag-and-drop tool for setting up views, which you will use in Chapter 32.

The code for creating and configuring views is dense, and we are not going to cover it in detail. The detailed syntax of creating and showing views on the screen is a topic for a book specifically about iOS application programming.

Still, you can follow the gist of what is happening. First, 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.

Figure 31.8  View objects in iTahDoodle

View objects in iTahDoodle

In BNRAppDelegate.m, remove any boilerplate code from application:didFinishLaunchingWithOptions: and replace it with the following code:

#​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​
{​
 ​ ​ ​ ​/​/​ ​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​ ​a​ ​s​i​z​e​ ​(​w​i​d​t​h​,​h​e​i​g​h​t​)​
 ​ ​ ​ ​C​G​R​e​c​t​ ​w​i​n​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​F​r​a​m​e​]​;​
 ​ ​ ​ ​s​e​l​f​.​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​,​ ​w​i​n​F​r​a​m​e​.​s​i​z​e​.​w​i​d​t​h​,​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​w​i​n​F​r​a​m​e​.​s​i​z​e​.​h​e​i​g​h​t​ ​-​ ​1​0​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​ ​U​I​T​a​b​l​e​V​i​e​w​ ​i​n​s​t​a​n​c​e​
 ​ ​ ​ ​s​e​l​f​.​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​]​;​
 ​ ​ ​ ​s​e​l​f​.​t​a​s​k​T​a​b​l​e​.​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​;​

 ​ ​ ​ ​/​/​ ​T​e​l​l​ ​t​h​e​ ​t​a​b​l​e​ ​v​i​e​w​ ​w​h​i​c​h​ ​c​l​a​s​s​ ​t​o​ ​i​n​s​t​a​n​t​i​a​t​e​ ​w​h​e​n​e​v​e​r​ ​i​t​
 ​ ​ ​ ​/​/​ ​n​e​e​d​s​ ​t​o​ ​c​r​e​a​t​e​ ​a​ ​n​e​w​ ​c​e​l​l​
 ​ ​ ​ ​[​s​e​l​f​.​t​a​s​k​T​a​b​l​e​ ​r​e​g​i​s​t​e​r​C​l​a​s​s​:​[​U​I​T​a​b​l​e​V​i​e​w​C​e​l​l​ ​c​l​a​s​s​]​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​f​o​r​C​e​l​l​R​e​u​s​e​I​d​e​n​t​i​f​i​e​r​:​@​"​C​e​l​l​"​]​;​

 ​ ​ ​ ​/​/​ ​C​r​e​a​t​e​ ​a​n​d​ ​c​o​n​f​i​g​u​r​e​ ​t​h​e​ ​U​I​T​e​x​t​F​i​e​l​d​ ​i​n​s​t​a​n​c​e​ ​w​h​e​r​e​ ​n​e​w​ ​t​a​s​k​s​ ​w​i​l​l​ ​b​e​ ​e​n​t​e​r​e​d​
 ​ ​ ​ ​s​e​l​f​.​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​]​;​
 ​ ​ ​ ​s​e​l​f​.​t​a​s​k​F​i​e​l​d​.​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​;​
 ​ ​ ​ ​s​e​l​f​.​t​a​s​k​F​i​e​l​d​.​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​ ​t​h​e​ ​U​I​B​u​t​t​o​n​ ​i​n​s​t​a​n​c​e​
 ​ ​ ​ ​s​e​l​f​.​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​]​;​
 ​ ​ ​ ​s​e​l​f​.​i​n​s​e​r​t​B​u​t​t​o​n​.​f​r​a​m​e​ ​=​ ​b​u​t​t​o​n​F​r​a​m​e​;​

 ​ ​ ​ ​/​/​ ​G​i​v​e​ ​t​h​e​ ​b​u​t​t​o​n​ ​a​ ​t​i​t​l​e​
 ​ ​ ​ ​[​s​e​l​f​.​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​:​s​e​l​f​.​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​:​s​e​l​f​.​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​:​s​e​l​f​.​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​.​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​;​
}​

Now that you have set up your views, you can run iTahDoodle on the iOS simulator to see them.

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

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