Initialize a view with a xib - objective-c

I want to initialize a view with an xib. That is a UIView. So I have a xib and a UIView subclass with the init code pasted below (initWitFrame nibName). I feed the xib name to the init code from a view controller when creating the view.
SubclassUIView *view = [[SubclassUIView alloc]initWithFrame:self.view.bounds nibName:#"xibName"];
Am I on the right track? So far the contents of the xib does not load with the view.
By the way the main view in the xib is set to be the class type of my UIView subclass.
- (id)initWithFrame:(CGRect)frame nibName:(NSString*)nibName
{
self = [super initWithFrame:frame];
if (self)
{
[[NSBundle mainBundle] loadNibNamed:nibName owner:self options:nil];
[self addSubview:self.view];
}
return self;
}

What is returned from the NSBundle is an NSArray (of visual controls, mainly UIViews). Appart from that the code seems right. Have a look at a functioning example:
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
NSArray* nibViews = [[NSBundle mainBundle] loadNibNamed:#"TestView"
owner:self
options:nil];
UIView* mainView = (UIView*)[nibViews objectAtIndex:0];
[self addSubview:mainView];
}
return self;
}

#Mike M:
This behaviour can cause random crashes. Sometimes an NSApplication object will be the firstObject -> crash
Apple docs:
https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/LoadingResources/CocoaNibs/CocoaNibs.html
"The order in which the nib-loading code calls the awakeFromNib methods of objects is not guaranteed. In OS X, Cocoa tries to call the awakeFromNib method of File’s Owner last but does not guarantee that behavior. If you need to configure the objects in your nib file further at load time, the most appropriate time to do so is after your nib-loading call returns. At that point, all of the objects are created, initialized, and ready for use."

Related

UIView Subclass backed by xib with Size Classes wrong frame

I am working on a project for iOS 7.0+ with a storyboard, using Size Classes with AutoLayout and I'm using a UIView subclass backed by a xib file of the same name.
What I'am trying to do is I'am instantiating a UIView from xib programmatically and adding it to a ViewController from a Storyboard. This ViewController has AutoLayout up and running but the UIView I am adding doesn't respect the frame of the ViewController.
I'm instantiating my UIView subclass like this:
tabBarView = [[SHDTabBarView alloc] initWithFrame:CGRectMake(0, self.view.height-50, self.view.width, 50)];
[self.view addSubview:tabBarView];
And inside the subclass I'm using a set up of creating a UIView IBOutlet called container to instantiate it form code like this:
-(id)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self == nil) return nil;
[self initalizeSubviews];
return self;
}
-(id)initWithCoder:(NSCoder *)aDecoder{
self = [super initWithCoder:aDecoder];
if (self == nil) return nil;
[self initalizeSubviews];
return self;
}
-(void)initalizeSubviews{
NSString *nibName = NSStringFromClass([self class]);
UINib *nib = [UINib nibWithNibName:nibName bundle:nil];
[nib instantiateWithOwner:self options:nil];
//Add the view loaded from the nib into self.
[self addSubview:self.container];
}
This is how my xib looks in the Interface Builder (notice the width of the canvas is 320 px):
And that's how it looks on the iPhone 6 (notice how it's getting cut off from the right side):
I've tried to use a multitude of solutions, including doing it all in code with an open-source solution PureLayout, using a manual constraint set up, etc.
None of my findings seem to work right. Ideally, I want to set up everything in Interface Builder, then just add the view to the superview of the ViewController with according frame and let AutoLayout do its magic.
How should I approach this task? Any advices are more than welcome.
Try to set the frame of your subview in the viewDidLayoutSubviews(). Looks like you init your subview before view fully layouted

NSViewController New vs. InitWithNibName issues

