UIScrollView and Storyboard - ios

I am testing out something I would like to have in my app. I have a UiViewController in a storyboard that has a UIScrollView - I now want to add other viewControllers to this scrollView and swipe between them.
I would like to add a view that I made in the storyboard into this UIScrollView. Is it possible?
I tried something along the lines of:
MYViewController *viewOne = [self.storyboard instantiateViewControllerWithIdentifier:#"myView"];
[self.scrollView addSubview:viewOne.view];
I've set the the scrollView size to be bigger than the screen and when the main view loads, I can see there is a scroll view (the scroll bars show) but my viewController is not inside it.
Anyone have any ideas?

The code you posted is still not right, on a couple of levels.
First, you should not use alloc/init for view controllers. You either need to use initWithNibName:bundle: (to create a view controller from a nib file) or instantiateViewControllerWithIdentifier: to load a view controller from a storyboard.
Second, you should not add a view controller's view as a subview of another view controller unless you use the parent/child view controller support that was added in iOS 5 and greatly improved in iOS 6. If you do what you are doing then all sorts of things won't work correctly: Auto-rotation, low memory warnings, background notifications etc. The list of things that can go wrong is unbounded.
The easiest way to do this is to add a container view as a frame to hold your child view controller, and then control-drag from your container view onto the scene that you want to set up as a child. This causes IB to set up and "Embed" segue. Embed segues do all the housekeeping you need to host one view controller's content inside another, with no code needed.
You could create a container view inside your scroll view's content view, and then it would just work fine.

I found the problem:
I was not allocating and initialising my viewController. Ooops.
This is the correct code:
BaseViewController *viewOne = [[BaseViewController alloc]init];
viewOne = [self.storyboard instantiateViewControllerWithIdentifier:#"myView"];
[self.scrollView addSubview:viewOne.view];

Related

How to add UITableView inside custom UIView?

I want to create common custom view with uitableview. I can do it using view controller and add subview as follows.
ViewController *vwCon=[[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
vwCon.view.center=self.view.center;
[self.view addSubview:vwCon.view];
But after scrolling table view app become crash. So how can I add uitableview inside custom UIView?
You shouldn't add a view controller as a subview - in fact Im not sure whether you are allowed to at all. You need to present a view controller modally, or push it as part of a navigation controller. I doubt you really want that though- instead just use a UIView here rather than a view controller. Then you can add that as a subview to another view.
You also mention tableviews though. Have you looked at the storyboard? Its easiest to do all that in there - just drag a UITableview object from the objects list on the right hand side, into the main view controller.

Set View Controller Subview Loaded from Storyboard

I have a custom view controller I load from a Storyboard. When I try to set one of its subviews nothing happens (remains generic white view). What I don't understand is if I try to set VC.view it works fine. Why is this? Everything seems to be initialized after I load from the Storyboard. Where would I set the VS's subview?
Yes, this slightly confusing behaviour is how it is 'supposed' to work. When a view controller is loaded, its view is not - at least not until it is actually needed. See this doc for further info. Only when the view controller is presented, will it then load the view. As you have found, this is tiresome, because you often want to set some of the the subviews' properties before it is presented (say in a prepareForSegue or prior to pushViewController: or presentViewController:).
There is a work around - based on what you have observed. If you directly access the view property, the view controller will immediately load the view and all its subviews. So, if you want to set subview properties, just "touch" the view itself:
NSLog(#"View tag is %i", viewController.view.tag);
and you should then be able to access the subviews.
Alternatively, you could pass the relevant data in (non-UI) properties of your view controller, and then set up the subviews using that data during viewDidLoad or viewWillAppear.

Possible to have a UINavigationController for a UITableView that doesn't fill the entire super UIView

This is on iOS5 w/ a storyboard.
I'd like to have a UINavigationController on the UITableView below but when I try the "embed in" option, it adds it to the Red UIView, not on the table itself. For lots of reasons this is not optimal. Is what I want to do not possible: to have a table subview with its own nav controller?
Oh - while I am here - what is the deal with UINavigationControllers not being able to be resized in a storyboard? I can only set "form" "page" or "full" - when I set it to "freeform" I am not able to enter any values to resize it
For lots of reasons this is not optimal
Actually, for lots of reasons what you are trying to do makes no sense. A UINavigationController has embedded within it (in Storyboard terms) an instance of a UIViewController. In other words, the nav controller's root view controller must be a view controller. Since UITableView is a subclass of UIView, you can't embed it inside a UINavigationController. And besides, you would never want to. A UINavigationController manages a hiearchy of view controllers. What are you trying to achieve that you think you need to put a UITableView inside of a UINavigationController? What you are probably trying to achieve is to place the view controller that the table view sits on inside a UINavigationController, in which case the result you're seeing in IB is the correct result.
UINavigationController is a view controller, not a view, so you can't embed it inside a view.
You should be able to get what you want with a little code: you can't embed it inside Xcode, but you can set up the UINavigationController and the red view separately, then write a few lines like this:
navigationController.view.frame = CGRectMake(20, 20, 280, 300);
[redView addSubview:navigationController.view];

Custom views with Storyboard

In complex screens (View Controllers) I used to separate the whole thing in smaller pieces (I call them widgets). These widgets consist basically of a MyWidget.h and a MyWidget.m file as well as a MyWidget.xib file, where the root element is a UIView and the MyWidget class is the File Owner of the UIView. In the init of this widget I do a loadNibNamed.
In my View Controller I then do a [[MyWidget alloc] init], which I add to View's Controller main view as a sub view. This, so far, works perfectly.
I'm now wondering, how to do the same with storyboard, because I cannot really start to drag in a UIView somewhere, I always have to start with an UIViewController, which I don't want to.
If there is no possible way doing this with a Storyboard, can I simply do it the old way, by using the Storyboard for my main screens and segues, and use a separate .xib file to define custom views?
Putting the widget/view in a separate .xib file works, and is appropriate especially if you might want to reference that same view from multiple View Controllers.
However, sometimes you do want to see the additional view/widget within the same storyboard, and it is possible. Here's how you do it:
Select your view controller in IB (click on the black bar below the view), then drag a UIView from the Object Library into the black bar:
When a view is in the black bar, it's instantiated like any other view in IB but just isn't added to your view hierarchy until you do so in code. Change the view's class to match your own subclass if necessary:
You can hook it up to your view controller like you would hook up any other view:
The added view shows up in your Document Outline and you can hook up actions and references there too:
Now, the problem that remains is that you can't actually see the view no matter how many times you try to click or double click, which would defeat the whole purpose of putting it in the same storyboard. Fortunately there are two workarounds that I know of.
The first workaround is to drag the view from the black bar back into your view controller's view, edit it, then drag it back into the black bar once you're done. This is troublesome but reliable.
The other workaround is more finicky, but I prefer it because it lets me see all my views at the same time:
Drag a UITableView from the Object Library into your newly added view.
Then drag a UITableViewCell into that UITableView.
Once you do that, your view pops out magically by the side, but you have a UITableView that you don't want. You can either resize that to 0x0, or you can delete it and your UIView will (usually) still stay visible.
Occasionally the secondary view will become hidden again in IB. You can repeat the above steps if you deleted the UITableView, or if the UITableView is still in the hierarchy you just need to click on the UITableViewCell and the view will appear again.
The second method works for UIViews but not so well for UIToolbars and is impossible for UIButtons, so the cleanest solution I've found when you need to include lots of different subviews is to attach a single secondary UIView to your view controller as a container that never gets shown, put all your secondary views in there, and use the UITableViewCell trick to make everything visible. I resize my dummy UITableView to 0x0 to make that invisible. Here's a screenshot of how it all looks like together:
If you're just looking to make your view controllers else-where(and not in your story-board), then there's a pretty simple way to accomplish this:
1) Create your CustomViewControllers(abcdController in the code I tried) with their individual xibs as usual.
2) Add a UIViewController(or whatever was the superclass of your CustomViewController) to the story-board.
3) Set the CustomClass to CustomViewController instead of UIViewController as shown here:
4) Finally, in your viewDidLoad, load the custom xib and you're done.
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSBundle mainBundle] loadNibNamed:#"abcdController" owner:self options:nil];
// Do any additional setup after loading the view from its nib.
}
I think you can do something like this to get instance of specific viewcontroller from Storyboard and use view on top of it.
ex:
MyViewController* myViewController = [[UIStoryboard storyboardWithName:#"Main" bundle:nil] instantiateViewControllerWithIdentifier:#"myViewController"];
UIView* view = myViewController.view; //Get the view from your StoryBoard.
Hope this helps
Thanks
Vijay

How can i import a view controller on a uiview controller?

May be it will a stupid for someone but i need the solution of this problem.I want to add a view controller on another view controller.If I explain it then,
My main view controller is containing a uitableview.
My another view controller containing a scrollview of buttons.
Now I want to set my scrollview controller at the top of the my main view controller [which is taking uitableview]. Thats mean [mainview addsubview:scrollview controller]
NOTE THAT: My scrollview is a view controller which is taking scrollview of some buttons.
If somebody give any example or source code or link of tutorial on these problem then it will very much helpful for me.
Thanks In advance.
EDIT:
Till now i have done...
In .h file
#import "scrollViewButtons.h"
scrollViewButtons *scrollButtonView;
#property (nonatomic,retain) scrollViewButtons *scrollButtonView;
In .m file
#synthesize scrollButtonView;
In viewDidLoad
scrollButtonView = [[scrollViewButtons alloc] initWithNibName:#"scrollViewButtons" bundle:nil];
CGRect frame = CGRectMake(0, 0, 320, 43);
scrollButtonView.view.frame = frame;
scrollButtonView.view.userInteractionEnabled =YES;
[self.view addSubview:scrollButtonView.view];
Now i can see the scrollButtonView in my main view but i can not find any user interaction.I can not scroll the scrollview of buttons.Can anybody tell me why i am not being able to interact with that scrollButtonView?
You are confusing views with view controllers. Apparently you have two views that have separate view controllers and you and you want these views to appear inside another view. That's very easy to do.
If you have a view controller, such as firstVC, then you can add the view that it controls by adding that view to another view, such as bigView. just use
[bigView addSubview:firstVC.view];
If you have a second view controller, such as secondVC, then you can add the view that it controls by adding that view to the another view, bigView. just use
[bigView addSubview:secondVC.view];
Now you only need to manage the view bigView with a view controller.
You can vary this, depending on your level of understanding and the level of complexity or simplification you want to have. Most view controllers managed several UIViews -- like labels, buttons, text fields, and other views. However, you may instantiate a second view controller (secondVC) inside the first view controller (firstVC) and then add the view of secondVC into the view of firstVC, like this:
[firstVC.view addSubView:secondVC.view];
I hope that helps.

Resources