简体   繁体   English

Xcode 6中的情节提要

[英]Storyboards in Xcode 6

I have recently started to learn iOS with obj-c from "iOS Programming The Big Nerd Ranch Guide 4th Edition". 我最近开始从“ iOS编程大书呆子牧场指南第4版”中使用obj-c学习iOS。 This edition was released in 2014 and is written with Xcode 5. 该版本于2014年发布,并使用Xcode 5编写。

I am trying to make a simple app with two buttons and two labels. 我正在尝试制作一个带有两个按钮和两个标签的简单应用程序。 The labels are connected to two arrays and when a button is pressed an object from the corresponding array is shown in the corresponding text label (it's the Quiz app in chapter 1). 标签被连接到两个数组,当按下按钮时,相应数组中的对象将显示在相应的文本标签中(这是第1章中的Quiz应用程序)。

I created the project as a Single View app in Xcode 6, and put all my objects in the view controller class. 我在Xcode 6中将项目创建为“单视图”应用程序,然后将所有对象放入视图控制器类中。 I have two labels two buttons two arrays and an int to keep track of the object that has to be displayed from the array. 我有两个标签,两个按钮,两个数组和一个int,以跟踪必须从数组中显示的对象。

In the book it says that I should initialize the arrays in the initWithNibName method. 在书中说我应该在initWithNibName方法中初始化数组。 I tried that but for some reason it never gets called. 我尝试过,但是由于某种原因它从未被调用过。 So I changed the initialization of the arrays to the init method. 因此,我将数组的初始化更改为init方法。 They initialize fine but when they are called from another method they are nil . 它们初始化很好,但是当从另一个方法调用它们时,它们为nil Do you have any idea why this is happening? 您知道为什么会这样吗?

The second issue I'm having is that I can't manage to get the contents of the storyboard on screen. 我遇到的第二个问题是我无法在屏幕上获取情节提要的内容。 It says that I'm supposed to make an instance of the ViewController inside the AppDelegate and make it the root window controller but all I get is a white window (or black in case I don't set the color). 它说我应该在AppDelegate内创建ViewController的实例,并使其成为根窗口控制器,但是我得到的只是一个白色窗口(如果不设置颜色,则为黑色)。

UPDATE: I changed the intialization of the arrays from the init method to the viewDidLoad method and now they seem to be working fine. 更新:我将数组的初始化从init方法更改为viewDidLoad方法,现在它们似乎运行良好。 Nothing on the screen though. 屏幕上什么也没有。

It sounds like you're initializing your UIViewController from the app delegate AND a storyboard. 听起来您正在从应用程序委托和情节提要中初始化UIViewController。 If you create a new project in XCode, a "Single view application", you won't have to touch the app delegate at all in order to get something on the screen. 如果您在XCode(一个“单视图应用程序”)中创建一个新项目,则无需触摸应用程序委托就可以在屏幕上看到一些东西。

I believe both your problems are related to this, since it sounds like you're seeing an empty UIViewController on the screen (the one you create in the app delegate) 我相信您的两个问题都与此有关,因为听起来好像您在屏幕上看到一个空的UIViewController(您在应用程序委托中创建的一个)

As for the initialization of your array, viewDidLoad is a popular place to do this. 至于数组的初始化, viewDidLoad是执行此操作的常用方法。

If you are using storyboards, the method initWithNibNameOrNil will not be called. 如果使用情节提要板,则不会调用initWithNibNameOrNil方法。 In the BNR book, it teaches you to use XIB files, which do use this method. 在BNR书中,它教您使用确实使用此方法的XIB文件。 If you are trying to follow the tutorials, I would suggest using XIB files. 如果您尝试遵循这些教程,建议您使用XIB文件。

For use of a book, I would suggest downloading whatever version of Xcode is being used for that book -- otherwise you will be running into a lot of confusing problems while learning. 对于一本书的使用,我建议下载用于该书的Xcode的任何版本-否则在学习时您会遇到很多令人困惑的问题。

If you would like to download previous version of Xcode, refer to this post: How to download Xcode DMG or XIP file? 如果要下载Xcode的早期版本,请参阅此文章: 如何下载Xcode DMG或XIP文件?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM