Objective C: How to reload a view controller's table view when tab is selected - objective-c

I need to reload the data in a view controller when it's tabbar is clicked.
I am using the UITabBarControllerDelegate method as below:
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
if (tabBarController.selectedIndex == 3)
{
[(SomeViewController *)viewController getData];
}
}
where 'getData' is an instance method in SomeViewController class. However when I run my app, I get the following error
2011-07-01 02:12:11.193 onethingaday[19169:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UINavigationController getData]: unrecognized selector sent to instance 0x600d500'
Can anyone advise me how I can overcome this issue? I just need to trigger the 'getData' method when tabbarcontroller.selected index ==3

It seems to me from the error message you get, that you use a UINavigationController in your tab controller; in this case, you cannot send directly the getData message to it; you should first find out which view controller under the UINavigationController should receive that message. (This is not actually related to the tab bar selectedIndex)
I don't know how your UINavigationController is organized, but you could do:
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
if (tabBarController.selectedIndex == 3) {
//-- option 1: getData goes to the first view controller in the UINavigationController:
[[(SomeViewController*)[(UINavigationController*)viewController topViewController] getData];
//-- option 2: getData goes to the last view controller in the UINavigationController (the visible one):
[[(SomeViewController*)[(UINavigationController*)viewController visibleViewController] getData];
}
}
If you give more details about the organization of your UINavigationController I can help further identifying the right option.
Anyway, as you can see from the casts, there is something that is not fully ok with your design. I would strongly suggest using a notification for that. I.e., your SomeViewController registers itself for a notification of a given type :
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(someSelector:)
name:ShouldGetDataNotification
object:nil];
and the tab bar controller sends the notification for your controller to react upon:
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
if (tabBarController.selectedIndex == 3) {
[[NSNotificationCenter defaultCenter] postNotificationName:ShouldGetDataNotification object:nil];
}
....
}
Look at this post.

See the solution to InterfaceBuilder - UIViewController subclass not recognized as subclass

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
if (tabBarController.selectedIndex == 3)
{
[[[(UINavigationController *)viewController viewControllers] objectAtIndex:2] getData];//2 for 3rd tabbar since 0,1,2
}
}

You could implement the -viewWillAppear method in your UITableViewController subclass. That should be called automatically when the UITabBarController switches to the view. It should look something like this:
- (void)viewWillAppear {
[super viewWillAppear];
[self getData];
}

Related

Trying to understand TabBarDelegate

