How to jump to tabbarcontroller from uiviewcontroller? - uitabbarcontroller

I have a ViewController as initial view controller and TabbarController. There are 3 buttons on view controller and TabbarController has 3 Tabs.
I would like tapping the first button jump to first tab, second button - to the second Tab etc.
How should I do that? I'm working with storyboard

Please Clarify your question if you just want to move your tab with click on button the use selectedatindex method for tab bar.

try this,
step 1 ->import appDelegate class in your ViewController.m
#import "AppDelegate.h"
- (IBAction) BtnOneClicked: (id) sender
{
AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
[appDelegate.tabBarController setSelectedIndex:0];
}
- (IBAction) BtnTwoClicked: (id) sender
{
AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
[appDelegate.tabBarController setSelectedIndex:1];
}
- (IBAction) BtnThreeClicked: (id) sender
{
AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
[appDelegate.tabBarController setSelectedIndex:2];
}

A normal View Controller Button Click Event We can jump On Tab bar Controller as simple as Normal just Crete a cocoatouch class which will we sub class of UitabBarController and present to the View.
#IBAction func goActivityView(_ sender: Any) {
let gotoActivityView = storyboard?.instantiateViewController(withIdentifier: "TabBarViewController") as! TabBarViewController
self.present(gotoActivityView, animated: false, completion: nil)
}

Related

Can't push from programmatically created UINavigationController

In a Swift 3 project using Xcode 8.3 and iOS 10.3, I get a navigation controller to push. The app runs with a navigation controller until I try to use it to push. All works until the last line which cause an app crash, fatal error: unexpectedly found nil while unwrapping an Optional value. I don't want to use the storyboard, the point of this question is how to accomplish this task without the storyboard.
App Delegate code
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
window = UIWindow(frame: UIScreen.main.bounds)
window?.makeKeyAndVisible()
window?.rootViewController = UINavigationController(rootViewController: MainViewController())
}
View Controller:
import UIKit
class MainViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white
let navController = navigationController
let myProfileViewController = MyProfileViewController()
navController!.pushViewController(myProfileViewController, animated: true)
}
}
Move
let navController = navigationController
let myProfileViewController = MyProfileViewController()
navController!.pushViewController(myProfileViewController, animated: true)
from viewDidLoad to viewDidAppear.
In viewDidLoad there is no parent navigation controller set up and you should not start animations from there.
You can add navigation controller in storyboard,
in Storyboard click on MainViewController & from editor menu add navigation controller & push via navigation controller as below code.
let vc = self.storyboard?.instantiateViewController(withIdentifier: "MyProfileViewController") as! MyProfileViewController
self.navigationController?.pushViewController(vc, animated: true)
Make sure storyboard identifier is correct. & remove navigation controller code form AppDelegate class.
first give name to your navigation controller like this
then in your code initialise it like this
let nav = (UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "yourNavigation") as! UINavigationController);
nav.pushFrontViewController(MainViewController(), animated: true);
window?.rootViewController = nav

Swift 3 trying to create and use a UINavigationController programmatically

I'm working on an existing application which uses the storyboard but I want to continue development without using the storyboard at all, or nib files.
The existing left menu is a UITableView which is not the root controller and does not have a navigation controller associated with it. I have created a UINavigationController in the app delegate and want to use this navigation controller to push a new controller. I am able to present a view controller but I want to use push in order to conform with the current UIX.
In the following code nothing happens because navigationController? returns nil.
Here is my new code in the app delegate.
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var navController: UINavigationController?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
navController = UINavigationController()
self.window = UIWindow(frame: UIScreen.main.bounds)
self.window!.rootViewController = navController
self.window!.backgroundColor = .red
self.window!.makeKeyAndVisible()
let menuViewController = SideMenuViewController()
menuViewController.navController = navController
Then in my side menu controller:
import UIKit
class SideMenuViewController: UIViewController {
var navController: UINavigationController?
.......
let myViewController = myViewController()
self.navigationController?.pushViewController(myViewController, animated: true )
From the UIViewController documentation:
var navigation​Controller:​ UINavigation​Controller?
The nearest
ancestor in the view controller hierarchy that is a navigation
controller.
Which means when you call self.navigationController in a view controller you get the (nearest) navigation controller that self is embedded in. Apparently your menu controller is not embedded in the navigation controller hierarchy. Using your app delegate's navController member is one way to make this work. Then you can say in your menu view controller:
func menuItemFooSelected()
{
let navController = (UIApplication.shared.delegate as! AppDelegate).navController
let myViewController = myViewController()
navController?.pushViewController(myViewController, animated: true)
}
Alternatively (and probably preferably), when you set up your initial structure in your app delegate, inject the navigation controller into the side menu controller:
class MenuViewController: UIViewController
{
var navController: UINavigationController?
...
func menuItemFooSelected()
{
let myViewController = myViewController()
self.navController?.pushViewController(myViewController, animated: true)
}
}
In application(_:didFinishLaunchingWithOptions:)
...
let menuViewController = MenuViewController(...)
menuViewController.navController = navController

Automatic presenting View Controller

I have 2 view controllers on my storyboard: viewController1(it input point) and viewController2. After loading the application i want to automatic presenting viewController2 after viewController1.
How i can do that?
if you want to go to second viewcontroller you can add this code to you viewDidLoad method:
let secondViewController = self.storyboard?.instantiateViewController(withIdentifier: "yourIdentifier") as! secondViewController
self.navigationController?.pushViewController(secondViewController, animated: true)
remeber to add a identifier for the second viewcontroller in storyboard and chenge the identifier that you use with "yourIdentifier"
if you don't want a animation put the animated: true to false.
if you dont want to show the fist viewcontroller, go into you storyboard and click on the second viewcontroller, then in the right side on attributes inspector select the box int the image:
For change viewcontroller you have to embed the first viewcontroller with a navigation controller, if you don't know how to do it, just select the fisrt viewcontroller and do like the image, click in navigation controller
Have you tried to show it in func viewDidLoad() {} like:
override func viewDidLoad() {
// ...
self.present(viewController, animated: true, completion: nil)
}
If you want to straight up load a different view controller you can do this in your app delegate:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "put your identifier here")
self.window?.rootViewController = vc
return true
}

