I want to add a UIViewController class to an existing class as a subview, so i used the following code.
myEventsView = [[EventNameViewController alloc]initWithNibName:#"EventNameViewController" bundle:nil];
[myEventsView.view setFrame:CGRectMake(0, 39, 320, 400)];
[self.view addSubview:myEventsView.view];
It is added successfully but the problem is with button actions, those actions which have to navigate to other view controllers are not working. Those action methods are called, but action not performed, not getting why so, please guide for the above.
Thanks in advance.
Try
myEventsView = [[EventNameViewController alloc]initWithNibName:#"EventNameViewController" bundle:nil];
[myEventsView.view setFrame:CGRectMake(0, 39, 320, 400)];
[myEventsView willMoveToParentViewController:self];
[self.view addSubview:myEventsView.view];
[self addChildViewController:myEventsView];
[myEventsView didMoveToParentViewController:self];
From the docs:
Each custom view controller object you create is responsible for managing all of the views in a single view hierarchy.
What you are trying to do is to add one viewcontroller's view as a subview of another viewcontroller's view, hence mixing two view hierarchies. That will cause you problems as you have already experienced. Have a look at Carbon Emitter's Abusing UIViewCtrollers article, it gives a thorough explanation and an alternative.
Update
As suggested in the correct answer above, there exists a way to make one UIViewController act as a container for another. From Implementing a Container View Controller (UIViewController Class Reference):
A custom UIViewController subclass can also act as a container view controller. A container view controller manages the presentation of content of other view controllers it owns, also known as its child view controllers. A child’s view can be presented as-is or in conjunction with views owned by the container view controller.
However, this is an iOS >= 5 feature, so my answer will remain correct for applications running iOS versions < 5.
Muncken is right. You cannot add myEventsView.view as a subview to self.view, since myEventsView.view is controlled by its own view controller.
What you probably wanted to do is to add just an new view (that is not controlled by another view controller) as a subview to self.view. So, why don't you just instantiate such a view without a new view controller?
Related
I am developing a navigation based application in iPhone.
In which I want to create physical menu button like menu like the one in Android phones.
The menu can be accessed from any view in the hierarchy.
Do any one have any idea how can we achieve this .
Can we add a drawer like menu to UINavigationBar ?
Please don't suggest the tabbarcontroller as it is the last option.
What you should do is create a subclass of UIViewController, e.g. MasterViewController. Have all of the view controllers subclass MasterViewController. In the MasterViewController you can write code to create and display your drawer view, and then all of your view controllers will inherit that code and can call it on themselves at any point.
To slide the view in:
drawerView.frame = CGRectMake(0, -1*drawerView.frame.size.height, self.view.frame.size.width, whateverHeight);
[UIView beginAnimations:nil context:NULL];
drawerView.frame = CGRectMake(0, 0, self.view.frame.size.width, whateverHeight);
[UIView commitAnimations];
I think that what you want is just set the titleView of UINavigationItem to an arbitrary UIView to handle user taps.
This doesn't have to be a text label :)
If that's the case then I'd suggest you taking sharedInstance with flags tracking the hierarchy of the view controller stack. Every time when user navigate just update your sharedInstance and do the custom menu preparation and populate it. The good practice would be reuse your UI component rather than making it new on every navigation operation.
You can create a "DrawerView" class and import it into where you will want to use it. Then you can use presentModalViewController: myDrawer to make it appear when you need it. Hope that makes sense.
Its very simple..
In one XIB u can add many views right. now add two main views like say data view and down side button view u want in. (if m nt wronge with 4 buttons to navigate) now keep ur this view at top . and all the other hirarchy of views u can add in that data view.
I think you should create a singleton UIViewController or UIView for this. Advantage of the UIViewController is that it can be presented with the presentModalViewController:animated: method, and also if you need, you can push it to your navigation stack. Also an advantage that you have a UIView instance in the same time.
Using the singleton pattern, you can reach your class anywhere.
Check this link from the official documentation about singleton in objective-c: Creating a Singleton Instance
Or you can use Matt Gallagher's macro (because this is very comfortable to use): Singleton macro
If you can get iOS 5 beta, I highly recommend it. The new beta dev release has an extension on UIViewController, dubbed by Apple as container view controllers. They are very easy to set up, and I am using one in my app.
Container view controllers work like UINavigationControllers and UITabBarControllers in that they allow you to add child view controllers, not just child views. Like I've said, they are simple to set up. I'll explain how:
Create a new class called ContainerController. Add an initialization method called - (id)initWithNavigationController:(UINavigationController *)controller. It's implementation should look something like this:
if ((self = [super init])) {
[self addChildViewController:controller];
controller.view.frame = CGRectMake(0, 0, 320, 420); // Leave room for the menu at bottom of the screen
[self.view addSubview:controller.view];
}
The reason we are adding a navigation controller is because it keeps this container controller from doing transitions, which is more complicated.
In the ContainerController's viewDidLoad method, create the menu:
[super viewDidLoad];
UIView *menuWrapperView = [[[UIView alloc] init] autorelease];
menuWrapperView.frame = CGRectMake(0, 420, 320, 40); // Place a wrapper for the menu buttons directly below the navigation controller
UIButton *button1 = ...;
UIButton *button2 = ...;
[menuWrapperView addSubview:button1];
[menuWrapperView addSubview:button2];
[self.view addSubview:menuWrapperView];
With this approach (using child view controllers), your views and navigation controller will get their needed memory warning, view loading, and rotation handling method calls when needed.
New to iPhone development, but I've been given a big project as a first go and I'm a bit stuck.
Basically the app will start with a settings screen, then you click a button to go to a dashboard with multiple option buttons. Each button will lead to a different Navigation View with tables.
The way I've approached this is to start with a UIViewController with a button, which I've got wired up but when you hit the button and I do:
[self.view removeFromSuperview];
UIViewController *newView = [[UIViewController alloc] initWithNibName:#"Dashboard" bundle:nil];
[self.view addSubview:newView.view];
the second view isn't loading. I just get a blank screen. Do I need to make a reference in the first controller to the second?
Also, am I approaching this in the right way? As long as I removeFromSuperview will I be able to load the navigation controllers on the press of a button?
Sorry if this isn't too clear, I've been through books and lots of websites but don't seem to be able to get my head around this.
Thanks
There is nothing here with the new view, rather the problem is with current view. You have removed the self.view from super view.
[self.view removeFromSuperview];
So anything added to self.view will not be shown, as self.view itself is removed.
When presenting child controller/view from a parent controller, you should consider using presentViewController. Eventually, use dismissViewControllerAnimated when you want child to disappear and parent to reappear.
In parent view controller:
ChildViewController * child = [[ChildViewController alloc] init];
[self presentViewController:child animation:YES completion:Nil];
In child view controller, ie. in some action handler:
-(IBAction)close:(id)sender
{
[self dismissViewControllerAnimated:YES completion:Nil];
}
IMHO you should also get in the habit of naming instance variables to what they are instantiated from. In your example you name the instance newView, when it should be something like newViewController. That way you make sure you don't mix up views with view controllers.
[self.view removeFromSuperview];
You've removed the view from the superview
[self.view addSubview:newView.view];
But you're adding the new view to the same view that you have just removed from the superview. It's not displaying anywhere.
Your third line adds newView as a subview of self.view, but you just removed self.view from it's superview.
I'd suggest reading more about view controllers. You'll want to have one view controller per "screen", so one for your settings screen, one for your dashboard, one for each table, and so on. Then, manage which one is visible by pushing and popping these view controllers from the nav controller's stack.
This removes self.view, which will most likely destroy the object since there will be no other references to it:
[self.view removeFromSuperview];
Here you are creating an UIViewController, and adding it's view to self.view, which is probably not what you want:
UIViewController *newView = [[UIViewController alloc] initWithNibName:#"Dashboard" bundle:nil];
[self.view addSubview:newView.view];
Look into UINavigationController so that you can easily swap screens in and out with some built in animations. Here's a bit more about them. Here's a tutorial.
The UIViewController's view should not be removed from or added to a view hierarchy outside the control of the view controller. While you might be able to get that manipulation to work now it won't in the future.
Read up on view controllers here.
The basic idea is that you present the view controller then it will take care of manipulating the view hierarchy for you.
So a better approach to get started would be to do something like this;
[viewController1 presentModalViewController:viewController2 animated:YES];
This line of code will present viewController2 with the default modal animation (slide in from the bottom). If you'd like a different animation you can change the modalPresentationStyle to one of the constants in the UIModalPresentationStyle enum on viewController1 (note thats a viewController1, not viewController2).
If you want something more like the Clock app look into the tab bar controller. If you want something more like the Mail app look into the navigation controller.
Can I load a UiViewController in a UIView in another UIViewcontrller.
suppose UIViewControllerA has a UIView named subuiview. Can I load UIViewControllerB into subbuiview?
Thanks!
Starting with iOS 5
"Container view controllers" have been added in iOS 5. You can add a view controller as a child of another one with addChildViewController:.
You also are responsible for adding its view to its parent's view.
Everything is well documented in the iOS SDK documentation: Implementing a Custom Container View Controller.
To add a child view controller:
childViewController.frame = ...
[self.view addSubview:childViewController.view];
[self addChildViewController:childViewController];
[childViewController didMoveToParentViewController:self];
and to remove it:
[self willMoveToParentViewController:nil];
[self.view removeFromSuperview];
[self removeFromParentViewController];
Prior to iOS 5
It's possible to load another view controller and add its view as a subview of another controller's view.
UIViewController *subController = ...
[self.view addSubview:subController.view];
Although it's not recommended by Apple's guidelines:
Each custom view controller object you
create is responsible for managing all
of the views in a single view
hierarchy. [...] The one-to-one
correspondence between a view
controller and the views in its view
hierarchy is the key design
consideration. You should not use
multiple custom view controllers to
manage different portions of the same
view hierarchy.
(from the View Controller Programming Guide)
Your sub-controller won't receive rotation events, or viewWillAppear, viewWillDisappear, etc (except viewDidLoad).
So Apple advises us to use a single view controller managing the entire view hierarchy (but doesn't forbid to use multiple ones).
Each view may still be a custom subclass of UIView. Maybe you don't need another view controller but rather a custom view.
[self addSubview:viewControllerB.view];
try this in the sub view
It has always been problematic to simply use addSubview to add a view controller's view as a subview of another's. It's especially bad when people use it to transition between views, rather than relying upon other, more robust, solutions like presentViewController or pushViewController.
If you really want to add one view controller's view as a subview of another's, iOS5 introduced "view controller containment". Containment is discussed in the View Controller Programming Guide as well as WWDC 2011 session 102. Bottom line, you want to ensure you keep your view controller hierarchy synchronized with your view hierarchy, by calls to addChildViewController, didMoveToParentViewController, etc. See the documentation and the video for specifics.
Well you cant technically load a viewcontroller. You can load a viewcontroller's view as a subview of any view.
You need to also retain the view controller incase you have any actions attached to it. Otherwise it may result in crashes.
addsubview:controller.view is the method you want to look at.
I am sure this is an easy question, but one that has escaped me for some time now.
Say I have a UIViewController, either defined as a root in an XIB or on a stack. At some point in my code I want to replace it with another view controller. Just flat out replace it. How would I do that?
I have tried defining the controller and assigning, but not sure what actually makes it push on the screen with the absence of a navigation controller.
I think when you say that you want to replace the view controller, what you actually mean is that you want to replace the view. Bear in mind that view controllers aren't visible, but every view controller maps to a view, which can become visible by getting added as a subview of a visible view.
Your solution of replacing self.view with the new view controller's view may work in your particular case, but it's probably not the "correct" answer to your question. There are going to be cases where this solution won't work for you.
Let's say you have a simple view based application with no navigation controller and no tab bar controller. In your app delegate you construct an instance of YourFirstViewController, and you call [window addSubview:yourFirstController];. Your view hierarchy now consists of a UIWindow with a single subview -- the view for YourFirstViewController.
Now let's say the user presses a button on that view, which is handled by an IBAction defined in YourFirstViewController. You want to respond by "replacing" YourFirstViewController's view with a view associated with YourSecondViewController. I put "replacing" in quotes because we more commonly present a view by pushing its view controller onto a navigation stack, or calling presentModalViewController:animated: to present the view modally, but let's assume that you've rejected those options for some reason, and you actually do want to manually replace YourFirstViewController's view with YourSecondViewController's view.
This is a simple matter of manipulating the view hierarchy. You want to remove YourFirstViewController's view from its superview (the UIWindow in this case), and you want to add YourSecondViewController's view as a subview to replace it. Your action would therefore look something like this:
- (IBAction)replaceButtonClicked {
UIView *mySuperview = self.view.superview;
YourSecondViewController *secondController = [[YourSecondViewController alloc] init];
[mySuperview addSubview:secondController.view];
[self.view removeFromSuperview];
[secondController release];
}
When we use a methods like -pushViewController:animated: or -presentModalViewController, the receiving controller manipulates the view hierarchy for us. This may make it seem like we're looking at view controllers on the screen, but we're not. We're just looking at a big hierarchy of nested views going all the way up to a UIWindow at the top.
You can present a new view controller modally:
[self presentModalViewController:aViewController animated:YES];
This won't outright replace the current VC, but it will display a new view over the current view.
I think I'm missing something fundamental and so I want to ask the community for some help. I'm building an app based around a basic iPhone Utility Application. My MainView and FlipsideView share some elements so I have created separate ViewControllers and nib files for those pieces. In order to do this I have done the following:
1. Created a viewcontroller called searchDateViewController which is the file's owner of searchDateView.xib
2. searchDateView.xib is basically a UIView with a UILabel inside, the view is wired up correctly
3. Inside both MainViewController.m and FlipsideViewController.m I add a subview as folllows:
- (void)loadView{
[super loadView];
searchDateViewController = [[SearchDateViewController alloc] initWithNibName:#"SearchDateView" bundle:nil];
[[searchDateViewController view] setFrame:[searchDateView frame]];
[[self view] addSubview:[searchDateViewController view]];
...
}
Everything displays and works just fine. Basically depending on actions that happen in each of the main and flipside views the UILabel of the nib is changed. However, I wanted to do something slightly different if the searchDateViewController is loaded from the MainView or the FlipsideView. However, I can't seem to figure out which ViewController is adding the searchDateViewController subview.
In searchDateViewController I tried:
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(#"superview %#", self.view.superview);
NSLog(#"parentviewcontroller %#", self.parentViewController);
}
In both cases I get nil.
So my question is - can I find out which ViewController is adding searchDateViewController a a subview? If so how? Or if my logic here is completely messed up, how should I be doing this?
Thanks!
viewDidLoad is invoked when the view controller has loaded its view. In your case, that happends in this line:
[[searchDateViewController view] setFrame:[searchDateView frame]];
At that moment, you haven't yet called addSubview: so it is no wonder the view's superview is nil.
To solve your problem, you should define a property inside SearchDateViewController to distinguish between the different cases. This property would then be set accordingly by the parent controller that creates the SearchDateViewController instance.
Generally, I do not think it is a good idea to use a UIViewController subclass as a controller for a view that is used as a subview of one or several fullscreen views rather than be used as a fullscreen view itself. Much of UIViewController's logic works on the assumption that it is used to manage a fullscreen view. For instance, with your design, I think it's possible that SearchDateViewController will modify the view's frame when the device orientation changes etc. Since you don't need all this functionality for a non-fullscreen subview, I suggest you subclass your SearchDateViewController directly from NSObject.
ViewController and views are completely separate.
In most cases, when you add a subview to a parent view you don't add its controller to the parent's viewController. The exception to this rule is the navigation controller which adds the controller instead of the view to maintain a hierarchy of view controllers.
Your SearchDate viewController can't find a parent controller because you never assigned one and the system does not do it automatically. You can just assign a parent controller when you evoke the view from another controller.
searchDateViewController.parentController=self;