检查视图控制器及其视图Inspecting the View Controller and Its View
正如早些时候学到的,一个视图控制器是代表它管理的一个场景,这个场景代表一个区域的内容。你在这个区域看到的内容被定义在这个视图控制器的视图中。这一章,你会更详细的了解场景通过 HelloWorldViewController 管理视图,还会学到如何改变视图的背景颜色。As you learned earlier, a view controller is responsible for managing one scene, which represents one area of content. The content that you see in this area is defined in the view controller’s view. In this chapter, you take a closer look at the scene managed by HelloWorldViewController
and learn how to adjust the background color of its view.
用检查器检查视图控制器Use the Inspector to Examine the View Controller
当一个应用启动,主 storyboard 文件被加载,初始视图控制器被例示。初始视图控制器管理用户打开应用看到的第一个场景。因为单视图模式只提供一个视图控制器,这个视图控制器自动设为初始视图控制器。你可以通过用 Xcode.app 的检查器核实视图控制器的状态,找到关于它的其他的东西。When an app starts up, the main storyboard file is loaded and the initial view controller is instantiated. The initial view controller manages the first scene that users see when they open the app. Because the Single View template provides only one view controller, it’s automatically set to be the initial view controller. You can verify the status of the view controller, and find out other things about it, by using the Xcode inspector.
改变视图背景颜色Change the View’s Background Color
早前的指导中,你已经学习了运行模拟器后,第一个视图默认为白色。为了确保应用正常运行,你可以把背景颜色设置成白色以外的颜色,然后再次运行应用,看新的背景色是否已经变成你设置的新颜色。Earlier in the tutorial, you learned that a view provides the white background that you saw when you ran the app in Simulator. To make sure that your app is working correctly, you can set the background color of the view to something other than white and verify that the new color is displayed by again running the app in Simulator.
改变背景色之前,要确保 storyboard 是在帆布中打开的。(如果有必要,点击导航条中的 MainStoryboard.storyboard ,在帆布中打开)。Before you change the background of the view, make sure that the storyboard is still open on the canvas. (If necessary, click MainStoryboard.storyboard
in the project navigator to open the storyboard on the canvas.)
点击运行按钮(或者在菜单栏中选择 产品>运行),在模拟器上测试你的应用。确保 Xcode.app 的工具条计划弹出菜单中任然是显示 HelloWorld>iPhone 5.0 Simulator Simulator 。你应该看到如下内容:Click the Run button (or choose Product > Run) to test your app in Simulator. Make sure the Scheme pop-up menu in the Xcode toolbar still displays HelloWorld > iPhone 5.0 Simulator. You should see something like this:
告诫/暗示Tip 在运行应用前不需要进行保存,当你运行时, Xcode.app 已经自动保存了你写的和你修改过的所有文件。You don’t have to save your work before you run your app because when you click Run (or choose Product > Run), Xcode automatically saves the files to which you’ve made changes.
在继续前,先将你的视图背景色恢复成白色。Before you continue with the tutorial, restore the view’s background color to white.
重新运行应用,看到白色背景,退出模拟器。After you’ve verified that your app again displays a white background, quit Simulator.
当运行程序时, Xcode.app 可能会在工作空间底部打开测试区(Debug area)。这次不需要用,所以可以先关掉,窗口也会有更大的地方。When you run your app, Xcode might open the Debug area at the bottom of the workspace window. You won’t use this pane in this tutorial, so you can close it to make more room in your window.
Recap
在这一章,你已经检查了场景,改变(和还原)视图背景颜色。In this chapter, you inspected the scene and changed (and restored) the background color of the view.
在下一章,将会向视图添加文本区、标签和按钮,用户可以通过这些(控件)与应用进行交互。In the next chapter, you add to the view the text field, label, and button that allow users to interact with your app.