Design views in IB and use them few times with different content? - objective-c

is it possible to design a view in interfacebuilder and use it dynamically a few times?
the view structure is
- UIScrollView
- UIView
- UIImageView
- UILabel
now i want to dynamically create multiple instances of the UIView in a for loop with different UILabels and UIImageViews. I want to give them side-by-side in the imageview.
thanks + regards

Sure. Just call:
[[NSBundle mainBundle] loadNibNamed:#"myNibName" owner:self options:nil];
It returns an array that contains all top-level objects you have placed in the NIB file. You can then assign those objects to any of your instance variables/properties. See the documentation for details.

Related

Add tableView to a custom View

I'm sure this might be fairly easy but I'm unable to do it. I have a custom View xib
and on basis of either segment selection I want to show one of my two tableViews. When tableView xib is loaded with
_userProfileView = [[[NSBundle mainBundle] loadNibNamed:#"UserProfile" owner:_userProfileView
options:nil]firstObject];
tableViewDataSource and Delegate methods are called on CustomView not on UserProfileView.
Am I missing something here ?

iOS subview widget hooked up to multiple controllers

So, I want to create a reusable widget as a xib and subview that can appear on a set amount of specific screens. This widget will have three buttons, each with an Action.
I want to be able to handle these actions on multiple viewcontrollers.
So say ViewControllerA, ViewControllerD, and ViewControllerF can handle the three button events, each in their own way.
I've created the nib file. How do I import it into the specific viewcontrollers, and then how do I wire up those events?
EDIT: I know that I could potentially get outlets set up via a viewcontroller, but Apple states that UIViewController is for full-screen views only, and my widget is only taking up a small portion of the screen.
You have done correctly. And one thing is, In iOS it's not widget.It's a UIView.
(Sorry there may be any typo in my code.I have written myself in StackOverflow)
Follow Below Steps to finish it..
1) After you have created the xib for the view, then you need to have a UIView subclass files.. For example your xib name likes this CustomView.xib means then create a files like this CustomView.m and CustomView.h
2) In your CustomView.xib , You need to set the fileOwner as your CustomView.h.
3) In your CustomView.m file, there will be a method like initWithFrame: In that method you need to load your xib file like this
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"CustomView" owner:self options:0];
UIView *currentView = [topLevelObjects objectAtIndex:0];
[self addSubView:currentView];
4) Almost over. In any of your view controller, you can use this xib like
CustomView *newSubView = [[CustomView alloc]initWithFrame:CGRectMake(0,0,55,67)];
[self.view addSubView:newSubView];
That's it.. Go on..
You have created a nib file but make sure that you have also created the .h and .m file that will be the controller for that nib file. You will have to look up how to implement delegate methods which your other view controllers can capture and act on in their own way. Here is a great tutorial to get you started on making custom classes with custom delegates: Link

How to design separate UIView outside of any ViewController in Storyboard?

I would like to design a UIView, which is larger than a ViewController in Storyboard (iOS 5).
The UIView should be used as the subview of a UIScrollView and hence be larger than any of my existing ViewControllers. How can I create such a UIView in Storyboard and associate it with my UIScrollView?
I would like to do this without xib files if possible.
Thank you!
I see no other option than using xibs, but it's not that annoying:
//We have file called "View.xib" in our project. It contains one SINGLE view
NSArray *xibContents = [[NSBundle mainBundle] loadNibNamed:#"View" owner:self options:nil];
UIView *view = [xibContents lastObject]; //safer than objectAtIndex:0
[self.scrollview addSubview:view];
self.scrollview.contentSize = view.frame.size;
In order to make IB connections you can set the filesOwner class in the xib to be your viewController, and connect like usual.
You can place a UIView into your scrollview and directly design it inside the viewController of your scrollView
I've found the way to edit the view added to the scene (being at the same hierarchy level as ViewController).
Unfortunately it is from hackish types of actions.
My Xcode version is Version 4.5.2 (4G2008a). I've tested this in real project and new empty project.
The basic idea is that Xcode do have an ability to edit such external views, unfortunately this mode doesn't activate straightforwardly.
In the method that I've found you need to have 2 levels of hierarchy inside your external view:
Scene
|- VC
|- View
|- ExternalView
|- SubView1
|- SubView2
Then goto Document Outline panel
find SubView2 in the tree of your scene
double click it
The editing area will appear and its coordinates will be saved to project's user data, so you can move it to more suitable place if you want to, and next time you'll open the storyboard in IB on the machine it will be there. Although I think on other machines you'll have to do it again (I haven't tested that).

Subviews added programmatically to UIView in storyboards does not responds to action

I have a scroll view with many elements that I had to build as a separate xib because Storyboards graphic interface was clipping it and made me impossible to work with.
The xib is built with interface builder, setting the file's owner graphically to the same view controller allow me to ctrl-click and link between buttons and methods in it.
The view is added like this in the viewDidLoad method:
UIView *w = [[[NSBundle mainBundle] loadNibNamed:#"MainView" owner:self options:nil] objectAtIndex:0];
[self.myView.subviewolder addSubview:w];
where myView is the main view and subviewHolder is UIScrollView the container, both of them are linked to the controller, and the subview get added and display just fine. Self is of course the view controller.
What seems to not responds are the actions in view controller linked to the UIButtons I have in the subviews. I have put some breakpoints but the flow is just not passing there.
What am I missing ?
thanks
The answer was really simple, what I did wrong is to declare subviewholder much more smaller in height than the added subviews.
I thought that subviewholder would act as a placeholder, but instead it was causing the subviews to be displayed correctly but 'clipped' for what concerning the user interaction, therefore the IBAction was never called.

Cocoa touch - creating view template for iPad

This is kinda a hard question to describe.
I'm just starting to make an iPad app.
Now with the amount of real estate I have, I'm planning to have different but similar "views" to populate the space. (might be easier to think a newspaper site, where many of the columns are similar)
I would like to create a "view template" so I can reuse the view in the different spots.
Is it possible to design the view template in Interface Builder? (meaning I design the UIView in Interface Builder and then somehow I can just do [window addSubview:viewController.viewTemplate1]; multiple times?
If so how would I load that view template in the different places?
If you know of an example code / tutorial that does something similar, that would work too.
Thanks,
Tee
Strictly speaking, UIViewControllers, as Laurent suggested, are only meant for views that take up the whole screen. From the View Controller Programming Guide:
The one-to-one correspondence between a view controller and a screen is a very important consideration in the design of your application. You should not use multiple custom view controllers to manage different portions of the same screen.
The correct technique here would be to use the NSBundle method -loadNibNamed:owner:options:, which instantiates the contents of a NIB and returns an array of the top-level objects. In this case, for each of your views, you'd do something like this:
NSArray *nibObjects = [[NSBundle mainBundle] loadNibNamed:#"MyViewTemplate" owner:nil options:nil];
UIView *newView = [nibObjects objectAtIndex:0];
[myViews addObject:newView];
// set up the view and add it to your content view and so on
The simplest way is to create a XIB file with:
- The view template you want.
- An UIViewController subclass, that will act as File Owner.
- Bind the UIView to the UIViewController subclass.
With this method (which is used widely in the Apple's samples), you can load as many as views you want and place them wherever you want.