简体   繁体   English

从视图控制器访问接口构建器对象

[英]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.

相关问题 在Interface Builder中从View Controller隐藏标签栏 - Hide tab bar from View Controller in Interface Builder 使用Interface Builder时查看控制器遏制 - View Controller Containment when using Interface Builder Interface Builder IB - 导航控制器,跳过视图 - Interface Builder IB - Navigation Controller, skipping a view 如何在从界面构建器中的Storyboard场景实例化的视图控制器上调用自定义init? - How do I call a custom init on a view controller instantiated from a Storyboard scene in interface builder? 无法将图像从媒体库拖放到Xcode Interface Builder中的视图控制器上 - Cannot drag and drop image from Media library onto a view controller in Xcode Interface Builder 通过接口构建器将xib的自定义视图添加到控制器-保留自动布局约束并根据其调整大小 - Custom view from xib added to controller via Interface builder - keep autolayout constraints and resize by them 从代码切换到在界面生成器中创建的视图 - Switching to a view made in interface builder from code 如何将接口构建器中的UITabBarContoller设置为根视图控制器? - How do you set UITabBarContoller in interface builder as root view controller? Xcode Interface Builder高度/宽度设置与视图控制器 - Xcode Interface Builder Height/Width Settings vs. View Controller Interface Builder:无法将自定义类分配给表视图控制器 - Interface Builder : cannot assign custom class to table view controller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM