How refresh a single control in xib? - ios

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

Related

Change height of UITableView which is in custom UIView

I have a tableview which is inside a custom class UIView. This custom UIView is then added to a different UIViewController. If I change the height of the table view inside the xib file then it corresponds and changes when the app is run. I am trying to programatically change the height of this tableview though depending on how many cells it has.
Normally i would do a tableview.frame=CGRectMake(15,29,290,height) and this would change the size but it does not seem to be working.
I have also tried the [tableView setFrame:CGRectMake(15,29,290,height)]; and this also doesnt work. I have tried it inside the method in the custom uiview that sets up the custom view and I have have also tried it within the UIViewController of which the view is added. Here is my code in the UIViewController that adds the custom uiview.
LeagueInvites* invitedView =[[[NSBundle mainBundle] loadNibNamed:#"InvitedView"
owner:nil
options:nil] objectAtIndex:0];
[invitedView setUpLeagueInvite:allInvites :obj.nUserName];
invitedView.frame=CGRectMake(0, (615 +height), 320, 29+([allInvites count]*120));
[invitedView.tableView setFrame:CGRectMake(0, 29, 290, 400)];
[self.fullScroll addSubview:invitedView];
Any help would be hugely appreciated.

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.

Subviews for custom UIView with Nib (.xib) don't load?

I am trying to create a custom UIView class with an associated Nib (.xib) file to help with layout and creation.
Right now, no subviews in my custom view appear, even though they exist in my nib file and are hooked up properly to my custom view class via IBOutlets.
I tried going through the following other answer:
Load custom UIView with XIB from a View Controller's view using IB
and this one:
How do I get a view in Interface Builder to load a custom view in another nib?
and this one:
http://soulwithmobiletechnology.blogspot.com/2011/06/create-uiview-with-nib-file-in-xcode-4.html
but the method [[NSBundle mainBundle] loadNibNamed:#"MyView" owner:self options:nil] seems to call -[initWithCoder:], leading to an infinite loop.
Please let me know what other details to include to help solve this bug!
UPDATE 1: Do I need to lay out my subviews via -[layoutSubviews]? Does my nib file actually not lay it out? If so, then what is the point of the nib file?
Open any of your view controllers and paste this code into your viewDidAppear method:
NSArray * allTheViewsInMyNIB = [[NSBundle mainBundle] loadNibNamed:#"MyView" owner:self options:nil]; // loading nib (might contain more than one view)
MyCustomView* problemView = allTheViewsInMyNIB[0]; // getting desired view
problemView.frame = CGRectMake(0,0,100,100); //setting the frame
[self.view addSubview:problemView]; // showing it to user
That's all you have to do, to present a view. If you want to interact with this view — you have to declare MyCustomView* problemView in your class' interface, not in the method.

UILabels Blocking Cell Selection on Custom UITableViewCell

I have 4 custom UITableViewCell subclasses generated from nibs that all include UILabels. I noticed that I am not able to select the cell when I tap on the frame of the UIlabel. Is there a way around this issue?
I believe that you can set userInteractionEnabled to NO so touches aren't intercepted by the UILabels.
Go into UIBuilder and select the labels, and make sure that userInteractionEnabled (it's under the view properties) is set to no.
OK I figured it out. I needed to set the entire UITableViewCell subclass nib to userinteractionenabled = NO. In my init method of each subclass I added the following code:
[[NSBundle mainBundle] loadNibNamed:#"ROIUITableViewCellType1"
owner:self
options:nil];
[self addSubview:self.mainView];
The subview from the nib obscured the cell which was responding to tap events. I am not sure if this is the proper way to load a nib for a UITableViewCell subclass which may have caused my problem. Any thoughts?

Dynamically create multiple instances of a UIView from a NIB

I have a grid on screen. It is an instance of a UIView subclass. In cells in the grid I want to add instances of subviews - another custom subclass of UIView - as the user interacts with the app. The subview itself is something I want to design in an XIB. Its controls represent state for that particular cell. It consists of a couple of UIControls (say a label and a button).
I already know I can load NIBs dynamically using
[[NSBundle mainBundle] loadNibNamed:#"MyNIB" owner:self options:nil];
and I know they'll work if I set the File's Owner property in the NIB to the view controller that's responsible for the view these subviews will be added to. I use this to add some external NIBs to a UIScrollview in the main app screen.
What I want to know is how can I do this dynamically? I want to say something like:
MySubviewCell * sv = [[NSBundle mainBundle] loadNibNamed:#"MyNIB" owner:self options:nil];
[sv setFoo:#"Foo"];
[sv setBar:123];
[sv setFrame:myrect];
[mainView addSubview:sv];
But of course loadNibNamed doesn't return me the subview instance, instead it returns an array of all the controls in the view.
EDIT File's Owner in the MySubviewCell NIB will be a problem: there isn't one, and there can't be: I don't know how many I'll need. Should I be using something like an ArrayController?
EDIT 2 Please ignore the previous edit; I've left it in as the answer refers to it but it's not the problem I thought it was.
Can I do this?
Thanks
This is commonly used when designing table view cells in a nib. You've got two options:
The object at index 0 of the returned array will be the "root" object in your nib.
Create an outlet in your view controller of type MySubviewCell. Set your file's owner class in the nib to your view controller, and link the outlet to the element in the nib you are interested in. When you load the nib with owner self, the outlet becomes populated. You can then configure it, add it to your array, and set the outlet back to nil to prepare to load the nib again for the next instance.
Try to access to first object in that array:
MySubviewCell * sv = (MySubviewCell*)[[[NSBundle mainBundle] loadNibNamed:#"MyNIB"
owner:self
options:nil]
objectAtIndex:0];

Resources