Xcode iOS: UITabBar update objects while switching to another tab - ios

I want an action, that starts when the user is switching the tabs in a UITabBarController so I can update the labels and stuff on the View the user is switching to. Do you guys know any action, that does this thing.
Thanks.

The following method is called, when a tab is switched in UITabBar:
-(void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
This is delegate method of UITabBarDelegate protocol.

Related

Check if TabBar Item has been selected

I'm new in iOS programming, I'm wondering if there is a way to use TabBar inside normal view instead of UITabBarController.
This is my scenario:
I've a UIViewController with a TabBar and a Container. Container is embedded to a UIPageViewController to show different pages. I made this due to easy way to control layout in storyboard. The problem is that I don't know how to use tabBarController:didSelectViewController: function with this setup. I'm able to use it in normal UITabBarController but I want to force my custom view to works! This is my setup:
Storyboard Setup
Is there a way to catch a tab item selected action with this setup?
I found the solution. It was so simple!
Just setted self.myTabBar.delegate = self; and <UITabBarDelegate> and using -(void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item instead of tabBarController's one.

Cant set the Delegate of my UITabbar

I have a simple TabBar-Controller in my Storyboard.
ViewController 1 has a Navigation Controller around itself.
ViewController 2 does not have one.
ViewController 1 is shown first when the App goes up.
i need to implement the following Method:
-(void)tabBar:(UITabBar *)tabBar didSelectItem(UITabBarItem *)item
But i cant set the Delegate of the UITabBar. I Want to set the Deleagte in ViewController 1. In IB i can show the delegate property but i cant draw a line. i tried to set the delegate programmatically. But no matter where i set it, the app always crashes.
So where do i have to set the delegate of my UITabBar ?
You should make the App Delegate the delegate of your tab bar. You should be able to drag from the tab bar controller in storyboard to the AppDelegate (maybe file's owner).
In code, you can get a reference to your tab bar controller and set tbc.delegate = self in didFinishLaunchingWithOptions:.
In both scenarios, make sure to first make the delegate listen to the delegate methods by adding <UITabBarControllerDelegate> to the interface declaration.
I found the solution for this problem. All i did was the following, i created a class for my UITabBarController. I put the Protocol in to the header file. Switched to the .m and implemented the
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item Method.
And that's it. There is no need to set the delegate explicitly.
The delegate-Method is called now.

ios tab bar button to modal view

I'm using idev-recipes/RaisedCenterTabBar and I want a modal view called from the center button, not a camera.
Code is here:
https://github.com/boctor/idev-recipes/tree/master/RaisedCenterTabBar
Any ideas on how to get this working?
There is a better approach to follow in order to accomplish that. And much easier.
What I understand by implementing using this methodology: https://github.com/boctor/idev-recipes/tree/master/RaisedCenterTabBar is that strange things are happening when you are trying to hide the tab bar. So the best solution I found for me (the same thing as you do) is here: http://www.lantean.co/display-a-modal-uiviewcontroller-when-a-uitabbaritem-is-pressed/
There is no need to do anything else. Just ignore the view controller that the UITabBarItem is associated with and present your modal view! That's all!
I would create your own subclass of UITabBarController and then add in this method:
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
}
You will be able to tell what item was selected and then instantiate a modal VC inside there.
Probably you could just use the UITabBarDelegate, with the - (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item method. The method gets sent to the delegate, when somebody presses a button in the tab bar. In there you could check if it was the right button, and then instantiate the modal view controller.
Either with subclassing or by using the delegate, you can simply check if the item selected is your middle button and if it is, have the tab bar select the item that was previously selected and then present your model view controller. Since you'll be doing this within the same RunLoop source that the original selection happened, the tab selection will effectively be undone without ever switching to the middle VC.
According to the code sample provided by you =>
https://github.com/boctor/idev-recipes/tree/master/RaisedCenterTabBar
The central raised tab button is a UIButton, so just set the action of that button like this in BaseViewController.m class
[button addTarget:self action:#selector(showmodalview) forControlEvents:UIControlEventTouchUpInside];
and then in showmodalview method write this code=>
-(void)showmodalview
{
UIViewController *view1=[[UIViewController alloc] init]; // you can use any view controller instance you want ,this is just the example.
[self presentModalViewController:view1 animated:YES];
}

How to Set the Selected Item in a UITabBar From within the Code?

I'm implementing UITabBar in my app. I managed making it work by implementing UITabBarDelegate in my header file and using
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
in my .m file (as explained in this tutorial).
Now, there are 3 scenarios in my app where I wish to set the selected UITabBarItem manually from within the code (and not based on user action):
Upon viewDidLoad
After didReceiveMemoryWarning
In a certain case when the user is entering another view controller - when they get back, they should get back to a different tab than the one they clicked on.
Can anyone direct me to how this should be done?
The UITabBarController class has two properties for managing the selected tab, namely, selectedViewController and selectedIndex. Look into those in the reference.

UISplitViewController delegate methods not called

I am using a UISplitViewController inside a UITabBarController with a plain UIViewController in the master pane of the split view and a UINavigationController in the detail pane, which itself contains a vanilla UIViewController.
I am aware that Apple advise to use split views at the root level only, however I have seen other applications (eg, Amazon- 'Wish List' tab) that use split views in tabs so I'm sure it's possible.
My problem is that the delegate methods of the split view, ie. those in UISplitViewControllerDelegate do not get called, which prevents me from creating my pop-over menu when switching into Portrait mode.
The methods in question are the following -
// Called when a button should be added to a toolbar for a hidden view controller
- (void)splitViewController: (UISplitViewController*)svc willHideViewController:(UIViewController *)aViewController withBarButtonItem:(UIBarButtonItem*)barButtonItem forPopoverController: (UIPopoverController*)pc;
// Called when the view is shown again in the split view, invalidating the button and popover controller
- (void)splitViewController: (UISplitViewController*)svc willShowViewController:(UIViewController *)aViewController invalidatingBarButtonItem:(UIBarButtonItem *)barButtonItem;
// Called when the view controller is shown in a popover so the delegate can take action like hiding other popovers.
- (void)splitViewController: (UISplitViewController*)svc popoverController: (UIPopoverController*)pc willPresentViewController:(UIViewController *)aViewController;
The UISplitViewController does receive the rotation notifications.
I can get the willShowViewController method to be called if I force the status bar orientation to landscape right (or left) at the beginning of the app launch, using
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight];
However, the willHideViewController doesn't get called. And I don't want to force the app to start in landscape. If I do the same thing but force it to portrait, I don't receive the callbacks.
I don't understand why the split view controller is not calling it's delegate methods when it is otherwise behaving correctly. These methods should be called from its method-
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
internally, and when I breakpoint inside this, I can check that the delegate is set and that it is still alive.
Been stuck on this all day! Everything else is working great and I'm very pleased that the splitview / tabbar / navbar combination is working well. I just need these notifications.
Should I perhaps just call them manually when I rotate? Seems very wrong when the `UISplitViewController' should be doing this.
Solved, it has to be at either root level or a direct subview of a tabBar which also must be at root level. Annoying!
First, try to see if you are setting the correct delegates.
e.g., lets say you created three controllers,
UISplitViewController* splitView;
UIViewController* masterView;
UIViewController* detailView;
You implemented the delegate protocol at the detail view, so that when orientation changes, detail view should be able to put a button in the toolbar.
Now in order for splitView to call this function from delegate, you need to set the delegate itself.
So somewhere, if you are missing the following call,
splitView.delegate = detailView;
detailView's will never get notified of the orientation changes etc. At least this is where I was stuck.
I like the following method of sending a message from the master UIViewController to the detail UIViewController. Somewhere inside the master's implementation:
id detailViewController = [[self.splitViewController viewControllers] lastObject];
[detailViewController setSomeProperty:…];
This is from Paul Hegarty's Fall 2011 Stanford iTunesU iPad and iPhone Application Development course.

Resources