I am having a weird error with NSViewController where if I allocate a view using the viewcontroller's regular init message, the view created is not my view, but when using the default NIB name, it does work.
Specifically, this code works all the time. It creates the view defined in the nib file, and displays it in the parentView.
+ (void)createTransparentViewCenteredInView:(NSView*)parentView withText:(NSString*)text duration:(NSTimeInterval)duration {
TransparentAccessoryViewController* controller = [[TransparentAccessoryViewController alloc] initWithNibName:#"TransparentAccessoryViewController" bundle:nil];
NSLog(#"%#", [controller.view class]); // Returns "TransparentAccessoryView" -- CORRECT
[parentView addSubview:controller.view];
}
However, the following code works SOME of the time (which is weird in that it doesn't always fail). With some parentViews, it works perfectly fine, and with others, it doesn't. The parent views are just random custom NSViews.
+ (void)createTransparentViewCenteredInView:(NSView*)parentView withText:(NSString*)text duration:(NSTimeInterval)duration {
TransparentAccessoryViewController* controller = [TransparentAccessoryViewController new];
NSLog(#"%#", [controller.view class]); // Returns "NSSplitView" -- INCORRECT
[parentView addSubview:controller.view];
}
The errors that comes up are as follows (I have no idea why it is bringing up an NSTableView, as I don't have an NSTableView here at all. Also, it is weird that it complains about an NSTableView when the type it prints is an NSSplitView):
2013-04-07 21:33:12.384 Could not connect the action refresh: to
target of class TransparentAccessoryViewController
2013-04-07 21:33:12.384 Could not connect the action remove: to target
of class TransparentAccessoryViewController
2013-04-07 21:33:12.385 * Illegal NSTableView data source
(). Must implement
numberOfRowsInTableView: and tableView:objectValueForTableColumn:row:
The NIB file defines a custom subclassed NSView, called TransparentAccessoryView, and hooks this up to the File Owner's view property, standard stuff (all I did was change the custom class name to TransparentAccessoryView). I added an NSLog's to see what was going on, and for some reason, in the second case, the view class type is incorrect and thinks it is an NSSplitView for some reason. The ViewController class is as follows:
#implementation TransparentAccessoryViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Initialization code here.
}
return self;
}
- (void)awakeFromNib {
self.textField.stringValue = #"";
}
+ (void)createTransparentViewCenteredInView:(NSView*)parentView withText:(NSString*)text duration:(NSTimeInterval)duration {
TransparentAccessoryViewController* controller = [[TransparentAccessoryViewController alloc] initWithNibName:#"TransparentAccessoryViewController" bundle:nil];
NSLog(#"%#", [controller.view class]);
[parentView addSubview:controller.view];
}
#end
I thought that the default init message triggers the viewcontroller to load the NIB named after the viewcontroller, which seems to be the case some of the time as the second version of my code works in certain conditions.
Does anyone know why this behavior is occurring at all?
From the docs:
If you pass in a nil for nibNameOrNil then nibName will return nil and
loadView will throw an exception; in this case you must invoke
setView: before view is invoked, or override loadView.
Therefore, if you're initializing a NSViewController with -init, you should call -setView: to set the view controller's view, or override -loadView. In the latter case, you could certainly implement the UIViewController-like behavior that you're probably expecting -- if nibNameOrNil is nil, try to load a nib that has the same name as the class.
I think that when you call init on a NSViewController, you're assuming that the implementation of init for NSViewController searches for a nib with the same name as the view controller and uses it. However, this is undocumented API or at least I can't seem to find any documentation supporting that assumption. The link you posted on your comments doesn't cite any documentation either and even reiterates that this is undocumented and that Apple could change this implementation at any point.
I think to assure that your code works in future versions of the SDK (and since it is already creating undesired behavior), you should not rely on this assumption. To achieve the same outcome simply override init and initWithNibName:bundle: in such a way as explained by this post:
#implementation MyCustomViewController
// This is now the designated initializer
- (id)init
{
NSString *nibName = #"MyCustomViewController";
NSBundle *bundle = nil;
self = [super initWithNibName:nibName bundle:bundle];
if (self) {
...
}
return self;
}
- (id)initWithNibName:(NSString *)nibName bundle:(NSBundle *)bundle
{
// Disregard parameters - nib name is an implementation detail
return [self init];
}

Use xib without a view controller

I need to create several similar views
In a easy way,I create some views in xib(each full screen)
And I have a view controller to use this xib's views,code like this:
NSArray* views = [[NSBundle mainBundle] loadNibNamed:#"MyXibName" owner:nil options:nil];
[self.view addSubview:[views objectAtIndex:aIndex]];
At this moment,view shows alright.
Now,there's some buttons in those views,so I connect a outlet for each view
bad thing happened
app crashed due to
uncaught exception 'NSUnknownKeyException', reason: '[<NSObject 0x969db50> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key
Analyse:
Although my xib file's "File Owner" has been set,but there's no connection between xib and the only view controller.
How could I get the pointer of a view's button?
You can do it like this:
NSNib* aNib = [[NSNib alloc] initWithNibNamed:#"MyGreatNib" bundle:nil];
NSArray* topLevelObjs = nil;
for (SomeClass *obj in myOwnerObjects) {
topLevelObjs = nil;
if (![aNib instantiateNibWithOwner:obj topLevelObjects:&topLevelObjs])
{
NSLog(#"Warning! Could not load nib file.\n");
return;
}
for (id topLevelObj in topLevelObjs) {
if ([topLevelObj isKindOfClass:[NSView class]]) {
NSView *otView = (NSView *)topLevelObj;
// set frame...
[self addSubview:otView];
}
}
}
Oops...
I just found something.
UINib* xib = [UINib nibWithNibName:#"MyXibName" bundle:nil];
UIView* view = [[xib instantiateWithOwner:self options:nil] lastObject];
It works!
You can design xib as per your need by defining class UIView
Code in .m file:
NSArray* objects = [[NSBundle mainBundle] loadNibNamed:#"Interface" owner:nil options:nil];
UIView* mainView = [objects objectAtIndex:0];
for (UIView* view in [mainView subviews]) {
if([view isKindOfClass:[UILabel class]])
{
UILabel* label = (UILabel*)view;
//....As You Wish...
}
}
Default implementation of -loadView creates the view or loads NIB. As far as I know, there is no way to know the final size of the view at time of creation in -loadView. So the default view size is set to [[UIScreen mainScreen] bounds]. This is because it may be difficult to work work with zero frame view in -viewDidLoad and other methods.
Your one-line implementation may look like this:
- (void)loadView {
self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
}
You don't need to set the autoresizing mask, because you don't know in what context the view will be displayed. The caller is responsible to set you correct frame, autoresizing mask and similar external attributes (I call them like this).
Imagine this in a UINavigationController method:
// we are pushing new VC, view is accessed for the first time
pushedVC.view.frame = CGRectMake(...);
It is setting the correct frame, but your -loadView is called just before that -setFrame:. So during -viewDidLoad you have temporary non-zero frame, just to be able to setup subviews and internal autoresizing. After this, the correct frame is set to you and in -viewWillAppear: you have final frame.

Infinite loop when overriding initWithCoder

I have a UIViewController with some controllers and some views. Two of these views (Grid Cell) are other nibs. I've got outlets from the Grid Cells to File's Owner, but they aren't loaded automatically.
So I try to override GridCell.m's initWithCoder.
This starts an infinite loop.
I know it's possible to just override initWithFrame and add the subview from code, but this is not what I want. I want to be able to move the view around in Interface Builder and have Xcode initialize the view with the right frame.
How do I go about achieving this?
EDIT 1
I'm trying to get it working with the help of Alexander. This is how I've now got it set up:
MainView has UIView with a Custom class set as GridCell. It got an outlet in the MainView/File's Owner.
Removed all init-code from GridCell.m and set up an outlet to my custom class
The MainView don't still display the GridCell though. There's no error, just a lonely, empty space where the red switch should be. What am I doing wrong?
I'm very close to just doing this programmatically. I would love to learn how to this with nibs though.
Loading the nib causes initWithCoder to be called again, so you only want to do so if the subclass currently doesn't have any subviews.
-(id)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
if (self.subviews.count == 0) {
UINib *nib = [UINib nibWithNibName:NSStringFromClass([self class]) bundle:nil];
UIView *subview = [[nib instantiateWithOwner:self options:nil] objectAtIndex:0];
subview.frame = self.bounds;
[self addSubview:subview];
}
}
return self;
}
Loading a nib will cause the corresponding owner in a
-(id) initWithCoder:(NSCoder *) coder;
call
Therefore your coude in this method:
self = [[[NSBundle mainBundle] loadNibNamed: #"GridCell"
owner: self
options: nil] objectAtIndex:0];
will cause again a call of the initWithCoder method. That's because you try to load the nib again. If you define a custom UIView and create a nib file to lay out its subviews you can't just add a UIView to another nib file, change the class name in IB to your custom class and expect the nib loading system to figure it out.
What you could do is the following:
Your custom view's nib file needs to have the 'File's owner' class set to your custom view class and you need to have an outlet in your custom class called 'toplevelSubView' connected to a view in your custom view nib file that is acting as a container for all the subviews. Add additional outlets to your view class and connect up the subviews to 'File's owner' (your custom UIView). (See https://stackoverflow.com/a/7589220/925622)
EDIT
Okay, to answer your edited question I would do the following:
Go to the nib file where you want to include the custom view with it's nib file layouting it.
Do not make it to the custom view (GridCell) itself, instead make a view which will contain your grid cell (gridCellContainer for example, but it should be a UIView)
Customize the initWithFrame method within your custom view like you did in initWithCoder:
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"GridCell" owner:self options:nil];
self = [nib objectAtIndex:0];
self.frame = frame;
}
return self;
}
And then, in the viewController which is the fileOwner for the view where you want to include your custom view (the one with the gridCellContainer view) do this in viewDidLoad e.g.
//...
GridCell *gridCell = [[GridCell alloc] initWithFrame:self.viewGridCellContainer.bounds];
[self.viewGridCellContainer addSubview:gridCell];
Now eveything should work as you expected
The File's owner will not get a call to
-(id) initWithCoder:(NSCoder *) coder;
when loading a xib.
However, every view defined in that xib will get a call to
-(id) initWithCoder:(NSCoder *) coder;
when loading a xib.
If you have a subclass of a UIView (i.e. GridCell) defined in a xib and also try to load that same xib in your subclass's initWithCoder, you will end up with an infinite loop. However, I can't see what will the use case be.
Usually you design your UIView's subclass (i.e. GridCell) in one xib, then use that subclass in a view controller’s xib for example.
Also, can't see a use case where your custom view will have a subview in it's initWithCoder, i.e.
-(id)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
if (self.subviews.count == 0) {
UINib *nib = [UINib nibWithNibName:NSStringFromClass([self class]) bundle:nil];
UIView *subview = [[nib instantiateWithOwner:self options:nil] objectAtIndex:0];
subview.frame = self.bounds;
[self addSubview:subview];
}
}
return self;
}
Unless you want to be able to override its view hierarchy on demand in some other xib. Which IMO assumes an external dependency (i.e. a hierarchy defined in another xib) and kinda defeats the purpose of having a reusable UIView in the first place.
Bare in mind that when loading a xib, passing an instance as the File's owner, will have all of its IBOutlet(s) set. In that case, you would be replacing self (i.e. GridCell) with whatever the root view in that GridCell.xib is, losing all IBOutlet connections in the process.
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"GridCell" owner:self options:nil];
self = [nib objectAtIndex:0];
self.frame = frame;
}
return self;
}
There is a more detailed post on "How to implement a reusable UIView." that goes into a bit more detail as well and hopefully clears things up.
loadNibNamed:: will call initWithCoder:
Why don't you follow this pattern?
-(id)initWithCoder:(NSCoder *)coder
{
if (self = [super initWithcoder:coder]) {
// do stuff here ...
}
return self;
}
Does [super initWithcoder:coder] do things that you want to avoid?
I had the same issue when I'm trying to override initWithsomething method, we need
-(id)initWithsomething:(Something *)something
{
if (self = [super initWithsomething:something]) {
// do stuff here ...
}
return self;
}
instead
-(id)initWithsomething:(Something *)something
{
if (self = [super init]) {
// do stuff here ...
}
return self;
}

