Display a UIView without a UIViewController - ios

I'm trying to create a custom UIView that can be displayed at any point in the game. In other words, the UIView is not tied to a specific UIViewController.
Right now my solution is to store a UIViewController* in my custom UIView, set it whenever the UIViewController I'm using changes, and then adding the UIView as a subview of the controller.
I figure there has to be a better, and safer, way of handling this, but I haven't been able to find anything so far.
Thanks

Make a UIView subclass. File > New File > Objective-C Class > (subclass of ) UIView > Give it a Class Name.
So now you should have something like MyCustomView.h and .m
In the View Controller that you want to display the custom view import the header file
#import "MyCustomView.h"
Then simply add it to your current view
MyCustomView *myCView = [[MyCustomView alloc] initWithFrame:<Where You Want It>];
[self.view addSubview:myCView];
[myCView release];
You can use Interface Builder to graphically lay out the custom view. Or you can do it programmatically. If you're going the IB way, create a new file, click on User Interface, and View. Give it the same Class Name i.e. (MyCustomView).
Select the File Owner show the Identity Inspector (option command 3).
For Class, change it from NSObject to MyCustomView.

Related

Creating IBoutlet from UIView in custom UIView class swift [duplicate]

This question already has answers here:
Cannot create outlet connections to subviews in Interface Builder (Xcode 5)
(5 answers)
Closed 6 years ago.
I have a UIView with a UIlabel and a UIImage inside it placed on my view controller.
I created a custom class for the UIView and tried to create an IBoutlet for the UIImage and UILabel in the custom class but i will not let me drop it.
I have already clicked on the UIView and set its class to the custom class, so I am not sure why it is not letting me create the outlet.
Below is a screenshot of the custom class, uiview (With image and label). You may have to click on it to see it.
Thanks for your help
In your situation, you should connect the outlets to a view controller instead of a view.
Instead of creating a UIView subclass, you should create a UIViewController subclass:
class FrontViewController: UIViewController {
}
Next, select your view controller in the storyboard. In the Identity Inspector (select the tab with an ID card on the panel on the left), type "FrontViewController" (the name of your class) into the "Class" field.
Now you should be able to drag outlets to the class!
As you are crating ViewController there you can connect only view controllers IBOutlets. What you are doing with setting this views class is only marking that this view should behave as this class (you are not creating it there, so you can't set it's IBOutlets). As well this to items won't have anything with tat view, as it's not it creation, that will just be two separate views and YOUR view will be as parent. The only thing you need to understand is that you can create IBOutlet only if it's views creation part, as it's only controllers creation part it's won't give you possibility to create such for his inner view.

trying to hook up UILabels and IBActions in a custom class (Xcode 5)

I am having issues making UILabels and IBAction buttons in custom classes. Xcode seems to not want me to do it. They way I'm trying to do it is through interface builder (storyboard). I have no issue clicking and dragging to make IBOutlets and IBActions using the main View Controller but when I click and drag over to connect them in a custom class it does nothing. Am I only able to make these in the View Controller? I've attached a pic of me trying to drag over to connect my button in the custom class and you can see that nothing pops up. So basically, Are labels, buttons, text fields etc, for the View Controller class only? Thanks for any feedback.
Within interface builder, you will need to make sure your view controller is using your custom class by opening utilities view on the RHS of the IB, selecting the third icon along (please see picture below) and enter your custom class name in the space provided.
Your custom class will need to be a subclass of UIViewController though, like the picture below, not an NSObject for example.
I'm referring to this line within your .h file ...
#interface MyCustomClassViewController : UIViewController
Your custom class needs to match the UI object you are connecting it with. For example if you had UIView object, your custom class would need to be a subclass of UIView, a UINavigationViewController object, a subclass of UINavigationController etc etc.
I hope this helps.
Select your view controller in the stoyboard and go to the identity inspector, make sure the name there is your custom class name.

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. 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?
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.
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!

Cannot create outlet connections to subviews in Interface Builder (Xcode 5)

I know this appears to be a duplicate of some other questions, but the answers are not working for me.
I have created a single view app.
In the storyboard I added a subview to my main view.
I have a label on my main view and another label on my subview.
I have created a class of type UIView and added it as the custom class for the subview.
I can ctrl-drag my label on my main view to the main view controller class. But when I try to ctrl-drag my label on my subview to my custom class, I cannot get the connection to occur.
I have even typed the property information and tried to make the connection manually to no avail.
Things have changed a bit in the latest version of Xcode's Interface Builder. Can somebody tell me what I am missing? There is literally no code here. I am just testing trying to connect outlets to a subview with a custom class.
The first image shows that I have set up the custom class and added a property but I cannot make the connection.
The second image shows the main view label is connected in the main view's controller.
The third image shows that there are no outlet connections for the subview's label.
You can manually write the IBOutlet property declaration in the #interface of the custom view subclass, and assuming you've defined the base class of your subview in IB, then you can drag from the outlet circle in the code back to the control in the scene.
Or, as you point out, Warren Burton suggested both this technique and another in his answer to this other question, Can't Wire to Subview in IB.
The issue has to do with the File Owner of the View Controller. It is probably set up as being IOViewController, thus you can only make property connections in that .h file.
What you can do, is create another .nib file for the subview and put the subview in there. Then in that .nib file, make the file owner IOSubview. Property connections will work just fine there. Then just add the subview to your IOViewController programatically. Just remember to load the nib file from bundle first.
This is what I did (in Swift):
I Created a new ViewController (e.g. class MyViewController: UIViewController {})
In StoryBoard, I expanded the 'Scenes' (i.e. the tree view of all UI components) and selected 'MyViewController'
Using the 'identity inspector' I assigned the 'MyViewController' class (as oppose to the default UIViewController)
After that I was able to assign an action.
I suspect that for Obj-C it is similar process.
You don't create outlets in the subclass, you create the outlet on the view controller it is on. You need to #import the subclass into IDViewController.h and create an outlet there.
IDViewController.h
#import "IDSubclass.h"
...
#property (strong, nonatomic) IBOutlet IDSubclass *outletName;
Zoom your storyboard to 100%. If you zoom out, to say 50%, then the outlet connection won't work.

Can't set custom class name

I can't seem to be able to set the identity of my view to GraphViewController in Xcode 4.2.
I dragged a regular UIView in storyboard on a UIScrollView.
I create a new file called GraphViewController extending UIViewController (.h and .m) but when I click on my view in storyboard and try to set the custom class name (its identity) to GraphViewController, it doesn't set it. It doesn't even show it in the dropdown list.
Am I missing something here a connection or something?
You can set any class inherited from UIView to a UIView instance.
But you have created a UIViewController subclass, not a UIView subclass.
You can assign your custom class to the controller, not to the view.
In case anyone ever makes the dumb mistake I just did... ensure you have created an appropriate storyboard for either iOS or OS X. Adding the wrong one to your project will certainly cause issues like this.
My solution was making sure I was clicking on the controller's top tool bar, and not the "screen" UI frame. If you select the frame or anything in it, your Identity Inspector will be looking at the View, or other elements in the storyboard, instead of the controller itself.

Resources