I have a view created in xib with UIImageviews and UILabel as the subviews. From my code, is it possible to create a new view object every time i run a loop.Please help me.
You need to create a new custom view named CustomView
Select class in XIB to CustomView.
Then load XIB name to a View object and can use in a loop
CustomView *aView = [[[NSBundle mainBundle] loadNibNamed:#"YourXibName" owner:nil options:nil] objectAtIndex:0];
When you have created a view and hooked it up via interface builder you can easily add views like
for(int i = 0; i < numberOfIterations; i++){
UIView * newView = [[UIView alloc] initWithFrame:MyFrame];
[myInterfaceBuilderView addSubview:newView];
}
Related
I am making a presentations app with that contains 50 slides. Each slide can contain buttons,videos,...
So I was thinking about doing it this way. I create 50 classes that are subclasses from UIView with each their own xib file. With this I can put different buttons on the UIView and use delegate methods to handle them.
Then I have one MainViewController with an header and footer image and in the middle one scrollView. I load up the XIB files like this.
UIView *rootView = [[[NSBundle mainBundle] loadNibNamed:#"page1" owner:self options:nil] objectAtIndex:0];
Put all these UIViews in an NSArray and next place them inside a scrollView by looping the NSArray
for (int i = 0; i < arrViews.count; i++) {
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * i;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
UIView *view = [arrViews objectAtIndex:i];
[view setFrame:frame];
[self.scrollView addSubview:view];
}
self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * arrViews.count,scrollView.frame.size.height);
So I have the following classes:
Page1.h (subclassed from UIVIew)
Page1.m
MainViewController.h (subclassed from UIViewController)
MainViewController.m
What my question is now, how can I say that the UIViews that I created (UIView *rootView) should use the classes of Page1.
In the storyboard you have to change a Class to a custom class. So for example in your page1 XIB you have to go to Identity Inspector (3th tab) and in Class text box type Page1, and for next page you have to do it again. In that case you can call rootView like:
Page1 *rootView = (Page1*)[[[NSBundle mainBundle] loadNibNamed:#"page1" owner:self options:nil] objectAtIndex:0];
Hope this help.
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).
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.
I am making an application, and I started with a single view controller with one view inside. I made my app but I want to have multiple pages and allow the user to swipe between them. I want to use a UISCrollView. How can I set up a UIScrollView so that I can insert my already made view from the xib. I don't want to make a view programmatically because a lot of it is graphical and I don't really know what I'm doing.
Recap: UIScrollView to swipe between views I have made in xib files
in your main view controller, add a scrollview. You can add scrollview to mainview in Xib.
In mainview controller .h file add a property , then load programmatically your views and add to scroll. Set each view position. Set scrollview reference in your main xib properly.
#property(nonatomic, weak) IBoutlet UIScrollView *scrollView;
UIView *view1 = [[NSBundle mainBundle] loadNibNamed:#"myViewXib1" owner:self options:nil];
UIView *view2 = [[NSBundle mainBundle] loadNibNamed:#"myViewXib2" owner:self options:nil];
UIView *view3 = [[NSBundle mainBundle] loadNibNamed:#"myViewXib3" owner:self options:nil];
CGRect frame;
frame = view1.frame;
frame.origin.x = 0;
view1.frame = frame;
frame = view2.frame;
frame.origin.x = view1.frame.size.width + view1.frame.origin.x ;
view2.frame = frame;
frame = view3.frame;
frame.origin.x = view2.frame.size.width + view2.frame.origin.x;
view3.frame = frame;
[scrolView addSubView:view1];
[scrolView addSubView:view2];
[scrolView addSubView:view3];
Declare IBOutlet in your view controller to point to that view.
#property(nonatomic, weak) IBoutlet UIView *myView;
Connect your view from xib to myView.
Load your xib in the view controller viewDidLoad method
[[NSBundle mainBundle] loadNibNamed:#"myViewXib" owner:self options:nil];
Then add myView to your scrollview.
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.