add subview is not displaying my uiview - objective-c

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

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.

IBOutlets are nil on a XIB view

I have a UIView subclass and it's corresponding XIB. I use this UIView in a UITableViewCell designed in Interface Builder. When dequeuing the cells, I have a correct reference from the cell to the custom view but the IBOutlets of the custom view are nil.
Here is a sample project that shows the problem
https://github.com/JanC/TestXIB
What am I doing wrong?
cheers,
Jan
I don't think it exactly works that way with UITableViewCell, defining it as IBOutlet won't load all the subviews so you have to load it from the bundle using [NSBundle mainBundle] loadNibNamed:#"" owner:self]; and assigned that to your item view
Now it's loading correctly. In the xib, I set the file owner to TAItemTitleView and the Class to just a UIView. I then load the xib in the TAItemTitleView itself
- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self) {
NSString *className = NSStringFromClass([self class]);
self.view = [[[NSBundle mainBundle] loadNibNamed:className owner:self options:nil] firstObject];
[self addSubview:self.view];
}
return self;
}
https://github.com/JanC/TestXIB/blob/master/TestXIB/Classes/Views/TAItemTitleView.m

loadNibNamed does not load the subviews of a XIB file

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

iOS: NSURLRequest returns the same page each time on different storyboards

I have a UIWebView class that is added to each storyboard on load, but when passing into this class the page I require form the parent ViewController it only show the file from the first ever page and on each different page/viewController its the same even though I set it different on each.
Is it somehow caching this page and if so, how can I have a clean UIWebview everytime?
my UIwebview class m file (ContentView.m):
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
NSString *urlpath = [[NSBundle mainBundle] pathForResource:pageToDisplay ofType:#"html" inDirectory:#"pageContent"];
NSURL *url=[[NSURL alloc] initFileURLWithPath:urlpath];
NSURLRequest *request=[NSURLRequest requestWithURL:url];
[self loadRequest:request];
}
return self;
}
My Parent view passes the requested page using the following variable pageToDisplay:
Parent View m file:
#import "ContentView.h"
- (void)viewDidLoad {
ContentView *dispalyPageContent = [[ContentView alloc] init];
[dispalyPageContent setPageToDisplay:#"THEpg1"];
[self.view addSubview:dispalyPageContent];
//--
[super viewDidLoad];
}
For each other parent view the code is the same but the [displayPageContent setPageToDisplay:#"THEpg1"]; which should change the page I want, e.g. next view would be [displayPageContent setPageToDisplay:#"THEpg2"];
Is it possible to clear this and load a fresh request each time?
It's because when you call ContentView *dispalyPageContent = [[ContentView alloc] init]; in your viewDidLoad , you init it with:
NSString *urlpath = [[NSBundle mainBundle] pathForResource:pageToDisplay ofType:#"html" inDirectory:#"pageContent"];
And in pathForResource:pageToDisplay at the moment of initializing is some trash.
And then, when you call [dispalyPageContent setPageToDisplay:#"smth"] , it changes nothing, because your class ContentView has been already inited with some trash in PageToDisplay
(Also, you call [[ContentView alloc] init]; and in your code there is only initWithFrame:... for objects of ContentView class)
You can solve this in this way:
1) in your ContentView.m
- (id)initWithFrame:(CGRect)frame pageToDisp:(NSString *)pageToDisp {
self = [super initWithFrame:frame];
if (self) {
NSString *urlpath = [[NSBundle mainBundle] pathForResource:pageToDisp ofType:#"html" inDirectory:#"pageContent"];
NSURL *url=[[NSURL alloc] initFileURLWithPath:urlpath];
NSURLRequest *request=[NSURLRequest requestWithURL:url];
[self loadRequest:request];
}
return self;
}
2) In your ContentView.h:
- (id)initWithFrame:(CGRect)frame pageToDisp:(NSString *)pageToDisp
3) And then, in viewDidLoad:
- (void)viewDidLoad {
ContentView *dispalyPageContent = [[ContentView alloc] initWithFrame:someFrame pageToDisp:#"THEpg1"];
[self.view addSubview:dispalyPageContent];
//--
[super viewDidLoad];
}

iOS: Returning to main XIB from secondary XIB

I am trying to change a back to the main xib after I was on a second XIB. This is the current code I am using:
NSArray *nibObjs = [[NSBundle mainBundle] loadNibNamed:#"Results1" owner:self options:nil];
UIView *aView = [nibObjs objectAtIndex:0];
self.view = aView;
I have a button on the second xib that returns back to the originating xib. When I hit the button the app crashes. I am not sure why because it does not display an error - it just crashes. Here is the code on the button on the second xib or "Results1" xib file:
-(IBAction)Button100:(id)sender
{
NSArray *nibObjs = [[NSBundle mainBundle] loadNibNamed:#"main" owner:self options:nil];
UIView *aView = [nibObjs objectAtIndex:0];
self.view = aView;
}
Not sure what I am doing wrong.
Add as subviews instead, so you can then just 'pop' the Results1 view off:
// in your .h
#property (strong) UIView *aView;
// replace your provided code with this
NSArray *nibObjs = [[NSBundle mainBundle] loadNibNamed:#"Results1" owner:self options:nil];
self.aView = [nibObjs objectAtIndex:0];
[self.view addSubview:self.aView];
- (IBAction)Button100:(id)sender
{
[self.aView removeFromSuperview];
self.aView = nil;
}
Just a suggestion — if you want them to 'swap', consider using the class method transitionFromView:toView:duration:options:completion: in UIView.
The problem is I needed to turn off Automatic Reference Counting.