loadNibNamed does not load the subviews of a XIB file - objective-c

I am using loadNibNamed to init a custom view. But I see that the subviews are not available when the view is returned.
-(instancetype) initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]){
NSArray *views = [[NSBundle mainBundle] loadNibNamed:#"HRAlternateCategoriesView" owner:self options:nil];
self = [views firstObject];
[self setFrame:frame];
}
return self;
}
viewDidLoad method
-(void) viewDidLoad {
CGRect frame = CGRectMake(0, 0, 320, 220);
self.altCategoryView = [[HRAlternateCategoriesView alloc] initWithFrame:frame];
[self.view addSubview:self.altCategoryView];
}
The XIB file contains 8 subviews (4 buttons and 4 image views) none of which are there in the generated UIView class. Iam not usng autolayout and size classes.
OS: iOS8

Related

custom UIVIew with nib subviews nil

I create a simple custom UIView with a xib file, I connect xib view to my class, then I linked my IBOutles to subviews, after this I create the view with initWithFrame and I add it programmatically to my parent view
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
NSArray *objects = [[NSBundle mainBundle] loadNibNamed:#"AddAlarmView" owner:self options:nil];
_scrollView = (UIScrollView *)[objects objectAtIndex:0];
[self addSubview:_scrollView];
}
return self;
}
the problem is that all the subviews are nil....if I try to get customView.textFeild1 it is nil....why?where is the mistake?
Instead of this you should try to get your UIView object from Xib like this..
- (id)initWithFrame:(CGRect)frame
{
NSArray *objects = [[NSBundle mainBundle] loadNibNamed:#"AddAlarmView" owner:self options:nil];
self = (YourClassName *)[objects objectAtIndex:0];
self.frame = frame;
return self;
}
You don't need to add subViews again your IBOutLets. They are already subView of your View in xib file.
Simply load that xib file and assign it to your self and update your frame according to the parameter.

add subview is not displaying my uiview

I have the following code, and the uiview is not showing, i've placed a breakpoint & see the view is not nil and the frame is set to the given size - why the view is not showing in my uiviewcontroller?
#property (nonatomic,strong) LoadingView *loadingView;
self.loadingView = [[[NSBundle mainBundle] loadNibNamed:#"LoadingView" owner:self options:nil] objectAtIndex:0];
self.loadingView.frame = CGRectMake(110,170,100,100);
[self.view addSubview:self.loadingView];
Your code is logically wrong. Your LoadingView class must allocate before you assigning nib member to it.
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
NSArray *nibs = [[NSBundle mainBundle] loadNibNamed:#"LoadingView" owner:self options:nil];
self = [nibs objectAtIndex:0];
}
return self;
}
Your nib assignment should be in the initwithframe method of LoadingView class.
Then Your implementation of adding view would be like,
self.loadingView = [[LoadingView alloc] initWithFrame:CGRectMake(110,170,100,100)];
[self.view addSubview:self.loadingView];

UIView with rotation issues

I've a sample project, where I'm creating an custom UIViewController.
#import "ViewController.h"
#implementation ViewController
#synthesize webViews = _webViews;
#synthesize webView = _webView;
- (void)setWebView:(UIWebView *)webView {
if (webView!=_webView) {
[self.webView removeFromSuperview];
_webView = nil;
_webView = webView;
[self.view addSubview:self.webView];
[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://www.google.com/"]]];
}
}
- (IBAction)newWebView:(id)sender {
self.webView = [self.webViews objectAtIndex:1];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
UIWebView *webView1 = [[UIWebView alloc] initWithFrame:self.view.bounds];
webView1.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
UIWebView *webView2 = [[UIWebView alloc] initWithFrame:self.view.bounds];
webView2.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.webViews = [NSArray arrayWithObjects: webView1, webView2, nil];
self.webView = [self.webViews objectAtIndex:0];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
#end
Two UIWebView's are created inside the viewWillAppear:animated and stored in an array -- and than the first UIWebView is added to the subview of self.view. When a button in the navigation bar is pressed, the first UIWebView will be removed from the subview and the next one will be added instead.
The problem is, that if I'm running the application in landscape (both iPhone and iPad) after the second UIWebView is added, the WebView is not filling the entire screen. Why?
You can download the small sample project here: http://uploads.demaweb.dk/WebView.zip.
I dont know why but this inside your action method will do the trick...
UIWebView *newWebView=[[UIWebView alloc] init];
newWebView=[self.webViews objectAtIndex:1] ;
newWebView.frame=self.view.frame;
self.webView = newWebView;
hope this helps.. :/

Display UIView subclass which contains other UIViews

I want to package some UIViews suchs as UIButton, UIImageView in a single UIView.
When I try to display that view, it is not showing up on my RootViewController:
Here is the code of my UIView subclass :
#import "Hover.h"
#implementation Hover
- (id)init{
self = [super init];
if (self) {
// Initialization code
UIImage *hover = [UIImage imageNamed:#"Hover.png"];
UIImageView *imageView = [[UIImageView alloc] init];
imageView.image = hover;
imageView.frame = CGRectMake(0, 0, hover.size.width, hover.size.height);
imageView.alpha = 0.75;
[self addSubview:imageView];
[imageView release];
}
return self;
}
And here is the RootViewController class:
- (void)viewDidLoad
{
Hover *hover = [[Hover alloc] init];
[self.navigationController.view addSubview:hover];
[hover release];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
The "hover view" is not displayed! However, when I add a single UIImageView to my RootViewController, it works!
In order for your view to display, you should override the -(id)initWithFrame:(CGRect)frame;, instead of writing a custom initializer.
Try this way :
-(id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if(self) {
// do init here...
}
return self;
}
Also, in the -(void)viewDidLoad; method, first send [super viewDidLoad];, and then alloc/init the view.
- (void)viewDidLoad
{
[super viewDidLoad];
Hover *hover = [[Hover alloc] init];
[self.navigationController.visibleViewController.view addSubview:hover];
[hover release];
// Do any additional setup after loading the view, typically from a nib.
}
Just a quick guess at a glance: If you're using a navigationcontroller, you should initWithRootViewController. You can't add a view to a navigationcontroller directly, Use push instead.
HTH
Marcus

Loading an OverlayView from XIB -vs- programmatically for use with UIImagePickerController

I am currently making a camera app for iPhone and I have a strange phenomenon that I can't figure out. I would appreciate some help understanding.
When recreating an overlay view for passing to UIImagePickerController, I have been successfully been able to create the view programmatically. What I haven't been able to do is create the view with/without controller in IB, load it and pass it to the overlay pointer successfully. If I do it via IB, the view is not opaque and obscures the view of the camera completely. I can not figure out why.
I was thinking that the normal view pointer might be assigned when loading from XIB and therefore overwrite the camera's view, but I have an example programmatically where view and overlayView are set equal in the controller class. Perhaps the load order is overwriting a pointer?
Help would be appreciated... kind regards.
This works fine...
- (void)applicationDidFinishLaunching:(UIApplication *)application {
// dislodge the MainView from the MainViewController
//
MainViewController *aController = [[MainViewController alloc] init]; //WithNibName:#"MainView" bundle:nil];
aController.sourceType= UIImagePickerControllerSourceTypeCamera;
aController.delegate= aController;
aController.showsCameraControls= NO;
// Programmatically load the MainView from the Nib and assign it as the OverlayView instead
//
NSArray* nibViews= [[NSBundle mainBundle] loadNibNamed:#"MainView" owner:aController options:nil];
UIView* uiView= [nibViews objectAtIndex:0];
uiView.opaque= NO;
uiView.backgroundColor= [UIColor clearColor];
aController.cameraOverlayView= uiView;
self.mainViewController = aController;
[aController release];
mainViewController.view.frame = [UIScreen mainScreen].applicationFrame;
[window addSubview:[mainViewController view]];
[window makeKeyAndVisible];
}
This DOESN'T...
- (void)applicationDidFinishLaunching:(UIApplication *)application {
// dislodge the MainView from the MainViewController
//
MainViewController *aController = [[MainViewController alloc] initWithNibName:#"MainView" bundle:nil];
aController.sourceType= UIImagePickerControllerSourceTypeCamera;
aController.delegate= aController;
aController.showsCameraControls= NO;
aController.cameraOverlayView= aController.view;
self.mainViewController = aController;
[aController release];
mainViewController.view.frame = [UIScreen mainScreen].applicationFrame;
[window addSubview:[mainViewController view]];
[window makeKeyAndVisible];
}