UIView addSubview and the subview isn't displayed - ios

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
HeadViewController *headViewController = [[HeadViewController alloc] initWithNibName:#"HeadViewController" bundle:nil];
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 120)];
[view addSubview:headViewController.vew];
[self.view addSubview:view];
}
HeadViewController.h:
#interface HeadViewController : UIViewController
{
IBOutlet UIView *view;
}
#property (nonatomic, retain)IBOutlet UIView *view;
#end
and I connect the view to the file's owner.
And I can't see the headViewController.view.

First of all, you do not need to define the view outlet in the HeadViewController class. It is automatically inherited from the UIViewController super class.
Then, I suggest you to add directly the view of HeadViewController to your current view. Eg.
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
HeadViewController *headViewController = [[HeadViewController alloc] initWithNibName:#"HeadViewController" bundle:nil];
headViewController.view.frame = CGRectMake(0, 0, 320, 120);
[self.view addSubview:headViewController.view];
}
But, if you are using ARC (Automatic Reference Counting), the headViewController instance will probably be deallocated after the end of the viewDidLoad method. It is convenient (and I'd say it is compulsory) to assign that instance to a local variable in the controller you are currently displaying. This way you will be able to handle its view's components later if needed, the instance will be retained, and everything else will work perfectly. You should have something like:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
self.headViewController = [[HeadViewController alloc] initWithNibName:#"HeadViewController" bundle:nil];
headViewController.view.frame = CGRectMake(0, 0, 320, 120);
[self.view addSubview:headViewController.view];
}
and
#interface MyController ()
#property (nonatomic, strong) HeadViewController *headViewController;
#end
in the hidden interface definition at the beginning of the .m class implementation file.

It looks like a typo - forgot the i in .view
[view addSubview:headViewController.vew];

i is missing in the syntax
[view addSubview:headViewController.view];

Related

How to load custom UIView from xib file into a UIView in UIViewController?

I created a xib file.
Below is the code:
#import "LoginView.h"
#implementation LoginView
-(id)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
[self setup];
}
return self;
}
-(void) setup{
[[NSBundle mainBundle] loadNibNamed:#"LoginView" owner:self options:nil];
self.bounds = self.loginView.bounds;
[self addSubview:self.loginView];
}
Now I created a view controller and add a UIView to it. In viewWillAppear, I am trying to load the xib file into a defined UIView in my view controller.
-(void)viewWillAppear:(BOOL)animated{
self.containerView.layer.borderColor = [UIColor blackColor].CGColor;
self.containerView.layer.borderWidth = 2.0f;
LoginView *loginView = [[LoginView alloc] initWithFrame:CGRectMake(self.containerView.frame.origin.x,self.containerView.frame.origin.y, self.containerView.frame.size.width, self.containerView.frame.size.height)];
[self.containerView addSubview:loginView];
}
But, the xib file is going outside the defined UIView which I named containerView. I have linked the containerView to UIView in ViewController.
#property(nonatomic, weak) IBOutlet UIView *containerView;
Can anyone help me why I am getting this output? I need to get the custom uiview inside the containerView in ViewController. Like the textfields and buttons should be inside the black border.
As you are adding your view on container view then x and y should be 0,0
You need to replace this :
LoginView *loginView = [[LoginView alloc] initWithFrame:CGRectMake(self.containerView.frame.origin.x,self.containerView.frame.origin.y, self.containerView.frame.size.width, self.containerView.frame.size.height)];
by this:
LoginView *loginView = [[LoginView alloc] initWithFrame:CGRectMake(0,0, self.containerView.frame.size.width, self.containerView.frame.size.height)];
Hope this Helps!

How can I make this UILabel accessible to other classes?

I have a class which is a viewcontroller and it contains:
-(void)testNSTimer
{
[UIView animateWithDuration:1.0 delay:0 options:UIViewAnimationOptionCurveEaseIn
animations:^{ self.view.welcomeBackLabel alpha = 0;} //welcomeBackLabel does not exist.
completion:nil];
}
However welcomeBackLabel is not available in this circumstance because it is created in the controller's view AKA self.view as shown in the following code:
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
welcomeBackLabel = [[UILabel alloc] initWithFrame:CGRectMake(110, -20, 100, 120)]; //welcomeBackLabel is not available for other classes to use, but I want it to be.
[welcomeBackLabel setText:#"Welcome"];
[welcomeBackLabel setTextAlignment:NSTextAlignmentCenter];
UIFont *welcomeFont = [UIFont fontWithName:#"Helvetica-Bold" size:20];
welcomeBackLabel.font = welcomeFont;
[welcomeBackLabel setTextColor:[UIColor whiteColor]];
[welcomeBackLabel setAlpha:0.65];
[self addSubview:welcomeBackLabel];
}
I created a property however it is not detected by the viewcontroller's class even after importing the view's .h file. Can anybody should me the various ways I could have welcomeBackLabel be usable in my controller class?
EDIT:
For example, in my view's .h I have:
#property(nonatomic, retain) UILabel *welcomeBackLabel;
and I have
#import "view.h"
in my controller.m but its not detecting it as a usable property o.o
EDIT2:
Ok so if I have HomeScreenView *testView = [[HomeScreenView alloc]init]; then YES i can use the property fine ala testView.welcomebackLabel.etc . It just does not work when I have set the view for the controller using [self setView:testView] and then use self.view.welcomeBackLabel... Why is this?
In a UIViewController, self.view is returned as a UIView. If you "know" that it is a particular subclass of UIView, you need to cast it so you can access methods from the subclass. You could inline this, but for clarity:
MyUIView *v = self.view;
[v myMethod];
Or if you want inline:
[(MyUIView *) self.view myMethod];
If it is sometimes the right subclass you can test:
UIView *v = self.view;
if([v isKindOfClass:[MYUIView class]]) {
[(MyUIView *) self.view myMethod];
}
Create an #property in the .h file of your class and it should be accessible from other classes well.

Not able to add subview to my UIView

I have a UIView element on the screen. It is connected by an IBOutlet radioButtonGroupView. I am trying to add a subview to that element and it is not working. The subview is never added.
Here is my code:
-(id) initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
UIView *subView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
subView.backgroundColor = [UIColor yellowColor];
[self.radioButtonGroupView addSubview:subView];
return self;
}
You will probably be better off if you make this element a subclass of a UIViewController (instead of a UIView).
Then put the subview loading code inside viewDidLoad method.
- (void)viewDidLoad
{
[super viewDidLoad]; //not really needed but it doesn't hurt
UIView *subView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
subView.backgroundColor = [UIColor yellowColor];
[self.radioButtonGroupView addSubview:subView];
}
EDIT:
You say that you don't get any frame information on self.radioButtonGroupView in your viewDidLoad. That is almost a certain sign that your IBOutlet is not properly connected to the element in InterfaceBuilder. You can always make a simple test:
- (void)viewDidLoad
{
[super viewDidLoad]; //not really needed but it doesn't hurt
NSLog (#"self.radioButtonGroupView: %#",self.radioButtonGroupView);
[self.radioButtonGroupView setHidden:YES];
}
If it is still shown - then it simply isn't properly connected. IB has it's ways. Simple
deletion of IBOutlet connection and reconnecting it again might do the trick. Also: it your radioButtonGroupView is not UIView but a subclass of UIView, make sure that it's header file is imported in your MyViewController.m file and class properly defined in .xib.

UIView setNeedsDisplay doesn't work

Simple code is here:
Custom MyButton.h
#interface PaiLifeReadingListButton : UIButton
#property (strong, nonatomic) UIImageView *logoImageView;
#end
Custom MyButton.m
#implementation PaiLifeReadingListButton
#synthesize logoImageView = _logoImageView;
-(id)init
{
_logoImageView = [[UIImageView alloc] initWithFrame:CGRectMake(33.0, 20.0, 80.0, 80.0)];
[_logoImageView setImage:[UIImage imageNamed:#"pic"]];
[self addSubview: _logoImageView];
}
#end
Custom MyView.h:
#property (strong, nonatomic) Mybutton *mybutton;
Custom MyView.m:
-(id) init
{
myButton = [[MyButton alloc] init];
[self addSubview:myButton];
}
ViewController.m:
#synthesize myView;
- (void)viewDidLoad
{
myView = [[Myview alloc] init];
[myView.myButton addTarget:self action:#selector(pressed:) forControlEvents:UIControlEventTouchDown];
self.view = myView;
}
-(void)pressed : (MyButton *)button
{
UIImageView *newImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"other-pic"] highlightedImage:nil];
myView.myButton.logImageView = newImageView;
[self.view setNeedsDisplay]; //----this does not work
}
When I press the button, the imageview does not change. How can I fix this?
Thank you very much!
First thing , you don't need setNeedsDisplay here. setNeedsDisplay is used for redrawing the view (it internally calls the drawRect: method of the view if required) which is not what you want.
Second, you have given the class of your button as MyButton,but in the actual .h and .m files, it is given as #interface PaiLifeReadingListButton and #implementation PaiLifeReadingListButton respectively.
Another thing, the view and button is not initialized with a frame. But I assume you have done that already, since your only problem is the image view not changing.
In your code in pressed method you should change views in the hierarchy, but you change only the object member. You can do something like
myView.myButton.logImageView.image = [UIImage imageNamed:#"other-pic"];

Add a subclass of subview to a subclass of UIView

I'm trying to do something very basic. I want to add a subView to my UIView subclass. I assume that I would put this in initWithFrame method as below, but view that are instances of this class do not draw this subview. What am I doing wrong?
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
redView = [[UIView alloc] initWithFrame:CGRectMake(10, 10, 20, 20)];
[redView setBackgroundColor:[UIColor redColor]];
[self addSubview:redView];
}
return self;
}
BTW redView is a property defined in the header of the sub class, like:
#property (strong, nonatomic) UIView *redView;
Thanks for reading!
You should place your initializing code inside:
- (id)initWithCoder:(NSCoder *)aDecoder { ... }
or
- (void)awakeFromNib { ... }
These methods are called when a view is loaded from nib. Don't forget to call [super ...] in the above methods.

Resources