1

我只习惯于 xcode 中的故事板,但我的新老板希望每个人都在 xib 文件中完成,我没有经验。

到目前为止,我所做的只是创建了一个新的单视图应用程序,并在“viewDidLoad”方法下添加了代码..

UILabel *scoreLabel = [ [UILabel alloc ] initWithFrame:CGRectMake(0, 0.0, 150.0, 43.0) ];

[self addSubview:scoreLabel];

和线

 [self addSubview:scoreLabel];

抛出错误 'No Visible @Interface for 'ViewController' 声明选择器 'addSubView'

我到底在这里想念什么?

4

4 回答 4

6

self是一个UIViewController。您将子视图添加到UIView. 所有你需要的是:

[self.view addSubview:scoreLabel];
于 2013-01-22T16:18:45.787 回答
1

您正在尝试向控制器添加子视图?它应该是视图。

于 2013-01-22T16:18:58.423 回答
1

viewDidLoad是 UIViewController 的方法,不是 UIView。要使您的代码正常工作,请替换:

[self addSubview:scoreLabel];   

[self.view addSubview:scoreLabel]

To add label from the xib, you do not need any of the above code. In your .h file you must declare property with IBOutlet. Then, in your .xib add label and set its referencing outlet to the declared property. I admit, it is not intuitive at first.

于 2013-01-22T16:20:03.710 回答
1
self.view will be the view of the view controller.

so you can add view ie; sub view only to the view. not its controller. self will be the view controller here

于 2013-01-22T16:21:51.383 回答