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!.
Related
I have a MainMenu which navigates me to a TabBarController with 4 views (4 Tabs) in it.
Now I've a button in the 4th view of the TabBarController which OnClick should take me to the MainMenu.
Problem is when I pushViewController (MainMenu) I'm not able to dismiss the TabBarController and also NavigationBar is not visible on the MainMenu!!
Can someone please suggest me how to solve this, Thanks in advance.
Do not use this :
[self.navigationController pushViewController:mainMenuViewController animated:YES];
Try using this:
[self.navigationController popViewController Animated:YES];
Take the reference of your same MainMenu and then push to that view controller
[self.parentViewController.navigationController popViewController animated:YES];
UITabBarController is a container controller and you're adding viewControllers into it like so:
NavController
||
TabBarController
||
DisplayingViewController
since displayingViewController doesn't have a navigationController associated with it but the TabBarController does, you need to call for its parentViewController.
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];
I have a navigation controller with a rootview.
The rootview contains buttons, which push new views onto the navigation controller correctly.
However, on this rootview, I also have subview (it's a scrolling preview like the appStore images, view made of 3 UIview items). On these UIViews, there is a button which I'd like to change the rootview navigation controller, like the other buttons.
At the moment, it builds and runs, but the new view is not pushed. Any ideas?
Click method on on the ScrollItem button:
MyViewController *newView = [[MyViewController alloc] initWithNibName:#"ManageMyPain" bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:newView animated:YES];
... i guess it is because, self doesn't have a navigation controller, but the rootview does.
Thanks!!
The property "navigationController" is only being set in a view controller if he's pushed to the navigation controllers stack. your second view is a sub view of a push viewcontroller which means this property is NULL and will not do anything if you try pushing things to it.
Yes, it is because self.navigationController will be nil if you didn't push that controller on navigations stack or if you didn't set it manually.
So you just need to have reference to rootViewController.navigationController and then [navigationController pushViewController:newView animated:YES]; will work perfectly.
Initially I used the IB to drag a navcontroller onto my tabbar and then setting the root controller. However, I'm trying to create a nav controller without IB.
What I've done is created a navcontroller subclass that will be used in my tabbar. The nav bar does show up so I know that is working.
Now, I need to push a viewcontroller into the hierarchy. In the nav controller's viewDidLoad:
- (void)viewDidLoad {
InfoViewController *initialController = [[InfoViewController alloc] init];
[self.navigationController pushViewController:initialController animated:YES];
[initialController release];
[super viewDidLoad];
}
I get no errors, but nothing shows up in the navigation controller. Anyone know why?
What does your InfoViewController's init method look like? If you're using a nib to build that, then you need to call initWithNib, not init.
Create the viewcontroller when/where you create the navigation controller (and add them to it) then add the list of view controllers to the tabbar.
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.