I have a UIViewController named LoginViewController, and it is a rootViewController in the AppDelegate. In the LoginViewController, I have two buttons: Login and Enroll.
When I tap Login, I assign a TabBarController as the rootViewController, then show the TabBarController. However, now I think I need to add another UIViewcController as a subview when I tap Enroll. I tried the following:
[self.view addsubview:viewcontroller.view];
But the problem here is My ViewController's view.top is pinned about 20 pixels below the top of the screen. I think there is an issue with the status bar, but I can't figure out how to fix it.
I think that I need to add my ViewController as a subview to the LoginViewController, then redirect from there to different views. Can someone please suggest other options?
Try to set frame to your enroll screen object then add it as a subview to loginview.
Ex:
[enrollViewcontroller.view setFrame:CGRectMake(0,0,320,440)];
[self.view addsubview:enrollViewcontroller.view];
You should not make a UIViewController a subview of another UIViewController's view. What you likely want to do if treat the subview as a normal UIView (if not both of those views) so that you only have one UIViewController on screen and it occupies the entire screen.
More here: How to add an UIViewController's view as subview
Instead of adding a UIViewController as a subview to another UIViewController, I have decided to present my ViewController as a ModalViewController using
[self presentModalViewController:myViewController animated:YES];
Related
I have a ViewController where I have placed another ViewController on one of it's SubView. In that ViewController (placed in SubView) I need to go to another ViewController on a click event. But when I can self.navigationController pushViewController method , My log says It's pushed but Screen is not changing.
My attached image depicts the scene , Match Details is the host ViewController. White rectangle is a UIView , and the guest ViewController is placed within this white rectangular View. When I click on the round button (Click written) , then another ViewController should replace the Host ViewController.
It's a complex application , so pasting codes here should not be a good idea. Just a few explanation would help me a lot , I can fix the codes myself. Some some hint required about my mistake.
Thanks in advance for help.
When you add view of viewController as subview you need also register your viewController as child view controller for parentViewController.
Example:
UIViewController *mainVC = [UIViewController new];
UIViewController *childVC = [UIViewController new];
[mainVC addChildViewController:childVC];
[mainVC.view addSubview:childVC.view];
UIViewController *parentVC = childVC.parentViewController;
Here parentVC is mainVC.
I have a UINavigationController inside it I have a UIViewController handling a ScrollView just to use addSubview: and I am loading several UIViewController into the ScrollView.
I have a button referring to pushViewController:animated: but it does nothing.
I used self.parentViewController and self.presentedViewController but ran into the same issue.
This answer is based off of the title of this question. The question however, is not really a question at all, really.
UIViewControllers must be pushed form a UINavigationController. You cannot use a UIScrollView to push a view. You can use animations to move UIViews within the UIScrollView.
Maybe self.navigationController or self.parentViewController.navigationController is pushing nil .
Initiate a UINavigationController:
UINavigationController * nav = [[UINavigationController alloc] initWithRootViewController:aViewController];
[self.navigationController pushViewController:nav animated:YES];
[nav release];
Try this. Hope this will help.
I got your problem. What should you do is create button or other view controllers dynamically and add them to UIView and then add this UIView to scrollView. You should add IBaction on runtime to every button and after that your navigation controller will work.
If your UIViewController is child of navigation controller then
[self.navigationController pushViewController:aViewController animated:YES]
will a push a viewcontroller over the present one.
Thats it!.
I have created a navigation based application.
I need to add a UIView before the navigation with the company details and have user to click on a button to enter the UINavigation view.
How can i do that?
Thanks.
Make the UINavigation's viewdidload show a modal viewcontroller, and give the viewcontroller a button to dismiss it.
You can add that UIView directly to the UIWindow, i.e. [self.window addSubview:self.myView]; and as you done with that UIView(after clicking on button), simply remove it from UIWindow and add UINavigationView to UIWindow.
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.
I am trying to remove two viewcontrollers (that have been added on top of each other) with one method. I have made the views in interfacebuilder. they all have their own .h and .m files to go with it.
Scenario I am in:
I have a main menu which has the view2 header file imported.
In a method I add the second view on top of the superview like so
view2ViewController * view2 = [[view2ViewController alloc] initWithNibName:#"view2ViewController" bundle:nil];
[self.view addSubview:view2.view];
then in view 2 I have added the view 3 header file so i can add view 3 as a subview ontop of view2. i have another method which is connected again to interface builder to a UIButton so upon button press a method gets called in view2 which adds view 3 on top in exactly the same way like so:
view3ViewController * view3 = [[view3ViewController alloc] initWithNibName:#"view3ViewController" bundle:nil];
[self.view addSubview:view3.view];
What im trying to solve:
I have a button in view 3 which should remove view 3.... and then it should also remove view 2 aswell so the main screen is visible.
How can this be achieved?
What I have so far:
[self.view removeFromSuperview];
This however only removes View 3... but leaves view 2 in place.
What needs to be modified so that i can remove view 2 as well??
Any help is appreciated.
I would normally do this as adding view2 and view3 as subviews of the main view. Then when the button actions are triggered, the adding and removing of subviews will be executed by the main view's view controller.
For a quick hack, I think you can try this in your button handler.
[self.view removeFromSuperview];
[self.view.superview removeFromSuperview];
Though I'm not sure if you should be doing it. :P
Is this what you need?
[[[self.view subviews] makeObjectsPerformSelector:#selector(removeFromSuperview)];
I don't know what you're trying to do exactly, but I get the impression that pushing a new view controller is what you want. If you have a UINavigationController in your app, you'd just have to do a
[navigationController pushViewController:view2 aimated:YES]
To go back to the main menu when the button is pressed, you should define a delegate protocol that looks something like this::
#protocol View3ViewControllerDelegate
- (void)view3ControllerBackToMainMenuButtonPressed:(View3ViewController*)controller;
#end
This protocol is then implemented by the class that actually pushes the other view controllers. In the implementation, you'd pop all view controllers you don't want to be displayed anymore. To do this, you could use
[navigationController popToViewController:myMainMenuViewController animated:YES]
or if your main menu view controller is actually the root view controller:
[navigationController [navigationController popToRootViewControllerAnimated:YES]
That way only one class is responsible for pushing and popping all view controllers and handling that "Back to Main Menu" button. Using a custom protocol as described above is the recommended way to handle the popping of pushed view controllers in a scenario like this.
Hope that helps!