简体   繁体   English

在不使用代码的情况下将xib UIView添加到storyboard视图控制器

[英]add xib UIView to storyboard view controller without using code

I am trying to reuse a specific view. 我正在尝试重用特定的视图。 Say it's a voting system with five buttons. 说这是一个有五个按钮的投票系统。 Since it appears in different scenes/screen, I just wanted to have one view and reuse it where needed. 由于它出现在不同的场景/屏幕中,我只想拥有一个视图并在需要时重用它。 So I create the view in a xib file named MyStarSystem.xib . 所以我在名为MyStarSystem.xib的xib文件中创建视图。 Now I want to simply drop my view into a storyboard view controller through the storyboard , not in code. 现在我想简单地通过故事板将我的视图放入故事板视图控制器,而不是代码。

If all goes well, I know I should be able to do that by just dragging a UIView into the view controller and then set the class as UIVotingView . 如果一切顺利,我知道我应该能够通过将UIView拖入视图控制器然后将类设置为UIVotingView But where do I tell UIVotingView to draw the content of the xib? 但是,我在哪里告诉UIVotingView绘制UIVotingView的内容? And of course, the buttons have to be interactive so I want to have IBOutlets and IBActions. 当然,按钮必须是交互式的,所以我想拥有IBOutlets和IBActions。 So I was thinking of using -(void)drawRect:(CGRec)rect but that makes no sense as a solution. 所以我在考虑使用-(void)drawRect:(CGRec)rect但这没有任何意义作为解决方案。 Does anyone know how I might create this view once and use it all over -- by dropping it into the storyboard? 有谁知道如何创建这个视图并将其全部用到 - 通过将其放入故事板?

I think you can accomplish what you want by making a base view controller in the storyboard, and connecting its IBActions and outlets. 我认为您可以通过在故事板中创建基本视图控制器并连接其IBActions和插座来实现您的目标。 You can then copy and paste this controller in the storyboard -- depending on how you do it, the copy may appear directly over the old one, so you need to move it over to see both. 然后,您可以在故事板中复制并粘贴此控制器 - 根据您的操作方式,副本可能会直接显示在旧的控制器上,因此您需要移动它以查看两者。 Create as many of these as you want, and create new subclasses for them that all inherit from your base view controller. 根据需要创建尽可能多的这些子类,并为它们创建所有都继承自基本视图控制器的子类。 They should all have access to the outlets and actions that you made in the base controller (the outlets need to be in the .h file, not the class extension in the .m). 他们应该都可以访问您在基本控制器中创建的插座和操作(插座需要位于.h文件中,而不是.m中的类扩展)。 You can then add any specific code in the subclasses, and any extra views in the storyboard to customize them. 然后,您可以在子类中添加任何特定代码,并在故事板中添加任何额外视图以自定义它们。

To reuse all information from one controller to other you have many ways, but never coding is impossible call a xib from a Storyboard , but is simple to do let me explain: 要重用从一个控制器到另一个控制器的所有信息,你有很多方法,但从来没有编码是不可能从一个Storyboard调用一个xib ,但是很容易让我解释一下:

1) To reuse your information class follow this step: 1)要重用您的信息类,请按照以下步骤操作:

- open your class.h for your.xib and in the first line use this: - 为your.xib打开class.h,在第一行使用:

    #import <UIKit/UIKit.h>
    //here you have to import your voting view so:
    #import "VotingView.h"

    @interface ViewController : UIViewController {

}

@end

- now you have to import all function from you voting view to you xib view to reuse them: - 现在你必须从你的投票视图中导入所有函数到你的xib视图以重用它们:

    #import <UIKit/UIKit.h>
    #import "VotingView.h"

    //change you UIViewController in to your VotingView like this 

    @interface ViewController : VotingView {

    }

@end
  • ok now you can call all function in your ViewController.m from VotingView for example if in your VotingView have a - (void)voting {} now in your ViewController.m under ViewDidLoad you have write only [self voting]; 好吧,现在你可以从VotingView调用ViewController.m所有函数,例如,如果你的VotingView中有一个- (void)voting {} vote - (void)voting {}现在你的ViewController.m中的ViewDidLoad你只写了[self voting];

Ok now how to call XIB from Storyboard: 好了,现在如何从Storyboard调用XIB:

go in your ViewController in storyboard and add a Button in Interface, using a IBAction to call a xib: 进入故事板中的ViewController并在接口中添加一个Button,使用IBAction调用xib:

- (IBAction)callXib:(id)sender{

ViewController *viewController=[[ViewController alloc]initWithNibName:@"MyStarSystem" bundle:nil];

[self presentViewController:viewController animated:YES completion:nil]; 

}

Now to dismiss an back from xib to a storyboard create other Button and use this funcioon: 现在要解除从xib到故事板的后退创建其他Button并使用此功能:

- (IBAction)backTo:(id)sender{

[self dismissViewControllerAnimated:YES completion:nil];

}

That's it hope this help you! 希望这对你有所帮助!

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

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