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.
Related
I've added a container view inside another ViewController. From this, I dont really know how to do to swith between ViewController inside the ContainerView.
The container view:
The default ViewController inside the ContainerView (with storyboard id "firstView"):
I've a last ViewController (with storyboard id "secondView")
From this, how can I do to switch between firstView and secondView inside my Container View ? How can I get an outlet for the ContainerView, firstView and secondView ? If this is not the right way to dos this, can someone help me out to achieve what I want ?
(I already checked the apple's documentation and didn't find what I need :( )
Thanks :)
Just add the controller's view to the container view.
[controller.containerView addSubview: othercontroller.view]
In my application i want to add a viewcontroller with nib on top of tabbarviewcontroller using storyboard.
for eg; when the application launch for first time i want to show that view controller for once and after that when ever user start the application it should show the tabbarviewcontroller. and not the viewcontroller.
following is my code
-(void)viewDidAppear:(BOOL)animated
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
UIViewController *vc = [storyboard instantiateInitialViewController];
[vc setModalPresentationStyle:UIModalPresentationFullScreen];
[self presentModalViewController:vc animated:YES];
}
I'm a little confused with how you described what you want. There are a couple of ways to do what you want and depending on how you want things to flow.
Storyboard
If you stay in the storyboard, you can add a UIViewController - in front of your tabbar (to the left of) controller. Basically, add a UIViewController and move the start arrow to it. then create a segue from it to your tabbar controller. You can bring in the tabbar controller via a push segue or even as a modal segue if you want.
You would have to move your xib file into the storyboard.
It would flow like this: UIViewController -> UITabbarController -> Rest of your app.
In this model, the first view controller would always be available on launch.
Another strategy - trying to keep things simple is to use the first view controller attached to the tabbar. It would align with the left most tab.
That view controller gets instantiated and put on screen by the tabbar controller first under normal conditions. You can add code in that UIViewController in the ViewDidLoad or ViewDidAppear methods to instantiate and put up the modal view using either a storyboard or a nib file.
Finally, the last way I can think of would be to load the nib file from your app delegate then display your tabbar from the storybook as a modal. I think this approach is the least desirable, but doable.
hope that helps. good luck.
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 am strugling on the following task.
I am trying to control my subview from another viewController class.
What I did and does not work is this.
I inserted an object and changed it class to my second viewController class.
Then I connected its UIButton outlet to a button I have on my subview.
I then connected the buttons action to the outlet of my second view controller.
What I get when I run is this.
It all shows up well but when I try to touch the button that resides in my subview app crashes. I am only left with a worringing: "Action unavailable: The "Touch Up Inside" event of "Rounded Rect Button".
It's probably my logic that is incorrect. Thanks for help.
Well after a long research I got an answer to my problem.
As it appears I was doing everything right.
The problem is that after the initial XIB file is initialized it autoreleases all views and subviews. So to prevent from gettig your second view controller from being released implement this method
- (void)awakeFromNib {
[self retain];
}
in your view controller .m file.
This method will retain your second view controller alive and allow it to recive and respond to UI actions.
My app have
AppDelegate ----> added MyViewController.view
MyViewController contains UINavigationController,
i.e. MyViewController.view = MyViewController.aNavigationController.view
With this I able to see the added view correctly
Then I created a NextViewController
Added one button to MyViewController with action
On button click , i wrote code for navigating to next view using pushViewController
But it's not working, After clicking button the same view is getting displayed
Please help, if anybody gone through same issue and resolved it.
Is it possible to add navigation controller to a view controller?
EDIT:
#raaz thanks for reply ...
I tried by the way u specified here i.e.
Added a UINavigationController to MyViewController using IB
Created outlet for UINavigation controller in MyViewcontroller.h
Did required connection for IBOutlet, also created IBAction for button
Then On button click , created object of NextViewController and push the next view controller to navigation controller
But still , App window shows the current view and do not displays the next view
My query is that , can we add a navigation controller to this "MyViewController" i.e. UIViewController ???
As you are not providing what you are implementing in you code so it difficult to say what is you problem
Solution:
//First import the NextViewContoller.h in your MyViewController.m
//Then implement this method in your application
-(IBAction)goToNext:(id)sender{
NextViewContoller *nxt=[[NextViewContoller alloc]init];
[self.navigationController pushViewController:nxt animated:YES];
}
//Finally release nxt in dealloc