Objective-C Present View Controller with its Navigation Controller - ios

I have a view controller like in the image below:
And I am trying to present this view controller from another view controller like so:
LHPDFFile *vc = [[LHPDFFile alloc] init];
vc.previewItemURL = self->_previewItemURL;
UINavigationController *navBar=[[UINavigationController alloc]initWithRootViewController:vc];
[self presentViewController:navBar animated:YES completion:nil];
This works, however my buttons are not appearing :(
It appears that the code above is creating a Navigation Controller instead of using mine with the buttons. What am I doing wrong?

Try instantiating your view controller with the storyboard. Something like:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"YourStoryboardName" bundle:nil];
LHPDFFile *vc = (LHPDFFile *)[storyboard instantiateViewControllerWithIdentifier:#"<id of your view controller in the storyboard>"];
Calling the empty init method leads to empty instantiation of the view, because you have never mentioned that it should use this storyboard's this view controller. More details here.

You init none of your controllers from storyboard! Those buttons belongs to file view controller. You should init that controller from storyboard instead of call init.
MyViewController *vc = [[self storyboard] instantiateViewControllerWithIdentifier: #"MyViewControllerStoryBoardID"];
UINavigationController *navBar = [[UINavigationController alloc]initWithRootViewController:vc];
[self presentViewController:navBar animated:YES completion:nil];

Related

Pass variable to View Controller Embedded in Navigation Controller in Objective-C

I have embedded a destination viewcontroller in a navigation controller and am now unable to pass it a variable.
My code is:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
//THis is the navigation controller
UIViewController *destVC = [storyboard instantiateViewControllerWithIdentifier:#"myNav"];
//This is the view controller embedded in the nav
IDNowVC* myVC = [storyboard instantiateViewControllerWithIdentifier:#"myVC"];
myVC.sender = #1;//for contacts
[self presentViewController:destVC animated:YES completion:nil];
After the launch of the VC, sender property is nil. It is not getting the value #1.
What am I missing?
Thanks for any suggestions.
In your code you have a comment that says:
//This is the view controller embedded in the nav
However, the view controller below that comment is not the one embedded in your navigation controller. It's a completely new controller that is created at that line and disposed of at the end of the function.
You need something more like this:
UINavigationController *destVC = (UINavigationController *)[storyboard instantiateViewControllerWithIdentifier:#"myNav"];
IDNowVC* myVC = destVC.childViewControllers[0];
myVC.sender = #1;
There might be some syntax issues with the above...

Call another class in IOS

I'm new on IOS platform and after study a little about how does it works, i had one doubt about how to call a new class/view and overlay the current view when a button is pressed. In android i do:
Intent intent = new Intent(a.class, b.class);
startActivity(intent);
Searching on internet, i noticed that i have to use navigation bars to do it. I start an app with tab bar controller and putted a navigation controller. I used the code below:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Storyboard" bundle:nil];
UIViewController *myController = [storyboard instantiateViewControllerWithIdentifier:#"Dicas"];
[self.navigationController pushViewController: myController animated:YES];
and the return:
There is a way to overlay the current view? I always have to use navigation bars to call another class (use bottom e upper controllers will make my app ugly)?
To hide the navigation bar:
[[self navigationController] setNavigationBarHidden:YES animated:YES];
To hide tab bar:
yourViewController.hidesBottomBarWhenPushed = YES;
You can also call another viewController by using presentViewController function of a viewController class.
[self presentViewController: myController animated:YES completion:nil];
As you said u want to overlay your current view so not sure but You can show your view controller using model view like this
yourViewController *secView = [[YourViewController alloc] initWithNibName:#"YourViewController" bundle:nil];
[secView setModalPresentationStyle:UIModalPresentationPageSheet];//u can use different UIModalPresentationStyle
[secView setModalTransitionStyle:UIModalTransitionStyleCoverVertical];
[self.navigationController presentViewController:secView animated:YES completion:nil];
Above one will show your view controller over existing view controller, once presented you need to make provision to dismiss this modal view.
Simple way to present new class or ViewController like this..
ViewController *view = [self.storyboard instantiateViewControllerWithIdentifier:#"ViewController"];
[self presentViewController:view animated:YES completion:nil];

Present a navigation view from view controller - IOS 7

I got an error when I need to present a navigation controller from view controller like below:
"Warning: Attempt to present on whose view is not in the window hierarchy!"
The code as below:
UIStoryboard* storyboard = [UIStoryboard storyboardWithName:#"NavMain" bundle:nil];
UINavigationController * mainViewController = [storyboard instantiateInitialViewController];
mainViewController.modalPresentationStyle = UIModalPresentationFormSheet;
[self presentViewController:mainViewController animated:YES completion:NO];
How to fix the bugs or does I had any error when implement storyboard?
I use two storyboard from main.storyboard to NavMain.storyboard
The error means that self's view hasn't loaded yet. You want to call this code after the view has loaded. So put it in:
-(void)viewDidAppear:(BOOL)animated;

Pushing a navigation controller is not supported

In my MainStoryBoard I want to push a viewController to the detailView but I get this error:
NSInvalidArgumentException', reason: 'Pushing a navigation controller is not supported'
I set the identifier 'JSA' ID for the viewController on the storyboard.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == 0) {
SWSJSAViewController *viewController = [[UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil] instantiateViewControllerWithIdentifier:#"JSA"];
[self.navigationController pushViewController:viewController animated:YES];
}
}
Like rmaddy said in the comments you are trying to push a navigation controller.
Navigation controllers should be presented (via presentViewController or they can be added as a childViewController) and ViewControllers should be pushed.
When you talk about pushing Navigation Controller, it is most likely that you want to present it.
Presenting UINavigationController
This is the most common way and this is what you want to do in most cases. UINavigationController cannot be pushed, it can only be presented with a new root View Controller.
MyViewController* vc = [[MyViewController alloc]
initWithNibName:#"MyController" bundle:nil];
UINavigationController *myNav = [[UINavigationController alloc] initWithRootViewController: vc];
[self presentViewController:myNav animated:YES completion:nil];
What you do here, is firstly create a UINavigationController and then set necessary UIViewController as its root controller.
Pushing UINavigationController
If you have a hierarchy of ViewControllers and you need to push view controller that contains navigation controller within, steps are:
1) Push ViewController, containing UINavigationController.
To push UINavigationController, first create a subclass of UIViewController, which will be a wrapper-/container- class for your UINavigationController and its content.
ContainerViewController* vc = [[ContainerViewController alloc] init];
2) Adding UINavigationController as a child view controller
In viewDidLoad of your container (that you have just instantiated) simply add something like this:
Objective-C
UINavigationController* myNav = [[UINavigationController alloc] initWithRootViewController: rootViewController];
[myNav willMoveToParentViewController:self];
myNav.view.frame = self.view.frame; //Set a frame or constraints
[self.view addSubview:myNav.view];
[self addChildViewController: myNav];
[myNav didMoveToParentViewController:self];
Swift 4.2+
let childNavigation = UINavigationController(rootViewController: viewController)
childNavigation.willMove(toParent: self)
addChild(childNavigation)
childNavigation.view.frame = view.frame
view.addSubview(childNavigation.view)
childNavigation.didMove(toParent: self)
What you do here is basically instantiate your navigation controller and add it as a child controller to your wrapper. That's it. You have successfully pushed your UINavigationController.

how to transit to a view controller

I made a view controller A in storyboard and linked with other view controller. Now I have another view controller B and I want to write a method that can transit from B to A. How can I do this?
There is a segue to controller A in storyboard.
And controller B is loaded from another view I created programmatically so I can't set a segue in storyboard.
Since you are using storyboard. This is more accurate, try this:
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
UIViewController *vc = [sb instantiateViewControllerWithIdentifier:#"YOUR VIEW CONTROLLER"];
vc.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentViewController:vc animated:YES completion:NULL];
But be sure to name your view's identifier otherwise it will crash and wont work.
Then when you want to dimiss the view:
[self dismissModalViewControllerAnimated:YES];

Resources