简体   繁体   English

Xcode:如何在UIScrollView中从XIB文件添加自定义视图

[英]Xcode: How to add Custom View from XIB File in a UIScrollView

Coming from an Android and Java background, I am relative new with Xcode Development. 来自Android和Java背景,我对Xcode Development相对较新。

I am using Xcode 5.0.2 and created my first IOS Application by selecting Create a New Xcode Project -> Single View Application. 我正在使用Xcode 5.0.2并通过选择Create a New Xcode Project - > Single View Application创建了我的第一个IOS应用程序。 The initial project structure has been generated and I found that there is a Main_iphone.storyboard file which opens a UI Designer where I can drag and drop items to it. 最初的项目结构已生成,我发现有一个Main_iphone.storyboard文件,它打开一个UI Designer,我可以在其中拖放项目。 I selected UIScrollView and dragged it into the main window that has been generated. 我选择了UIScrollView并将其拖动到已生成的主窗口中。

Now in the Controller header file, I added @property (nonatomic, retain) IBOutlet UIScrollView *scrollView; 现在在Controller头文件中,我添加了@property(非原子,保留)IBOutlet UIScrollView * scrollView; so that i could access the scrollView from inside my controller code. 这样我就可以从我的控制器代码中访问scrollView。

I wanted to add an item to the scrollView programatically so I created a template Custom View by adding new file -> Objective-C Class with XIB for User Interface, named it TSTFilesInfoController and designed the XIB by adding a View and a label inside the view. 我想以编程方式向scrollView添加一个项目,所以我通过添加新文件创建了一个模板Custom View - > Objective-C Class with XIB for User Interface,命名为TSTFilesInfoController,并通过在视图中添加View和一个标签来设计XIB 。 Same with the scrollView above, I created a property to expose the mainView in my controller class. 与上面的scrollView相同,我创建了一个属性来公开我的控制器类中的mainView。

I hardcoded a loop of 10x inside the controller of the UIScrollView and inside the for loop I am instantiating TSTFilesInfoController and adding the view to the UIScrollView. 我在UIScrollView的控制器内硬编码了一个10x的循环,在for循环中我实例化TSTFilesInfoController并将视图添加到UIScrollView。 But when i run the application, nothing is shown or added in the UIScrollView. 但是当我运行应用程序时,UIScrollView中没有显示或添加任何内容。

Heres the code for adding the CustomView: 下面是添加CustomView的代码:

- (void)viewDidLoad {
        [super viewDidLoad];

        for ( int i = 0; i < 10 ; i++) {
            TSTFilesInfoController *info = [[TSTFilesInfoController alloc] init];

            [self addChildViewController:info];

            [self.scrollView addSubview:info.mainView];
            NSLog(@"View has been added into the scrollView");


        }

}

Can someone please tell me whats wrong with my codes and what would be the correct approach to achieve the output that i wanted? 有人可以告诉我我的代码有什么问题,以及实现我想要的输出的正确方法是什么? Thank you in advance for the help. 提前感谢您的帮助。

-- EDIT -- - 编辑 -

This code is auto-generated in TSTFilesInfoController.m 此代码在TSTFilesInfoController.m中自动生成

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {
        self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
        if (self) {
            // Custom initialization
    }


    return self;
}

This is a misuse of a UIViewController subclass. 这是对UIViewController子类的误用。 Do not use a view controller just as a sort of fishing rod to hook a view that's inside a xib file. 不要使用视图控制器作为一种钓鱼杆来挂钩xib文件中的视图。 Simply load the xib file and grab the view yourself, and stuff it into your interface. 只需加载xib文件并自己抓取视图,然后将其填入界面。

There are complex rules for how to put a view controller's view manually inside your interface, and in general it is something you should be reluctant to do. 有关如何在界面中手动放置视图控制器视图的复杂规则,通常这是您不愿意做的事情。 But making views come and go dynamically and directly is easy and common. 但是,动态和直接地制作视图很容易也很常见。

Let's suppose the .xib file is called TSTFilesInfo.xib and it has a top-level UIView subclass object, class MyView, which is the one you want. 假设.xib文件名为TSTFilesInfo.xib ,它有一个顶级的UIView子类对象,类MyView,这是你想要的。 Then: 然后:

MyView* v = (MyView*)
    ([[UINib nibWithNibName:@"TSTFilesInfo" bundle:nil]
         instantiateWithOwner:nil options:nil][0]);

This loads the nib once, instantiating its contents, and handing you a reference to that instance (the UIView in this case). 这会加载nib一次,实例化其内容,并向您提供对该实例的引用(在本例中为UIView)。 Now plunk v into your interface. 现在插入你的界面v Keep that reference in an instance variable (probably weak, since it is retained by its superview) so that you can subsequently configure and communicate with any subviews of MyView to which you have created outlets. 将该引用保留在实例变量中(可能很弱,因为它由超级视图保留),以便您随后可以配置并与您创建了出口的MyView的任何子视图进行通信。

NOTE : However, I must say from the example so far that it sounds to me like what you really want here is a UITableView, not a simple UIScrollView. 注意 :但是,我必须从目前为止的例子中说,它听起来像你真正想要的是UITableView,而不是简单的UIScrollView。 It comes all set to do just the kind of thing you seem to up to here. 它完全可以做到你在这里看到的那种东西。

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

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