Dismissing a modal view controller via code - ios

iPhone project/iOS 7.1/objective-C Due to circumstances, i have presented a navigation controller which contains a view controller which in turn contains a web view.All of the before said are added programmatically (i.e. no .h,.m or xib files).How do i dismiss the navigation controller?
P.S: I've created all these in a method.There is no property or instance available for the navigationcontroller.

you have to create on barButtonItem on navigation bar like "Back" or "Done" and on it's click event you should write :
[self dismissViewControllerAnimated:YES completion:nil];

Related

iOS - navigation controller back button

I'm developing an iPad App using storyboards. In this app when the user click on "preferences" appear a modal View. One button of this view send the user to another View Controller (but this view has an action bar to go back to his root view controller), but when user taps the action bar back button nothing happen (it's called navigationController popViewControllerAnimated), the user continue in the same view.
Can anyone help me??
Thanks.
UPDATE:
The code to handle the back button:
- (IBAction)btnBackTapped:(id)sender {
[self.navigationController popViewControllerAnimated:YES];
}
I'm using Segue (from storyboard) to call this View Controller:
When the user click on "Meus Favoritos"
They will be redirect to this page:
The segue is with a Modal (from image one to two)...
When you are presenting a View Controller modally, it is likely not within a Navigation Controller, so probably the reference to navigationController in your code is nil, can you check that?
If you are presenting your View Controller modally this will work instead
[self.presentingViewController dismissViewControllerAnimated:YES completion:nil];
However, if you actually want to use a Navigation Controller, you should embed the View Controller that is presenting the Preferences View Controller in a Navigation Controller and present the Preferences View Controller with a show segue instead of a modal one.

Tabs do not show when using PushViewController

I'm trying to use the following code to display a tabbarcontroller
UITabBarController *tc = [[self storyboard] instantiateViewControllerWithIdentifier:#"tabbarcontroller"];
[self.navigationController pushViewController:tc animated:YES];
It does load the view, and I can tell it which of the tabs I want it to default to. The problem is the tabs don't show. From what I've read I gather it has something to do with putting the tab controller inside of the navigation controller, but I couldn't find any solutions.
If you use Storyboard, use pushViewController method is a bad choice (also if it work). You have to insert a "segue".
Go in the storyboard and while press ctrl button, click on the main controller (which must open the tabViewController) and then release the click on the tabBarController.
Now you have the segue. Click on the circle which appears and choose an identifier for this segue, for example: MainToTab .
Now in your method, you have just to call:
[self performSegueWithIdentifier:#"MainToTab" sender:self];
Moreover, if you want manage the property on the destination controller (by segue), you can implement this method:
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"MainToTab"]) {
UITabViewController *tb = (UITabViewController *)segue.destinationViewController;
//set the properties...
}
}
This method is called automatically when you launch the previous method.
What you want inside your UITabBarController are UIViewControllers, possibly inside a UINavigationController.
You shouldn't as well push a UITabBarController. You can see Apple's explanation here:
An app that uses a tab bar controller can also use navigation
controllers in one or more tabs. When combining these two types of
view controller in the same user interface, the tab bar controller
always acts as the wrapper for the navigation controllers.
The most common way to use a tab bar controller is to embed its view
in your app’s main window. (...)
Still you can present it modally:
It is possible (although uncommon) to present a tab bar controller
modally in your app. Tab bar interfaces are normally installed in your
app’s main window and updated only as needed. However, you could
present a tab bar controller modally if the design of your interface
seems to warrant it. For example, to toggle from your app’s primary
operational mode to a completely different mode that uses a tab bar
interface, you could present the secondary tab bar controller modally
using a crossfade transition.

Dismiss Modal NavigationController from pushed ViewController inside

I have an iPad app and I'm using the storyboard. I'm presenting a modal view, which I wrap in a UINavigationController. It's a modal "Settings" view with it's own navigation.
I have a delegate setup to dismiss the modal view itself with a Done button in the Settings view which works fine, but I need to be able to dismiss it even after it has pushed another view when the user taps a setting.
So, basically a "cancel" button on the right side of the Navigation bar in the pushed views.
Instead of having to go back to the first "Settings" view to hit the done button.
I've tried setting up a second delegate for the pushed view without success:
Just add a bar button item and put this line in its action method:
[self.presentingViewController dismissViewControllerAnimated:YES completion:nil];
The presenting view controller will be the controller that presented the modal, not the navigation controller.

Segue to TabBar Controller

I'm using the following code to perform a segue to another view controller:
[self performSegueWithIdentifier:#"BackSegue" sender:self];
This works fine when the destination of "BackSegue" named segue is another view controller (one of the tabs, actually) but I need to display the tabs at the bottom so transitioning directly to this view controller won't work as there will be no tabs. Is it allowed/possible to segue to a tabbar controller? Is anything wrong with this specific code or would it be something else I'm doing?
Edit 1
The TabBar controller has no .m/.h files and is never declared programmatically, but I'm pretty sure the segue is set up correctly in the storyboard to the best of my knowledge (the same way it was set up earlier directly to the other viewcontroller).
You can segue directly to a UITabBarController. Just change the segue in your storyboard. When the segue occurs, it should load the tab bar controller, and consequently the tab bar at the bottom of the screen and the first view controller's view associated with the tab bar controller.
In order to segue to the specific tab in the tab bar controller:
You need to add the selectedIndex=1
Add these lines of code for segue:
UITabBarController *loadTabBar = [self.storyboard instantiateViewControllerWithIdentifier:#"TasksAppsTabs"];
loadTabBar.selectedIndex=1;
[self presentViewController:loadTabBar animated:YES completion:nil];

pushViewController without navigationcontroller

I have a class that is of type UITableViewController.
This class has a member of type UINavigationBar that I use for adding in an edit button for the table view.
Using this method calling is invalid.
[self.navigationController pushViewController:controller];
How can I push a detail view onto the view after selecting a table row without wrapping my UITableViewController in a UINavigationController?
The closest alternative if you don't want to use navigation controller are modal view controllers.
[self presentViewController:controller animated:YES completion:nil];
This will slide the controller into your screen from bottom, or if you change controller's modalTransitionStyle it can flip over or fade in.
To dismiss the controller just call:
[self dismissViewControllerAnimated:YES completion:nil];
I would wrap the UITableView inside a UINavigationController and just hide the UINavigationBar.
[self.navigationController setNavigationBarHidden:YES animated:NO];
And then create a back button that pops the ViewController off the stack.
[self.navigationController popViewControllerAnimated:YES]
What could also be done is to use a Navigation Controller as usual and then hide it.
To hide the Navigation Controller using storyboards: select it and uncheck "Show Navigation Bar" in the attribute inspector. Others might suggest to hide the navigation bar in each controller, but the problem with that is that it will appear for a millisecond and then disappear.
You can't push a view controller onto a navigation controller if there is no navigation controller. If you are wanting to be pushing controllers and have it display the topmost controller and everything, just use a UINavigationController and be done with it.
You can push arbitrary UINavigationItems onto your UINavigationBar, and your bar's delegate will be notified when the user uses the back button so you can take appropriate action. See the documentation for more information.
It's true that without a UINavigationController you can not push view controllers. You rather present view controllers modally via UIViewController.present(_ viewControllerToPresent:, animated:, completion:)
But it's possible to create a custom segue to display the view controller as if it were a push (or any other animation you want), although it seems that using a UINavigationController just makes things easier.
Here are some related links to the documentation:
UINavigationController Class Reference
Customizing the Transition Animations
Presenting a Modal View Controller

Resources