Subviews for custom UIView with Nib (.xib) don't load? - ios

I am trying to create a custom UIView class with an associated Nib (.xib) file to help with layout and creation.
Right now, no subviews in my custom view appear, even though they exist in my nib file and are hooked up properly to my custom view class via IBOutlets.
I tried going through the following other answer:
Load custom UIView with XIB from a View Controller's view using IB
and this one:
How do I get a view in Interface Builder to load a custom view in another nib?
and this one:
http://soulwithmobiletechnology.blogspot.com/2011/06/create-uiview-with-nib-file-in-xcode-4.html
but the method [[NSBundle mainBundle] loadNibNamed:#"MyView" owner:self options:nil] seems to call -[initWithCoder:], leading to an infinite loop.
Please let me know what other details to include to help solve this bug!
UPDATE 1: Do I need to lay out my subviews via -[layoutSubviews]? Does my nib file actually not lay it out? If so, then what is the point of the nib file?

Open any of your view controllers and paste this code into your viewDidAppear method:
NSArray * allTheViewsInMyNIB = [[NSBundle mainBundle] loadNibNamed:#"MyView" owner:self options:nil]; // loading nib (might contain more than one view)
MyCustomView* problemView = allTheViewsInMyNIB[0]; // getting desired view
problemView.frame = CGRectMake(0,0,100,100); //setting the frame
[self.view addSubview:problemView]; // showing it to user
That's all you have to do, to present a view. If you want to interact with this view — you have to declare MyCustomView* problemView in your class' interface, not in the method.

Related

ios loadNibNamed to Superclass

I have got a view.xib file with a CustomClass = "BaseClass" (I configure it on XCode editor), so I can do that:
BaseClass *mBassClass= [[[NSBundle mainBundle] loadNibNamed:#"view" owner:self options:nil] objectAtIndex:0];
But I want to know how I can do that:
I would have got a child class from BaseClass. For example ChildClass extends from BaseClass, and load a view from Nib but with a ChildClass
ChildClass *aChild = [[[NSBundle mainBundle] loadNibNamed:#"view" owner:self options:nil] objectAtIndex:0];
ChildClass2 *anotherView = [[[NSBundle mainBundle] loadNibNamed:#"view" owner:self options:nil] objectAtIndex:0];
So, I want to have got BaseClass with some child class with specific functions but all of these views have got the same Nib file
In my case I'm developing the following views:
(1) A nib file, it has got some uibutton from control and a UIView that will contain a Core Plot Graph
(2) A BaseClass, called GraphView, it matchs with the nib file and it does function with the uibuttons.
(3) Some child class, each one paint a different graph/plot on the UIView, but the inherits the functions of uibutton controls.
Any suggestion? Thanks
Create your XIB as the container for the graph. It contains all of the common views and functionality, but not the graph specific content.
Now, when you load the NIB, you will always get a BaseClass instance. Indeed, you must always get a BaseClass - you can not change the class that is unarchived from the NIB.
Your graph views shouldn't be subclasses of BaseClass. Your graph view class that is appropriate to the current situation should then be instantiated and added as a subview of the container view, which is owned by the BaseClass (container) instance.
If the graph view needs to change the common button status then you should provide the graph view with a reference to the BaseClass instance when you add it as a subview.

In this project, why am I unable to access my view's properties that I load from a nib?

Here's a sample project recreating the issue: http://cl.ly/3O3M232f1q0R
I created a TutorialScreen subclass of UIView, and in a xib file I created three UIViews of that type and used them in another class by bringing those objects into UIViews:
self.tutorialScreen1 = [[[NSBundle mainBundle] loadNibNamed:#"View" owner:nil options:nil] objectAtIndex:0];
self.tutorialScreen1.translatesAutoresizingMaskIntoConstraints = NO;
self.tutorialScreen1.layer.cornerRadius = 8.0;
self.tutorialScreen1.alpha = 0.0;
[self.notificationWindow addSubview:self.tutorialScreen1];
In the xib file, each UIView has a UILabel in the middle that I created an outlet for (and linked all three UIViews to), called textLabel.
But in that class I created tutorialScreen1 in, when I do the following:
NSLog(#"%#", self.tutorialScreen3.textLabel.text);
Every time that outputs (null). Why on earth is it doing that? The label is explicitly set to "text" so I don't see why it keeps calling it null. I can't manipulate it at all because the label doesn't seem to exist.
You didn't connect the outlets:

Why am I unable to access this variable I created in a nib?

I created a TutorialScreen subclass of UIView, and in a xib file I created three UIViews of that type and used them in another class by bringing those objects into UIViews:
self.tutorialScreen1 = [[[NSBundle mainBundle] loadNibNamed:#"View" owner:nil options:nil] objectAtIndex:0];
self.tutorialScreen1.translatesAutoresizingMaskIntoConstraints = NO;
self.tutorialScreen1.layer.cornerRadius = 8.0;
self.tutorialScreen1.alpha = 0.0;
[self.notificationWindow addSubview:self.tutorialScreen1];
In the xib file, each UIView has a UILabel in the middle that I created an outlet for (and linked all three UIViews to), called textLabel.
But in that class I created tutorialScreen1 in, when I do the following:
NSLog(#"%#", self.tutorialScreen3.textLabel.text);
Every time that outputs (null). Why on earth is it doing that? The label is explicitly set to "text" so I don't see why it keeps calling it null. I can't manipulate it at all because the label doesn't seem to exist.
Did you create outlet of all the views if yes then create outlet of UILabel also after when you click on any button e.t.c present that view which you want.

How refresh a single control in xib?

I have a xib with a 2 UIViews named subview1 and subview2. In subview1 a UILabel is palced and in subview2 a UITableView is placed.
I want to refresh the subview1 to change the label text at a particular time but no need to refresh tableview that time. After few operations again I need to refresh the tableview but no need to refresh the label.
How can I achieve this ?.
Thanks.
You can try next way.
Load xib by loadNibNamed and just replace wanted subview.
NSArray * views = [[NSBundle mainBundle] loadNibNamed:#"your xib name" owner:self options:nil];

Dynamically create multiple instances of a UIView from a NIB

I have a grid on screen. It is an instance of a UIView subclass. In cells in the grid I want to add instances of subviews - another custom subclass of UIView - as the user interacts with the app. The subview itself is something I want to design in an XIB. Its controls represent state for that particular cell. It consists of a couple of UIControls (say a label and a button).
I already know I can load NIBs dynamically using
[[NSBundle mainBundle] loadNibNamed:#"MyNIB" owner:self options:nil];
and I know they'll work if I set the File's Owner property in the NIB to the view controller that's responsible for the view these subviews will be added to. I use this to add some external NIBs to a UIScrollview in the main app screen.
What I want to know is how can I do this dynamically? I want to say something like:
MySubviewCell * sv = [[NSBundle mainBundle] loadNibNamed:#"MyNIB" owner:self options:nil];
[sv setFoo:#"Foo"];
[sv setBar:123];
[sv setFrame:myrect];
[mainView addSubview:sv];
But of course loadNibNamed doesn't return me the subview instance, instead it returns an array of all the controls in the view.
EDIT File's Owner in the MySubviewCell NIB will be a problem: there isn't one, and there can't be: I don't know how many I'll need. Should I be using something like an ArrayController?
EDIT 2 Please ignore the previous edit; I've left it in as the answer refers to it but it's not the problem I thought it was.
Can I do this?
Thanks
This is commonly used when designing table view cells in a nib. You've got two options:
The object at index 0 of the returned array will be the "root" object in your nib.
Create an outlet in your view controller of type MySubviewCell. Set your file's owner class in the nib to your view controller, and link the outlet to the element in the nib you are interested in. When you load the nib with owner self, the outlet becomes populated. You can then configure it, add it to your array, and set the outlet back to nil to prepare to load the nib again for the next instance.
Try to access to first object in that array:
MySubviewCell * sv = (MySubviewCell*)[[[NSBundle mainBundle] loadNibNamed:#"MyNIB"
owner:self
options:nil]
objectAtIndex:0];

Resources