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

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.

Related

Force Landscape Orientation on iOS 6 in Objective-C

I have a master view controller that's inside a UINavigationController. In that master view controller, I have a button that pushes a detail view controller that has a UIWebView inside of it. I want this detail view controller to be on landscape mode when it's loaded. Going back to the master view controller, it forcibly goes back again to portrait mode. I'm running iOS 6 on this.
I have seen the other similar questions but it's not working on my end. I have created a LandscapeViewController that's a subclass of UIViewController where I have written these methods:
#pragma mark - Orientation Methods
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscape;
}
- (BOOL)shouldAutorotate
{
return YES;
}
This is my code when I push the detail view controller:
DetailViewController *detailVC = [[DetailViewController alloc]
initWithNibName:#"DetailViewController"
bundle:nil];
[self.navigationController pushViewController:detailVC
animated:YES];
I'm thinking on where to subclass my LandscapeViewController on the code above to make it work or on how to properly subclass and push my detail view controller. I can also present my detail view controller modally if it's not possible for the navigation controller to push my detail view controller from portrait to landscape. Where am I doing it wrong?
Considering:
View A: Portrait only - View B: Landscape only
I couldn't do it in the navigation controller. Instead what I did was to open a modal view from view A to view B and force a new navigation controller into this view.
This is working for me in iOS5+.
You need to create a category for the navigation controller like this:
UINavigationController+Rotation_IOS6.h
#import <UIKit/UIKit.h>
#interface UINavigationController (Rotation_IOS6)
- (BOOL)shouldAutorotate;
- (NSUInteger)supportedInterfaceOrientations;
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation;
#end
UINavigationController+Rotation_IOS6.h
#import "UINavigationController+Rotation_IOS6.h"
#implementation UINavigationController (Rotation_IOS6)
- (BOOL)shouldAutorotate
{
return [self.topViewController shouldAutorotate];
}
- (NSUInteger)supportedInterfaceOrientations
{
return [self.topViewController supportedInterfaceOrientations];
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return [self.topViewController preferredInterfaceOrientationForPresentation];
}
#end
In AppDelegate.m add:
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
return UIInterfaceOrientationMaskAll;
}
Then in View A:
- (BOOL)shouldAutorotate {
return YES;
}
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationPortrait;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown);
}
also in View A to open View B do this:
ViewB *vc = [[ViewB alloc] initWithNibName:#"ViewB" bundle:nil];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:vc];
[self presentViewController:navigationController animated:YES completion:nil];
And, finally, in View B
- (BOOL)shouldAutorotate {
return YES;
}
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskLandscapeLeft;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationLandscapeRight;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationLandscapeRight || interfaceOrientation == UIInterfaceOrientationLandscapeLeft);
}
They kinda screwed the pooch in iOS 6 with regard to this. Here's what I've figured out so far:
First off, Apple added the "Supported Interface Orientations" buttons with Xcode 4.5. This corresponds to the "Supported interface orientations" attribute in _info.plist. These buttons must be toggled to the correct choices before any of the rest will work. (If the buttons seem to refuse to toggle it's likely because the info.plist is locked by CVS or some other process.)
Next, the property .window.rootViewController must be set, and must point to the "bottom" view controller in the stack. Generally this will be either a navigation controller or a tab controller.
If the desire is to disable all rotation, this can be done using the buttons, or one can implement, in the "bottom" view controller, the "shouldAutorotate" method and have it return NO. (If the method is omitted then the default is YES.)
In spite of having autorotation disabled with shouldAutorotate, if there is a MPMoviePlayerViewController being displayed, that will autorotate. Only toggling the supported interface orientation buttons appears to prevent this.
If one wants to conditionally autorotate other view controllers it gets messier. Basically, your "bottom" view controller must implement the supportedInterfaceOrientations method and return, based on the current topViewController, the appropriate bit mask. This can be done with a routine that queries the topViewController's old "shouldAutorotateToInterfaceOrientation" method, but it's a bit ugly. And even though this scheme doesn't require modifying the rotating view controller's code, you DO need to modify the VC just "below" the rotated one to implement "supportedInterfaceOrientation", or else that view will be rotated on return. (At least this is a simple copy/paste.) No one seems to have come up with a better, more general scheme, though.

Need a really simple navigation controller with a table view inside a tab bar controller

I have an app with a tab bar controller (2 tabs). In one tab view controller, a button leads to an alert window. I want one button of the alert window to call a table view containing possible answers. I want that table view to have a done button and a title. I think that means a navigation controller has to be used. But most everything I can find on navigation controllers assumes a much more complicated situation. Here's part of the alert window logic:
-(void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 2) {
AnswersViewController *aVC = [[AnswersViewController alloc] init];
[self presentViewController:aVC
animated:YES
completion:NULL];
}
}
And AnswersViewController looks like this:
#interface AnswersViewController : UITableViewController
#end
#implementation AnswersViewController
- (id) init
{
self = [super initWithStyle:UITableViewStylePlain];
return self;
}
- (id) initWithStyle:(UITableViewStyle)style
{
return [self init];
}
- (void)viewDidLoad
{
[super viewDidLoad];
[[self view] setBackgroundColor:[UIColor redColor]];
}
#end
This code all works as expected (an empty red UITableView appears).
Two questions I guess: 1. Is there a simple modification to what I have that can give me a done button and title in my table view? 2. If I have to go to a navigation controller (probably), how can I make a bare-bones navigation controller with a done button and title and embed the table view within it? Oh, and I want to do this programatically. And I think I prefer the done button and title to be in the navigation bar, no tool bar desired. Thanks!
To get what you are looking for, you do need to use a UINavigationController. That will provide the UINavigationBar where you can display a title and also buttons.
To implement this with a UINavigationController, you want to do smoothing like this (assuming you are using ARC, so you don't need to worry about memory management):
-(void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 2) {
AnswersViewController *aVC = [[AnswersViewController alloc] init];
//Make our done button
//Target is this same class, tapping the button will call dismissAnswersViewController:
aVC.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(dismissAnswersViewController:)];
//Set the title of the view controller
aVC.title = #"Answers";
UINavigationController *aNavigationController = [[UINavigationController alloc] initWithRootViewController:aVC];
[self presentViewController:aNavigationController
animated:YES
completion:NULL];
}
}
Then you would also implement - (void)dismissAnswersViewController:(id)sender in the same class as the UIAlertView delegate method (based on the implementation I have here).
Hope this helps!

