Excuse the complete monotouch noob.
I have a main Tab controller that is the basis for the app, when I navigate to one of the tabs I need is uinavigationcontroller with a uitableview inside it.
The main tabview I have handled.
the secondary view for the chosen tab, I went into IB and dragged a navigationcontroller over,
I then dragged a uitableview ontop of that. So now I have the XIB set.
How do I declare this stuff in mono to hook it up ? The main controller is obvious, the class is right there, derive from UINvaigationController and it will load it from the XIB. Where do I declare the UITableView that is the sub view ? How do I hook it up to the XIB as its in the same XIB as the main navigation controller ?
Thanks for any pointers you can give, and apologies if this is a repeat question.
I recommend to you to do so:
Make XIB-file with UITableView, but with no UINavigationController. If you want to see how a view could looks like, set view's "Top Bar" property values from "None" to "Navigation Bar". It's just for previews.
Add instance of UINavigationController to your AppDelegate;
Call UINavigationController.PushViewController with UITableView;
Add UINavigationController as view controller to one of your tab;
Example:
Imagine that UI of friendsView contains UITableView.
public partial class AppDelegate : UIApplicationDelegate
{
// 1, 2
Friends friendsView;
UINavigationController friendsNav;
...
// 3
public override bool FinishedLaunching (UIApplication app, NSDictionary options)
{
friendsView = new Friends ();
friendsNav = new UINavigationController();
friendsNav.PushViewController(friendsView, false);
...
// 4
MyTabController.ViewControllers = new UIViewController[]{
journalNav, friendsNav
};
}
}
Thus, you will have 2 tabs journalNav, friendsNav. When you switched to friendsNav, friendsView with UITableView and navigation bar.
Related
Just imagine( as case) that i have TabBarController as root and several ViewControllers that are linked to this TabBarController( via Storyboard(!) that is important step,i mean programmatically it's something other).
And its works without any code behind on TabBarController class!
So i'm interested in right approach, how can i Inistiate Viewcontrollers inside TabBarController on right way!?
Or it's isn't necessary and OS will do it for me , because at current moment, i can do something like this :
public override void ViewDidLoad()
{ //No Init for VC that are inside linked to TabBAr
base.ViewDidLoad();
//Find all viewcontrollers that are linked in storyboard
foreach (var Item in ViewControllers)
{
Item.TabBarItem.Image =
UIImage.FromBundle("RandomImg");
//Change title for all VC to "Main"
Item.TabBarItem.Title = "Main";
break;
}
}
What if i will do initialization of ViewControllers inside TabBarController constructor.
What will be happened on behind the scenes? How the system will recognize my init of VC,or it's doesn't matter and thats will be an redundant action, because technique via storyboard anyway creates ViewControllers without any initialization on code behind!?
How to handle/customize correctly ViewControllers that are inside of TabBarController?(e.g. select another VC at beginning, change buttons on TabBar and etc)
I would like to take all answers on all my questions and if it possible, need an live example/article that explains more advanced about this stuff. Thanks!
I am currently implementing the XLPagerTabStrip (https://github.com/xmartlabs/XLPagerTabStrip) which effectively creates a tab bar at the top of the view controller. I want to be able to segue to a new view controller from one of the tabbed controllers and be able to use the navigation bar to move backwards (or a custom version of the navigation bar if this isn't possible).
XLPagerTabStrip provides the moveToViewController and moveToViewControllerAtIndex functions to navigate between child view controllers, but this method doesn't allow use of a navigation bar to go backwards.
Conceptually XLPagerTabStrip is a collection of view controllers declared and initialized during the XLPagerTabStrip model creation.
It has virtually no sense to use a UINavigationController if you already have all the viewcontrollers available.
You can create a global var previousIndex to store the previous viewController index and allow users to go back by using canonical methods:
func moveToViewControllerAtIndex(index: Int)
func moveToViewControllerAtIndex(index: Int, animated: Bool)
func moveToViewController(viewController: UIViewController)
func moveToViewController(viewController: UIViewController, animated: Bool)
About a new viewController, suppose you have 4 viewControllers that built your container (XLPagerTabStrip) named for example z1, z2, z3 e z4.
You can embed to z4 a UINavigationController (so it have the z4 controller as rootViewController) and start to push or pop your external views. When you want to return to your z4 you can do popToRootViewControllerAnimated to your UINavigationController
When you are go back to z4 , here you can handle your global var previousIndex to moving inside XLPagerTabStrip.
I'm not familiar with XLPagerTabStrip, but I had a similar problem recently and the solution was to use an unwind segue to go back to the previous view controller. It's pretty trivial to implement so probably worth a try.
To navigate back to your previous view tab controller, you had initially navigated from;
Embed your new view controller, from which you wish to navigate
away from in a navigation bar
Connect it's Navigation Bar Button to the Parent view containing the
tab bar by dragging a segue between the 2 views
Create a global variable in App delegate to store current index
which you will use in the Parent view to determine what tab view
controller to be shown
var previousIndex: Int = 0 //0 being a random tab index I have chosen
In your new view controller's (the one you wish to segue from)
viewdidload function, create an instance of your global variable as
shown below and assign a value to represent a representative index
of the child tab bar view controller which houses it.
//Global variable instance to set tab index on segue
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.previousIndex = 2
You can write this for as many child-tab connected views as you wish, remembering to set the appropriate child-tab index you wish to segue back to
Now, create a class property to reference your global variable and a function in your Parent view as shown below
let appDelegatefetch = UIApplication.shared.delegate as! AppDelegate
The function
func moveToViewControllerAtIndex(){
if (appDelegatefetch.previousIndex == 1){
self.moveToViewControllerAtIndex((self.appDelegatefetch.previousIndex), animated: false)
} else if (appDelegatefetch.previousIndex == 2){
self.moveToViewControllerAtIndex((self.appDelegatefetch.previousIndex), animated: false)
}
}
You may now call this function in the Parent View Controller's viewDidLoad, as shown below.
moveToViewControllerAtIndex()
Run your project and that's it.
I want to pass data from one tabBar controller to another,
I am switching tabBar using tabBarController?.selectedIndex = 0
I tried many option but unable to pass data to another tab, is there any simple solution like we pass data using navigation controller?
Tabs are usually some custom UIViewControllers. From these view controllers (tabs) you can also get access to the UITabBarController with something like:
if let mainController = UIApplication.sharedApplication().delegate?.window??.rootViewController as? YourMainTabBarControllerClass {
mainController.someVariable = 123
}
Here you have to be careful, because the tab bar controller may not be the rootViewController, see this question for more details.
In order to store some properties in the UITabBarController you have to implement your own class by extending UITabBarController and then set the custom class in the StoryBoard. The class will then look like:
class YourMainTabBarControllerClass: UITabBarController {
// some custom variables here...
var someVariable = 0
...
}
when I'm moving from one story board to another storyboard via click on signIn button, this.NavigationController is showing null. so I'm not able to PushViewController.
I've one stoaryboard with two views and that two views have separate UIViewControllers.
You must explicitly create a UINavigationController and place your initial view controller inside of it. Doing this will automatically set the NavigationController property of any view controller contained within that Navigation controller.
// create the view controller for your initial view - using storyboard, code, etc
var first = new UIViewController(...);
// wrap your VC inside a Nav controller
var nav = new UINavigationController(first);
Just to add to Jason's answer. By default the RootViewcontroller is the initial view controller.
What I did was to override the that initial view controller with the a navigation view controller:
[Register("AppDelegate")]
public class AppDelegate : UIApplicationDelegate
{
public override UIWindow Window
{
get;
set;
}
public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions)
{
Window.RootViewController = new UINavigationController(Window.RootViewController);
return true;
}
This is the line I added:
Window.RootViewController = new UINavigationController(Window.RootViewController);
Window.RootViewController: is the ViewController, e.g MainViewController
and I'm overriding this initial controller, that's set by default, with the navigation controller.
This worked perfectly for me.
I have 1 tab bar controller in storyboard and 1 UIViewController associated with it. I would like to re-use the same UIViewController in order to create second item in tab bar. When I am creating second relation from tab bar to view controller I need to specify 2 different items names. How can I re-use same view controller and set different items names from storyboard? If not possible to do it in storyboard, then do I have to rename each in tab bar controller class or there is better way?
I was going to provide different data to view controller in prepareforsegue.
UPDATE:
little more details and clarification
In above screenshot marked VC at the moment is reachable a) directly from tab, b) through 3 transitions. I want to add another DIRECT relation to initial tab bar, just like in case of "a".
I can give you a little tweak for that and at least that worked for me.
Drag a tabbarcontroller and associated tab item view controllers to
your storyboard. Name them as you like.
Create an extra view controller that you want to reuse from your storyboard.
Add container views to each tab item view controllers and remove their default embedded view controllers.
Create embed segue from each tab item controller to your re-usuable view controller.
The configuration looks something like the following:
Thus you can use the same embedded VC for different tabbar item. Obviously if you need the reference of the tabbarcontroller, you need to use self.parentViewController.tabBarController instead of self.tabBarController directly. But it solves the issue of reusing a VC right from the storyboard.
I've found much simpler solution using storyboard only.
Setup your storyboard like this:
Then in your Navigation Controller Identity Inspector set Restoration ID like this:
And in your ViewController class file put the following code:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.navigationItem.title = parent?.restorationIdentifier
label.text = parent?.restorationIdentifier
}
or do what you like based on parent?.restorationIdentifier value
If you don't want the Navigation TopBar to appear on the ViewController just set it to None in Attributes Inspector of the desired Navigation Controller like this:
That's it! Hope it helps.
Yes you can.
All you need to do is to create a new View Controller in StoryBoard as if there is going to be a different View Controller for tab 2. Then Select the 2nd view controller and simply add its class name the same classname of view controller 1
Things to note:
When you are sharing the same view controller class (.m ad .h) files, each tab will create a NEW instance of that class.
Edit:
This works as long as you have either a "custom" cell scenario (i.e. reusing two table view controllers) OR, have all your views inside a "container view" (i.e. reusing UIView).
I needed slightly different solution than the accepted answer. I needed to use same Table View Controller with the different data source for different tab bar items. So in the storyboard, i created two Navigation Controllers with same classes like this;
I also give different "Restoration ID" to each of them.
For the first one, I gave "navCont1" and "navCont2" for the second one.
In subclass("GeneralNavCont") of these Navigation Controllers; I override init method and check restoration id of self. Then i initiate my TableViewController and set its data source based on ids like this;
class GeneralNavCont: UINavigationController {
var dataSource1 = [Countries]()
var dataSource2 = [Cities]()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
initiateTableVCBasedOnId()
}
func initiateTableVCBasedOnId() {
let storyBoard = UIStoryboard(name: "Main", bundle: nil)
let tableVC = storyBoard.instantiateViewController(withIdentifier: "tableVC") as! MyTableViewController
if self.restorationIdentifier == "navCont1" {
tableVC.dataSource = self.dataSource1
self.viewControllers = [tableVC]
}
else if self.restorationIdentifier == "navCont2" {
tableVC.dataSource = self.dataSource2
self.viewControllers = [tableVC]
}
}
}
Hope it helps someone. Cheers.