How to know - view controller's current view in iphone - iphone

Let's have an example.
In application I have a tab bar controller.
Tab bar has two items dynamically - two view controllers.
User can select any of tab.
Suppose user selects first tab.
First view controller is already loaded.
Now he clicks on a button of First view controller.
From First View controller -> Second View controller is pushed.
Now when user taps on tab bar first item
second view is popped out.
This is done through by default by tab bar controller.
Now, If I want to check following condition
if(tab bar first item-view controller has first view controller view)
then perform this
if(tab bar first item-view controller has second view controller view)
then perform this
How to implement this logic?

If you are using a UITabBarController, you can use its selectedViewController property to know what kind of view controller is on the screen, so if you have two subclasses of view controller FirstViewController and SecondViewController you can say
if([[tabBarController.selectedVIewController isKindOfClass:[FirstViewController class]])
//... do something
else ...

Related

How to add the default back button in the navigation view controller from storyboard or in swift?

Okay so I have one main view controller and then I added a navigation view controller from the 'Editor -> Embed In -> Navigation Controller' toolbar and then I segued from the first view controller button to the empty navigation controller scene because if I segued directly to the signup or signin VC the main view controller would end up having a bar on the top like the navigation view controller. But now i added an bar button item "Back" and segued to the main VC and the bar comes back with a builtin back button. So how can i add a default back button or any back button without having the main VC becoming a navigation view controller. Here is a link to an image: https://www.dropbox.com/s/1623a45v846ijn8/Screen%20Shot%202015-09-27%20at%2012.23.26%20PM.png?dl=0
The problem is that you have two navigation controllers when you should only have one.
You should make your initial view controller a UINavigationController, and set your previous initial view controller as the root. If you don't want to see the bar, just set the bar to be hidden on your viewWillAppear, and unhide it on the Sign In / Sign Up pages.

how to use UITabBarController's shouldSelectViewController delegate method

I have a tabbar-based application (tabbar controller is added in window itself) and all the navigation controller with their respective root view controllers are being set in window's xib. I have 4 tab bar items.
Suppose I click on item 1, then the root view controller for that item is being shown to me. This root view contains a table with 5 cells. If I click on a row, then a new view is pushed onto the navigation stack. Now, this pushed view has a button clicking on which will again push a new view controller. I have 4 such view controllers which are getting pushed one after the other on navigation stack.
Now, lets say I am on 3rd view in navigation stack and then, I have clicked on tab bar item 1 (the same on which I clicked earlier); then, the first root view controller is shown and my whole navigation stack is gone. I just don't want this to happen, that is, I want to remain on the 3rd view controller and also, be able to click on all tab bar items (dont want to disable any item). I know that it can be achieved through implementing tab bar controllers delegate method: shouldSelectViewController, but i dont know how??
perform a check for the currently selected viewcontroller.
if current is the same as the tab tapped, then return no in your delegate method. Think something like this is what you mean?
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController;
{
if ([[tabBarController viewControllers] objectAtIndex:tabBarController.selectedIndex] == viewController)
{
return NO;
}
else
{
return YES;
}
}

Why i can not push a view controller in my navigationController?

In my navigation based application, first view is sign in or signup view. After that i am using a view which is using tab view controller. That view has three tab items. Now i want to create a new view and push in navigationController. But its not working. But adding new view as subView in tabBarController View works. I want navigation for subViews for each tabBarItem? How can i do that?
See this
In your case just create three items and make them all UINavigationControllers like the first tab item in the above example.

Change Selected Index of Tab Bar Application from Modal View Controller

I'm creating a tab bar application using the built-in tab bar app template in Xcode. I have 4 tabs, one of which is a mapView. For a few of the view controllers my code programmatically sets the selected index of the tab bar depending on the user actions. For the mapView view controller I have a method that presents a modal view when the user taps on a selected annotation. The modal view has some information about that selected annotation. I can dismiss the modal view controller and return to the mapView correctly.
My problem is that I would like to put a 'home' button on the modal view controller that should dismiss the modal view and take the user to 0 index on the tab bar (AKA home). The mapView is index 3.
I cannot do a [self.tab setSelectedIndex:0] from the modal view attached to the home button - it doesn't work. Perhaps I'm overthinking this. Can anyone offer a solution / hint? I greatly appreciate it! Thanks.
When you present a modal view controller from one of the tabs, the tab bar controller instance becomes the parentViewController of the modal view controller. You can use this property to call the tab bar controller methods.
[(UITabBarController *)self.parentViewController setSelectedIndex:0];

Add a default view on a navigation view controller

I have a navigation controller and a tab bar where I have buttons for multiple options. When a button is ever pressed it pushes its respective viewcontroller in navigation controller. That's fine, but when I again press the button in tab bar after loading any other suboption viewcontroller in navigation controller, it shows blank screen instead of that "options screen" which it shows when I touch it first time.
I am using this code:
[self.navigationController pushViewController:accountOptions animated:NO];
You're pushing same view controller twice without ever poping it from navigation controller.
You can either pop view controller when switching to another one or push view controllers once on navigation controller in tab bar and be done with it.