In one of my ViewControllers I have the following code:
- (void)viewDidLoad
{
UITabBarController *tabBarController = (UITabBarController*)[UIApplication sharedApplication].keyWindow.rootViewController ;
[tabBarController setDelegate:self];
}
and :
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController: (UIViewController*)viewController {
NSLog(#"Yup!");
}
Whenever I switch tabs in my multi-tab setup, the console spits out
Yup
just as expected.
However, when I add
UITabBarController *tabController = (UITabBarController*)self.window.rootViewController;
tabController.selectedIndex = 1;
to my AppDelegate.m's
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:
the 'Yup' doesn't show anymore.
How come?
didSelectViewController will call when you select/change tabs from the app itself, it will not call when you set the selectedIndex programmatically
tabController.selectedIndex = 1; , will mostly useful when you want to set default tab or want to change the selectedIndex programmatically
From apple doc:
- (void)tabBarController:(UITabBarController *)tabBarController
didSelectViewController:(UIViewController *)viewController
it is called only in response to user taps in the tab bar and is not
called when your code changes the tab bar contents programmatically.
You can try calling that method manually like this:
- (void) selectedItemWithIndex:(int)value {
tabbar.selectedIndex = value;
[self tabBarController:tabbar didSelectViewController:tabbar.viewControllers.firstObject];//place you vc here by array or manually
}
Ref: https://stackoverflow.com/a/30700712/4557505

Tab bar item third touch for a table view not scrolling to the top

I have a tab bar item which is connected navigation controller with a UIViewController as the root view controller. The first touch on the tab bar item switches to that view. The second touch pops to the root view controller. The third touch does not scroll to the top.
I've seen this scroll-to-top behavior in other apps, but after searching the webs, I cannot find out anything about it.
Is this default behavior for scroll views or table views attached to tab bar items, or is it something I need to implement myself?
I realize this is an older question, but I'm also looking to create this behavior, and I think I have a simpler solution.
First, set your AppDelegate to be the delegate for your UITabBarController. Then add this method to AppDelegate.m:
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
if ([tabBarController.viewControllers objectAtIndex:tabBarController.selectedIndex]==viewController)
{
if ([viewController isKindOfClass:[UITableViewController class]])
{
[[(UITableViewController *)viewController tableView] setContentOffset:CGPointZero animated:YES];
}
else if ([viewController isKindOfClass:[UINavigationController class]])
{
UINavigationController *nav = (UINavigationController *)viewController;
if ([nav.visibleViewController isKindOfClass:[UITableViewController class]])
[[(UITableViewController *)nav.visibleViewController tableView] setContentOffset:CGPointZero animated:YES];
}
}
return YES;
}
This works if your tab points at a UITableViewController or at a UINavigationController with a UITableViewController as the root view, and you don't have to worry about distinguishing between which UITableViewController is affected, sending notifications, etc.
Here is the solution to scroll to top of the table view when tab bar is clicked
In AppDelegate set tabbar delegate
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
if (tabBarController.selectedIndex == 0) {
UINavigationController *selectedNav = [self.tabBarController.viewControllers objectAtIndex:self.tabBarController.selectedIndex];
UIViewController *currentVC = selectedNav.visibleViewController;
if([currentVC isMemberOfClass:NSClassFromString(#"HomeViewController")])
{
[[NSNotificationCenter defaultCenter] postNotificationName:#"refreshView" object:nil];
}
}
return YES;
}
In HomeViewController.m view did load listen for the notification
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(refreshView:)
name:#"refreshView"
object:nil];
Refresh method
-(void)refreshView:(NSNotification *) notification{
if (self == self.navigationController.topViewController)
[self.tableView scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:YES];
}
No, this isn't default behaviour, you have to implement it yourself.
I'd do it by making the application delegate the delegate of the tab bar controller, and implement -tabBarController:didSelectViewController: to post a notification. Listen for that notification in your table view controller and do something like:
if (self == self.navigationController.topViewController)
[self.tableView scrollToTop];
Since your tab controller can only have one delegate, you may want to look at the answer to this question, which describes how to listen for the tap using KVO.

Reload ViewController by clicking on TabBarItem

I'm kinda desperate right now :/
I have a Tab Bar Controller with 4 Items. In the 4. Tab I included a webView which shows a list of pdf's. If I open a PDF in the webView there is no way to go back to the main webView with the links. Is there a way by re-clicking the 4. TabBar to reload the View? If I change from the 3. to the 4. tabbar it works (viewWillAppear).
Someone told me, that the following method should work:
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
if ([viewController isKindOfClass:[UIColor class]]) {
//Try this if you're pushing the webView in another ViewController
[viewController.navigationController popToRootViewControllerAnimated:YES];
//or access to your webView and call goBack();
}
}
but actually I have no idea in which file I should insert that method. (See print Screen)
Thanks a LOT in advance for your help guys!
Subclass UITabBarController
1.1. Cmd+N and create a new instance of NSObject class, and name it TabBarController
1.2. in TabBarController.h replace NSObject so that it reads #interface TabBarController : UITabBarController <UITabBarControllerDelegate>
1.3. in TabBarController.m add this:
- (id) init
{
self = [super init];
if (self)
{
self.delegate = self;
}
return self;
}
1.4. and this
- (void)tabBarController:(UITabBarController *)tabBarController
didSelectViewController:(UIViewController *)viewController
{
// Is this the view controller type you are interested in?
if ([viewController isKindOfClass:[MehrViewController class]])
{
// call appropriate method on the class, e.g. updateView or reloadView
[(MehrViewController *) viewController updateView];
}
}
1.5. In IB, Inspection, change the class of Tab Bar Controller to your TabBarController (instead of UITabBarController)
1.6. You also need to include MehrViewController.h in TabBarController.m
Edit
in MehrViewController.m (as you posted in your question, assuming it has a webView)
// An example of implementing reloadView
- (void)reloadView {
[self.webView reload];
}

viewDidLoad issue

I am having problem with ViewDidLoad Method.I have one .Xib File in my app and 5 viewControlelr.And Each View Controller Contains some initialization and method calls.
Problem is at the startup all the ViewDidLoad method runs.
Is there any way i can do this just when my ViewController will be called and the View is Loaded on the screen?
What is the proper way in this case?
I tried like this code :
-(void)viewDidAppear
{
[ScoreWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://smwebtech.com/Pandit/web_service/getScore.php?u=aa"]]];
}
But my program is not reaching this method.Why?Am i missing something?Please take a look.
Use viewWillAppear method instead. It is only called when the view is going to appear on screen.
Or viewDidAppear which is only called after the view appears on screen.
EDIT:
Both viewWillAppear and viewDidAppear take a BOOL argument each:
- (void)viewWillAppear:(BOOL)animated {
}
- (void)viewDidAppear:(BOOL)animated {
}
EDIT2:
These two delegate methods have their corresponding 'opposite' methods that you can override to release resources you allocate in the former methods:
- (void)viewWillDisappear:(BOOL)animated {
[resource release];
[super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated {
[resource release];
[super viewDidDisappear:animated];
}

Objective C: How to disable user interaction to all of tab bars except one?

As what the title suggests, I would like to be able to lock all my tab bars except for one. And only after the user completes an action will I enable all the rest of the tab bars. How can I do that?
I haven't tried it, but according to the docs, you can return NO from the tabBarController:shouldSelectViewController: delegate.
[UPDATE] I just tried that out of curiosity - it seems to work fine. Create a new project from the "Tab bar application" template and then go to the -viewDidLoad of your FirstViewController. Add this line:
[self.tabBarController setDelegate:self];
and then implement the delegate method:
-(BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
if (userHasCompletedAction) {
return YES;
}
return NO;
}
Don't forget to conform to <UITabBarControllerDelegate> in your .h file!
Hope that helps.
You have to implement this method
- (void)tabBarController:(UITabBarController *)tabBarController1 didSelectViewController:(UIViewController *)viewController {
if ([tabBarController1 selectedIndex]==0) {
UITabBarItem *tabBarItem = [[[[self tabBarController]tabBar]items] objectAtIndex:1];
[tabBarItem setEnabled:FALSE];
}
}
You have to do something like this for disabling your required tabbar items.
The method tabBar:didSelectItem: in UITabBarDelegate could help.