How can I trigger one UIViewController of the UITabBarController from another UIViewController?

I have a project that is a "tab-bar controller" app. The first button is essentially a Home screen. The second one displays a UITableView of content. The third button displays a different UITableView, etc.
From the first view (Home), I have a button on the page that is functionally equivalent to the second-button of the tab controller. It is an alternative path to get to that UITableView. I do not want to send a button press to the AppDelegate's UITabBarController.
The code in HomeViewController that I want is essentially this:
-(IBAction) touchInsideButton:(id)sender {
[self presentModalViewController: [appDelegate secondViewController] animated:YES];
}
or
-(IBAction) touchInsideButton:(id)sender {
AppDelegate *appDelegate = (AppDelegate*) [[UIApplication sharedApplication] delegate];
[appDelegate launchSecondViewController: self];
}
where, "launchSecondViewController" is (uncomment-out one of the two lines)
-(void) launchSecondViewController: (id) sender {
// [self.tabBarController presentModalViewController: secondViewController animated:YES];
// [self.tabBarController.navigationController pushViewController: secondViewController animated:YES];
}
Regardless, all three approaches give the same error:
2013-05-16 12:04:26.977 MyApp[55273:f803] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Application tried to present modally an active controller .'
* First throw call stack:
The problem is that this instance of the second view controller, which you refer to as secondViewController, is already in the interface, as a child of the tab view controller. You cannot have it be there and also present or push it.
The way to manipulate a tab bar controller in code is to set is selectedViewController (or selected index). Try doing that instead.

iPhone ModalViewController from First Tab Bar View

I am trying to launch an initial login/registration screen before my TabBarController View loads. I have read that putting a ModalViewController in the First View is a good way to go. This works, but I am trying to add navigation controls to the ModalViewController. I am getting the following issues:
1 - ERROR: Property 'navigationController' not found on object of type 'AppDelegate'
2 - WARNING: Initializing 'AppDelegate *'with an expression of incompatible type 'id'
here is the code on my ModalViewController:
-(IBAction)signUpButtonTapped {
// i need to get the control for main navigation controller
AppDelegate *appDelegate = [[UIApplication sharedApplication]delegate];
[appDelegate.navigationController popToRootViewControllerAnimated:NO];
// create object from app main view to push it
SignUpViewController *signUpViewController = [[SignUpViewController alloc] initWithNibName:#"SignUpViewController" bundle:nil];
[AppDelegate.navigationController pushViewController:signUpViewController animated:YES]; }
Anyone have any ideas? Thanks so much!
There are Two Problems in your code
1) Accessing the OBJECT with class name. It must be appDelegate.nav... (small a for Solving 1 ERROR)
[appDelegate.navigationController pushViewController:signUpViewController animated:YES];
2) Type Casting the assignment (for Solving 2 Warning)
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication]delegate];
So your Complete working Code must go as
-(IBAction)signUpButtonTapped {
// i need to get the control for main navigation controller
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication]delegate];
[appDelegate.navigationController popToRootViewControllerAnimated:NO];
// create object from app main view to push it
SignUpViewController *signUpViewController = [[SignUpViewController alloc] initWithNibName:#"SignUpViewController" bundle:nil];
[appDelegate.navigationController pushViewController:signUpViewController animated:YES];
}
AppDelegate instance declaration is like this:
AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication]delegate];
Change this and u r good to go.
In your Code Modify this Line
AppDelegate *appDelegate = [[UIApplication sharedApplication]delegate];
as
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
and try now
1.make sure your appDelegate have a UINavigationController property called navigationController.
2.Line 2 of your code:
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication]delegate];
3.last line of your code:
[appDelegate.navigat.... NOT:[AppDelegate.navigatio....