简体   繁体   English

Objective-C-将对象从XIB文件导入视图控制器

[英]Objective-c - Getting objects from XIB file into a view controller

So I have a XIB file, which contains a view. 所以我有一个XIB文件,其中包含一个视图。 In that view, I want to add custom objects and then being able to get those objects, and use them in a view controller I have where I crate other things programmatically . 在该视图中,我想添加自定义对象,然后能够获取这些对象,并在视图控制器中使用它们,在这里我可以通过programmatically其他对象。

My XIB file is called "MyXibLibrary.xib" My view controller where I want the objects to be added to is called "ContactDetailsViewController" 我的XIB文件称为"MyXibLibrary.xib"我要将对象添加到其中的视图控制器称为"ContactDetailsViewController"

My view controller is being pushed from a UITableViewController like this: 我的视图控制器是从UITableViewController推送的,如下所示:

 ContactDetailsViewController *detailViewController = [[ContactDetailsViewController alloc] init];

And inside my ContactDetailsViewController viewWillAppear I have this code to get the XIB objects: 在我的ContactDetailsViewController viewWillAppear内部,我有以下代码来获取XIB对象:

 UIView *xibView = [[[NSBundle mainBundle] loadNibNamed:@"MyLib" owner:self options:nil] objectAtIndex:0];
    [self.view addSubview:xibView];

Now, a for instance, the loadNibNamed property should be? 现在,例如, loadNibNamed属性应该是? Name of the XIB file? XIB文件的名称? Name of the view in that XIB file? 该XIB文件中的视图名称? Or what? 要不然是啥?

All this is bringing me errors and the app trows exeption. 所有这些都给我带来了错误,并且该应用程序异常强大。

I have no clue what so ever on how to work with XIB files since I super new to Objective-c coding. 自从我不熟悉Objective-c编码以来,我对如何使用XIB文件一无所知。

Any help would be really appreciated!!! 任何帮助将非常感激!!! Thanks! 谢谢!

The XIB, which is referred to as a NIB (as a matter of history), defines at least one view that is to be "controlled" by a view controller. XIB被称为NIB(根据历史记录),它定义了至少一个将由视图控制器“控制”的视图。 This view can represent the whole user interface or simply a subview of another view (eg your XIB could represent a reusable table row). 该视图可以表示整个用户界面,也可以仅表示另一个视图的子视图(例如,您的XIB可以表示可重复使用的表行)。 Thus, you should not be using a XIB as a sort of container for pre-built interface elements in the manner you describe. 因此,您不应以您描述的方式将XIB用作预先构建的界面元素的一种容器。

However, it is simple to work with the components of the XIB provided your controller knows about them. 但是,只要您的控制器知道XIB的组件,就很容易使用它们。 That is, the elements of your XIB should connect to properties of your view controller class. 也就是说,XIB的元素应连接到视图控制器类的属性。

For example, let's say you have the following view controller interface: 例如,假设您具有以下视图控制器界面:

#import <UIKit/UIKit.h>

interface MyViewController : UIViewController { 
}

@property (strong, nonatomic) IBOutlet UITextView *textEntry;
@property (strong, nonatomic) IBOutlet UIButton *enterButton;

A corresponding NIB would be named MyView.xib . 相应的NIB将被命名为MyView.xib In the interface builder, you would set the "File's Owner" for the NIB to be "MyViewController". 在界面生成器中,将NIB的“文件所有者”设置为“ MyViewController”。 You would then link the interface elements, a UITextView and a UIButton, to MyViewController's properties (in whatever method you prefer - usually an option+click & drag from the interface element to the File's Owner object). 然后,您可以将界面元素,UITextView和UIButton链接到MyViewController的属性(以您喜欢的任何方法-通常是选项,然后单击并从界面元素拖动到File的Owner对象)。

Having done this, you can then instantiate the view controller anywhere you please and work with the properties of that object. 完成此操作后,您可以在任意位置实例化视图控制器,并使用该对象的属性。 For example, let's pretend this code is in a file named "SomeOtherController.m": 例如,让我们假设此代码位于名为“ SomeOtherController.m”的文件中:

- (void)aMethodOfSomeOtherController
{
  MyViewController *myView = [[MyViewController alloc] 
                              initWithNibName:@"MyViewController" 
                              bundle:nibBundleOrNil];

  NSString *buttonLabelText = [[[myView] enterButton] titleLabel] text];
  NSLog(@"Button label text = %@", buttonLabelText);

  [myView release];
}

When this method is invoked, an instance of MyViewController will be created which will automatically load the stored objects from the NIB and bind them to the view controller object's properties. 调用此方法时,将创建MyViewController的实例,该实例将自动从NIB加载存储的对象并将其绑定到视图控制器对象的属性。 It will then retrieve the text of the button's label and write it to the log console. 然后,它将检索按钮标签的文本并将其写入日志控制台。

If you look at the documentation for UIViewController , there's a method called initWithNibName:bundle: . 如果您查看UIViewController文档 ,则有一个名为initWithNibName:bundle: It lists five different sample code projects that Apple provides to demonstrate how to use xib files and view controllers. 它列出了Apple提供的五个不同的示例代码项目,以演示如何使用xib文件和视图控制器。 You should read a few of those to get a basic understanding. 您应该阅读其中的一些内容以获得基本理解。

If you want to load an UIView from NIB, this code would be more correct (since you don't know the index of a needed object in the xib file). 如果要从NIB加载UIView ,则此代码会更正确(因为您不知道xib文件中所需对象的索引)。

- (id)loadViewFromNIB:(NSString *)nibName owner:(id)owner class:(Class)_class
{
    NSArray *objects = [[NSBundle mainBundle] loadNibNamed:nibName owner:owner options:nil];

    for (id object in objects) {
        if ([object isKindOfClass:_class]) {
            return object;
        }
    }
}

Open MyLib.xib in interface builder and check that you actually have a UIView component in that file. 在界面构建器中打开MyLib.xib,并检查该文件中是否确实有一个UIView组件。

What error message do you get from the exception? 您从异常中得到什么错误信息?

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

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