Objective-c How can add one UIview at the main view directly from a class?

I have this class
- (void) initWithFrame:(CGRect)frame withString:(NSString *)html{
//CGRect rectFrame = [UIScreen mainScreen].applicationFrame;
news = [[UIView alloc] initWithFrame:frame];
web = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 200, 300)];
web.scalesPageToFit = YES;
//web.delegate = self;
web.scalesPageToFit = YES;
[web loadHTMLString:html baseURL:nil];
[news addSubview:web];
[[self view] addSubview:news];
}
I receive Sigabrt at line [[self view] addSubview:news];
How can add the UIview at the main view directly from a class? is it possibile?
1) init<...> methods should return initialized object - declare it returning value of id type. Return self in implementation or nil if it's failed.
2) init<...> methods have to initialize this object. Read about initializing objects here - The Objective-C Programming Language - Allocating and Initializing Objects. Basically you have to call one of the initialization methods of superclass, assign self to returned value (self = [super init]) and check if this value is nil.
Is this class a controller? If so:
3) You have to load it's view - either by loading it from nib on initialization (init or initWithNibName:bundle: methods) or by implementing method loadView. See View Controller Programming Guide for iOS
4) You can be sure your view is available only when viewDidLoad is called on your controller. Implement this method if you want to add something to controller's view.
The reason you are receiving the abort signal is that your view is not initialized. Instead try adding to awakeFromNib call.