[英]Accessing interface builder object from view controller
I'm completely new to Objective-C, XCode, and iOS development and I'm trying to figure out how to run certain code at startup, after all UI views and controls have been instantiated. 我对Objective-C,XCode和iOS开发是完全陌生的,并且我试图找出在实例化所有UI视图和控件之后如何在启动时运行某些代码的方法。 I have a generic NSObject that I've added through interface builder by dragging it into my view controller scene.
我有一个通用的NSObject,可以通过将其拖动到视图控制器场景中来通过界面生成器添加。 It's defined as follows:
它的定义如下:
#import <Foundation/Foundation.h>
@interface Controller : NSObject {
IBOutlet UISlider *slider;
IBOutlet UILabel *label;
}
-(IBAction)sliderChanged:(id)sender;
@end
I need to run sliderChanged on initialization. 我需要在初始化时运行sliderChanged。 I've tried the following way:
我尝试了以下方法:
#import "Controller.h"
@implementation Controller
-(id)init {
self = [super init];
if (self){
[self sliderChanged:nil];
}
return self;
}
// More code here
But both my slider and label are nil when this is called. 但是,当我调用它时,我的滑块和标签都为零。 I understand there's a
viewDidLoad
method within the ViewController
class which may be what I need, but I'm not sure how to access the instance of my Controller
class (which seems to be instantiated somewhere behind the scenes by the interface builder) from within that method. 我知道
ViewController
类中有一个viewDidLoad
方法,可能是我所需要的,但是我不确定如何从中访问我的Controller
类的实例(它似乎是由界面生成器实例化的)。方法。 Should all of this code simply be moved to the ViewController
itself? 是否应该将所有这些代码简单地移到
ViewController
本身? That would seem to make sense, but the design above is what we've been instructed in class, so I'm not really sure how to go about doing this. 这似乎是有道理的,但是上面的设计是我们在课堂上得到的指导,因此我不确定如何执行此操作。
After the XIB/Storyboard loader finishes loading all the objects and wiring them up, it sends awakeFromNib
to every object that was instantiated from the XIB. XIB / Storyboard加载器完成所有对象的加载并进行接线后,它将
awakeFromNib
发送到从XIB实例化的每个对象。 So try adding this to your Controller
class: 因此,请尝试将其添加到
Controller
类中:
- (void)awakeFromNib {
[super awakeFromNib];
[self sliderChanged:nil];
}
You can find more information in the NSObject UIKit Additions Reference and “The Nib Object Life Cycle” in the Resource Programming Guide . 您可以在《 NSObject UIKit添加参考》和《 资源编程指南 》中的“笔尖对象生命周期”中找到更多信息。
HOWEVER , if you created Controller
as a top-level object, and you didn't connect any outlets to it, then nothing references it after the XIB loader finishes with it, so the system will deallocate it again. 但是 ,如果你创建
Controller
作为一个顶级对象,而你没有任何插座连接到它,那么XIB装载机为完成后,这样系统就会重新释放它没有引用它。 That's probably not what you want, so you should connect an outlet in your view controller to the Controller
. 那可能不是您想要的,因此您应该将视图控制器中的插座连接到
Controller
。 If you do that (and let's say the outlet is named controller
), then you can access it in viewDidLoad
in your view controller class: 如果这样做(并且将出口命名为
controller
),则可以在视图控制器类的viewDidLoad
中访问它:
#import <UIKit/UIKit.h>
#import "Controller.h"
@interface ViewController : UIViewController {
IBOutlet Controller *controller;
}
@end
Implementation: 实现方式:
- (void)viewDidLoad {
[super viewDidLoad];
[self.controller sliderChanged:self];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.