Trigger events when switching views

It is easy.
Suppose I have two views: firstView and SecondView.
firstView is the ROOT view.
I load secondView from firstView:
secondView *secondViewController;
secondViewController = [[SecondView alloc]
initWithNibName:#"SecondView" bundle:nil];
[self.view addSubview:SecondViewController.view];
I added a "Back" button in secondView.
When I click that button I go back to firstView:
[self.view removeFromSuperView];
Here is the question:
When firstView -> secondView, viewDidLoad in secondView is triggered.
How can I trigger an event to inform firstView when I go back using removeFromSuperView in secondView?
What do you want to do exactly ? maybe you just want to go from your first view controller to your second view controller ? and then go back to your first view controller ? in this case just do like this, if your first controller is already embbeded in a navigation controller:
[self.navigationController pushViewController:secondViewController animated:YES];
But if you just want to create a view from Interface Builder and then add it above your first view, you can use the notification center to post an event when you click on your Back button from your second view:
- (void) backButtonClicked:(id)sender {
[self.view removeFromSuperView];
[[NSNotificationCenter defaultCenter] postNotificationName:#"back"
object:self.view];
}
You can then add an observer for this event in your first view controller, like this:
- (void) pushSecondViewController {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(secondViewDidGoBack:)
name:#"back"
object:secondViewController.view];
SecondView *secondViewController = [[SecondView alloc] initWithNibName:#"SecondView"
bundle:nil];
[self.view addSubview:secondViewController.view];
}
- (void) secondViewDidGoBack:(NSNotification *)notification {
[[NSNotificationCenter defaultCenter] removeObserver:self];
NSLog(#"My Second View Did Go back !");
}
I hope this will help you !

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

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];
}

Can't show a nagivationbar after hiding it

I have the following in the mainwindow.xib
Navigation Controller
List item
Tab bar controller
tabbar
firstViewController
SecondViewController
The entrypoint
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
navController.viewControllers = [NSArray arrayWithObject:tabBarController];
[window addSubview:navController.view];
[window makeKeyAndVisible];
return YES;
}
and now in the first viewcontroller i'm writing
-(void)loadView
{
if(rootAppDelegate==nil)
rootAppDelegate=(tabbarAppDelegate *) [[UIApplication sharedApplication]delegate];
listEmergencyItems= rootAppDelegate.listOfEmergencySectionItems;
self.rootAppDelegate.navController.navigationBarHidden = NO;
[super loadView];
}
and in the second viewcontroller i'm writing
- (void)loadView
{
if(rootAppDelegate==nil){
rootAppDelegate=(tabbarAppDelegate *) [[UIApplication sharedApplication]delegate];
}
listHospitalsItems= self.rootAppDelegate.listOfHospitalsItems;
self.rootAppDelegate.navController.navigationBarHidden = YES;
[super loadView];
}
And on the runtime, when it first loads the first view, i see the navigationbar where i need to navigate into a detail view.
And when i press the second tab bar item, i go to the second view, and the navigation bar gets hidden.
But when i press back on the first tabbar item, i.e. returning to the first viewcontroller. the navigation bar remains hidden.
Any idea?
The navigation bar won't show in the second view because neither view was placed on the navigationcontroller's stack. you want to use something like this in the parent to present a child view instead of overriding loadView
ViewToPresentViewController *myVController = [[ViewToPresentViewController alloc] initWithNibName:#"ViewToPresentViewController"
bundle:nil];
myVController.property = someValue;
[self.navigationController pushViewController:myVController
animated:YES];
[myVController release];
then, as i said previously, you can just use [self.navigationController setNavigationBarHidden:animated:]