4
votes

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. 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. But where do I tell UIVotingView to draw the content of the xib? And of course, the buttons have to be interactive so I want to have IBOutlets and IBActions. So I was thinking of using -(void)drawRect:(CGRec)rect but that makes no sense as a solution. Does anyone know how I might create this view once and use it all over -- by dropping it into the storyboard?

2
You can copy and paste a view from a xib into a storyboard. Delete the view that comes with a view controller, and paste in the one from the xib.rdelmar
@rdelmar thanks. Let me try that, especially to see if it carries with it all the IBOutlets and IBActions.Katedral Pillon
@rdelmar when I drag and drop directly form xib, the IBActions and IBOutlets don't come along. I was hoping for an object oriented way of taking the whole system along to create composite objects. The displays themselves are not the problem. The real problem is that I am going to have duplicate code if this composition is not possible in iOS.Katedral Pillon
@KatedralPillon I believe you and I are looking for the same answer to how we can just drag and drop a custom view (xib) into the controller in a storyboard and use it like other UIView objects, but many response is about loading the xib programmatically and adding the custom view to the parent view programmatically. I don't think I've find the solution yet. I would like to know.Sam
Did you try with container views?João Nunes

2 Answers

1
votes

I think you can accomplish what you want by making a base view controller in the storyboard, and connecting its IBActions and outlets. 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). You can then add any specific code in the subclasses, and any extra views in the storyboard to customize them.

-2
votes

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:

1) To reuse your information class follow this step:

- open your class.h for your.xib and in the first line use this:

    #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:

    #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];

Ok now how to call XIB from Storyboard:

go in your ViewController in storyboard and add a Button in Interface, using a IBAction to call a 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:

- (IBAction)backTo:(id)sender{

[self dismissViewControllerAnimated:YES completion:nil];

}

That's it hope this help you!