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.
Related
I'm trying to create an IOS app that has one navigation view controller with several bar buttons, where each button changes the content of the view itself. Most of my views are custom views anyway, so I won't really see any clear previews in the storyboard anyway.
I'm thinking about creating one navigation view controller, and several NIB files to represent each view. Each NIB will have its own class with all the IBOutlets and IBActions.
Then, when a button is clicked in the navigation view controller, just switch to a new view by clearing the old one (removeFromSuperview), and call loadNibNamed to load a new view.
Does this sound reasonable or will all this deserializing be expensive in terms of CPU? Would it be better to just create different viewcontrollers in the storyboard, with segues etc, and copy those nav bar buttons to each view controller?
The simplest solution:
ViewController->
View->
rootViewOne
rootViewTwo
rootViewThree
...
upon your bar button's action,set specific rootView's hidden property to true or false:
self.rootViewOne.hidden = currentIndex == 0
self.rootViewTwo.hidden = currentIndex == 1
self.rootViewThree.hidden = currentIndex == 2
You can add your other view controller/s as child view controller.
In this way, you will have code for different views in their respective view controllers and your main/parent view controller will not be messy.
To add child view controller:
// Get instance of your view controller
UIViewController *childViewController = [UIViewController new];
// Add your view controller as a child view controller
[self addChildViewController:self.loginView];
// Set frame for your childViewController's view
[childViewController.view setFrame:CGRectMake(0.0f, 0.0f, self.view.frame.size.width, self.view.frame.size.height)];
// Add childViewController's view as a subview to your viewcontroller's view
[self.view addSubview:childViewController.view];
// Notify your main/parent controller that a child view controller is being added
[childViewController didMoveToParentViewController:self];
To remove child view controller:
// Notify the childViewController that it will be removed
[childViewController willMoveToParentViewController:nil];
// Remove childViewController's view
[childViewController.view removeFromSuperview];
// Remove childViewController from your main/parent view controller
[childViewController removeFromParentViewController];
Hope this helps.
UPDATE - Embedded swapping by Sandmoose Software:
http://sandmoose.com/post/35714028270/storyboards-with-custom-container-view-controllers
Here's a snippet from the "how-it-works" section:
The segue between ViewController and ContainerViewController is an
embed segue. The segues between ContainerViewController and its two
child view controllers is a custom segue. This custom segue is named
Empty and is a subclass of UIStoryboardSegue. It contains an empty
perform method. The ContainerViewController will take care of moving
the child view controllers into and out of place. However, the fake
segues are needed to create the connections in the storyboard.
If you’re like me you cringe at the thought of having a fake/empty
class like this but the benefit is that it allows us to stay firmly in
the world of storyboard idioms without resorting to programmatically
loading isolated storyboard scenes. It’s one tiny bit of ugliness
which allows us to preserve the usefulness and elegance of
storyboards. The storyboard still visually represents the scenes and
their relationships accurately. We can still use segues to control
what happens.
---------- Old answer below (phfffft!) ------
You pose several different questions here: one regarding custom buttons in the nav bar of a nav controller, another about assigning multiple views to the nav controller, and one about placing the view in separate XIB files, while maintaining IBOutlet connections and such.
The first one calls for a tab bar controller, which you can customize to look in whatever way you deem fit, even like a nav bar (ADC has ready-to-use sample code, "UIKit Catalog (iOS): Creating and Customizing UIKit Controls", which fits your needs).
The second can be done easily in Interface Builder by following any number of instructions freely available on YouTube, at Apple, and elsewhere on the Internet; you'll find it an extremely intuitive process. If you can drag-connect a tab bar controller to a view controller, you can intuit the rest. Otherwise, Apple has sample code for customizing the navigation or transition between views in a single view controller interface. "State Restoration of Child View Controllers" provides the best structure for what you want (just pull out the restoration-related code, and use the rest).
The third is not intuitive at all; and, as far as I know, there are only a couple of sets of instructions that exist for separating views (or other view controllers and their views) from a parent or top-level view controller. Here's one I haven't tried yet, so I'll be interested to hear how it worked for you:
http://digginginswift.com/2015/08/30/making-reusable-views-in-separate-xibs
The technique I use involves creating a storyboard with a single view controller to start, followed by a new view controller embedded in a container view for each of your intended views; then, deleting the view in each of the child view controllers, which leaves place for the views you set up in separate XIBs. They are connected by setting the File's Owner to the parent view controller. I can send you sample code, if you'd like to try a different approach than the aforementioned website offers.
In my app I have a shopping cart, when the user clicks the quantity button, I essentially want the picker (and an accompanying black background), to take up the entire screen space. How do I design this in storyboard? How do I place two full screen UIViews in one ViewController? There isn't enough space for my tableview and picker view to fit in the same view controller. And it doesn't seem like I can drag a UIView directly in storyboard.
Its possible to add views inside your main view of the view controller in storyboard.
Did you try to drag and drop it inside the view?
You can also create a view entirely in code as follows [i like do to it in storyboard as that makes adding constraints a piece of cake]:
Subclass UIView to customize your view ... say in class MyView
Then from your view controller's ViewDidLoad method you can create and add the view as such
self.myView = [[MyView alloc] initWithFrame:CGRectMake(0,0,[[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height)];
[self.view addSubview:self.myView];
// add constraints as needed]
If you are adding multiple views you will have to be careful about the ordering of the views especially if they are full page. A full page view will hide all other views under it.
The recommended way is that if you want the user action to show a full page view, you should probably create a New ViewController instead of a view.
Hth
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];
I'm struggling with this problem, so I need your help. Basically I've programmed a complex table view controller (with NSFetchedResults protocol etc) and already used it in my app and it worked great. Since I want now exactly this table view inside another view (so I can add a small subview at the bottom of the screen), I'm really struggling how to do this! I know by know how to embed a simple table view inside another view and link it to it's view controller.
But how can I do this in my case with as little effort as possible? I mean can I somehow use this table view controller I already have even though the superview must have its own view controller?! And how would I do that? Sorry I'm still a beginner :)
Since you already have a TableViewController. To add it as an subview to another ViewController's (self) view, do this:
TVC = <your tableViewController instance>;
[self addChildViewController:TVC];
TVC.view.frame = <your desired frame>;
[self.view addSubview:TVC.view];
adding the TVC as childViewController will forward UI methods of 'self' like willAppear and all to TVC.
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];