How to load UILabel from UIView? - ios

I have a problem with UILabel and UIView. In my UIViewController I load a UIView:
NSArray *first = [[NSBundle mainBundle] loadNibNamed:#"customView" owner:self options:nil];
UIView *detail = [first objectAtIndex:0];
Inside the Xib of the UIView that i have already loaded (#"customView") there is a UILabel. How can i load also this UILabel and change the text?
Thanks

If in the XIB the root view has a subview then that is also loaded when you load the XIB. You have a few options for connecting to it:
Add an outlet to the file owner in the XIB (this is your view controller)
Add an outlet to the root view that the view controller can use to get the label
Use the tag on the label and viewWithTag: to search for it
Arguably option 2 is the best from that list as it is the best containment of knowledge and the most reusable.

You can tag the UILabel inside of the .xib file and then access the UILabel like this:
(Assume you have tagged the UILabel 42)
UILabel *label = [detail viewWithTag:42];
You can then change the text as you would with any other UILabel:
label.text = #"Text for the label";

In the XIB file you can create an outlet to the file's owner. Then you can access the property of the view controller, since it is the file's owner of the XIB (set in your first line of code).

Related

How to add an UIView from a XIB file as a subview to another Xib file

I am trying to add a custom UIView that I created in XIB, to my view controller in my main.storyboard as a subview. How can I do this?
matchScrollView (tag 1) is the UIScrollView in view controller in main.storyboard, while matchView (tag 2) is the custom UIView I created in another XIB file.
With the press of a button i want to have the custom UIView added to the UIScrollView as a subview. But how can i actually make it show up on display? I guess i have yet to alloc and init it, along with indicate position and such, but how can i do that? I tried different ways without success. I can create UIViews programmatically, but have yet to find a way to just load the UIView from XIB.
-(IBAction) buttonTapped:(id)sender {
UIScrollView *matchScrollView = (UIScrollView *) [self.view viewWithTag:1];
UIView *matchView = (UIView *) [self.view viewWithTag:2];
[matchScrollView addSubview:matchView];
}
The reason that I am creating my custom UIView in another XIB file instead of directly implementing it on my main.storyboard view controller, is because I want to re-use the same view multiple times. So the UIScrollView has a numerous subviews of UIViews.
I was hoping I could create numerous instances of MatchView and add them all to matchScrollView as subviews.
The issue you are having is completely normal. The way Apple designed it doesn't allow to reuse custom views with their own xib into other xibs.
Lets say you have a custom view named HeaderView with a custom xib named HeaderView.xib. And lets say you want to be able to, in another xib named GlobalView.xib, drag a subview and specify its class to be of type HeaderView expecting it to load that view from HeaderView.xib and insert it inplace. You can do it like this:
A) Make sure File's Owner in HeaderView.xib is set to be HeaderView class.
B) Go to your GlobalView.xib, drag the subview and make it of class HeaderView.
C) In HeaverView.m implement initWithCoder, if after loading the view there aren't subviews means it got loaded from GlobalView, then load it manually from the correct nib, connect the IBOutlets and set the frame and autoresizingmasks if you want to use the GlobalView's frame (this is usually what you want).
- (id)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self.subviews.count == 0) { //means view got loaded from GlobalView.xib or other external nib, cause there aren't any subviews
HeaverView *viewFromNib = [[NSBundle mainBundle] loadNibNamed:#"HeaverView" owner:self options:nil].firstObject;
//Now connect IBOutlets
self.myLabel1 = viewFromNib.myLabel1;
self.myLabel2 = viewFromNib.myLabel2;
self.myLabel3 = viewFromNib.myLabel3;
[viewFromNib setFrame:self.bounds];
[viewFromNib setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];
[self addSubview:viewFromNib];
}
return self;
}

UIView in a custom xib

I created a UIView in a separate subclass of UIView and with an xib.
I have a UIViewController with a UIView in storyboard and I want to set custom UIView to this UIView.
I modified class name in storyboard UIView to the class name of custom UIView. I am getting the UIView on the result but with the frame size of custom UIView. I added correct constraints in the custom xib. and some touch gestures not working as desired because of the wrong frame etc.
Please suggest me if anything wrong I did.
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self) {
// Load the UIView from Interface Builder
[[NSBundle mainBundle] loadNibNamed:#"TMPhotoEdit" owner:self options:nil];
// Add UIView to current UIView object.
[self addSubview:self.view];
}
return self;
}
self.view is the same view object I created a variable for it.
I want the frame to set as same as the one i am using in storyboard.
Solution to get it work using code also helps me a lot. I mean, without IBOutlet in storyboard, just adding this UIView with some frame set to it.
If I call initWithFrame, I can't set the xib like what I am doing with initWithCoder above and initWithCoder is not being called if I use initWithFrame in code.
Assuming you have an instance of the view you want to resemble in size
self.view.frame = CGRectMake(otherView.frame.x, otherView.frame.y, otherView.frame.width, otherView.frame.height);
Actually what you are doing is not correct but anyway you should load the custom view nib like this
self.view = [[NSBundle mainBundle] loadNibNamed:#"TMPhotoEdit" owner:self options:nil][0];
Again this is not correct, why do you need it anyway? but it shall solve your nib loading issue.

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.

Add a subview in xib to a custom UIView

First, I have a exist Asset.m/Asset.h, it is the subclass of UIView. Then I create a empty xib file and change xib's file's owner class to Asset. Then I create a view and a label to this xib, and I can drag and create IBOutlet in UIView.
IBOutlet UIView *videoOverlay;
IBOutlet UILabel *videoTimeLable;
Then I want to add videoOverlay to Asset.m's view using addSubview.
self = [super initWithFrame:CGRectMake(0, 0, 0, 0)];
[self addSubview:videoOverlay];
But the view -- videoOverlay do not show up.
I print out size of videoOverlay, all 0:
NSLog(#"width =%f height= %f", videoOverlay.frame.size.width, videoOverlay.frame.size.height);
Why ?
Finally, I find the solution by myself. Use under code to add this view (videoOverlay and videoTimeLable belong to this view )in:
UIView *thumbs;
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"ELCAsset" owner:self options:nil];
thumbs = [nib objectAtIndex:0];
[self addSubview:thumbs];
then videoOverlay and videoTimeLable can show up.

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

Resources