1

This is the hierarchy of my app

  1. There is a TabBarViewCOnyroller with 4 tabs
  2. Each tab shows a TableListViewOCnroller
  3. When user selects a row in the the TableListViewCOntrilelrs, different view controllers are pushed depending on which tab and which row. Each of these has a navigate back button to that will take the UI back to Tab-Bar view controller

For e.g. Chat tab displays a list of chats in timestamp sorted order. The last chat in the exchange with each far-end is shown. When user selects a certain row, it goes it into a view controller that shows all the messages between user and far-end who is connected with the row.

When I get a push notification with a message from a particular far-end, and user selects it, I want the app to directly shift to the view controller with all the messages with the far-end with a navigate back button pointing to the "Chat tab". I know how to show the message display view controller programmatically, but I don't know how to embed it in a way that will allow user to navigate back to the chat tab.

Can someone point me to some example code on how this can be achieved. Sorry to be asking for a ready-made solution. Too much base-framework stuff to master, so no time to master UI stuff!

EDIT with code I am using

    NSInteger index = 1 ;// the index of the chat tab
    UITabBarController *myTabBarController = (UITabBarController *)self.window.rootViewController;
    myTabBarController.selectedIndex = index;
    UINavigationController *navVC = myTabBarController.viewControllers[index];

    ChatListTableViewController *listVC = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"chatlistview"];
    listVC.navigationItem.backBarButtonItem.title = @"Chats";

    MessageDisplayViewController *detailVC = [[MessageDisplayViewController alloc] init];
    //Initialize all the variables needed by JSQMessage class
    detailVC.hidesBottomBarWhenPushed = YES;
    detailVC.title = detailVC.recipient = userInfo[@"sender"];
    //To Do Map farEndID to farEndNickName using unique user table
    detailVC.recipientNickName= detailVC.recipient ;
    detailVC.managedObjectContext=self.managedObjectContext ;
    detailVC.senderDisplayName=[[NSUserDefaults standardUserDefaults] stringForKey:@"NickName"];
    detailVC.senderId=[[NSUserDefaults standardUserDefaults] stringForKey:@"UserID"];

    //navVC.viewControllers = @[listVC, detailVC];
    navVC.viewControllers = @[listVC];
    [navVC pushViewController: detailVC animated:NO];
Smart Home
  • 801
  • 7
  • 26

1 Answers1

1

To change the tab bar controller to a tab, use

NSInteger index = // the index of the chat tab
myTabBarController.selectedIndex = index;

The UINavigationController at that tab can be manipulated programmatically as well.

  1. Get the navigation controller at the tab:

    UINavigationController *navVC = myTabBarController.viewControllers[index];
    
  2. Build the root view controller that shows all the conversations as you normally do.

    UIViewController *listVC = // you build this somehow in your code already... do that
    listVC.title = @"Chat";
    
  3. Build the view controller that shows a conversation detail as you normally do, initializing it with the newly received message.

    UIViewController *detailVC = // you build this somehow in your code already... do that
    
  4. Replace the navigation view controller stack.

    navVC.viewControllers = @[listVC];
    [navVC pushViewController: detailVC animated:NO];
    
danh
  • 62,181
  • 10
  • 95
  • 136
  • Thanks a ton @danh. One small issue. The text associated with the back navigation bar says "Back" instead of "Chat". How do I set the title of text associated with the back navigation to make it same as the name of the tab bar it is navigating back to? – Smart Home Dec 30 '15 at 06:17
  • Please see edit. The title can be set on the listVC, or on its `navigationItem.backBarButtonItem.title`. This is probably being done someplace in your code already (maybe at or after viewWillAppear on that vc). – danh Dec 30 '15 at 06:21
  • Thanks. That does not seem to work, it still says "Back". Right now my initial Tab View Controller and the view controllers linked to each tab are being done through Storyboard. When I am in the "Chats" tab it is linked to the "ChatListViewController" through Storyboard. When in this tab and a row is selected, I call a [self.navigationController pushViewController:vc animated:YES]; to push the "messagesListViewContriller" that shows the sequence of messages with the particular user. I am assuming the backbutton is being named automatically when I do this since I don't see it in my code – Smart Home Dec 30 '15 at 16:58
  • And in the code just suggested in this answer, do you build the ChatListViewController by using the storyboard? (instatantiateViewControllerWithId) ? – danh Dec 30 '15 at 17:00
  • Back button title is a confusing aspect of the sdk (see here http://stackoverflow.com/questions/2197698/how-to-set-the-text-of-a-back-button-on-a-uinavigationbar and see the duplicate it refers to). Just in case I'm wrong about the title setting already edited (I don't think I am), try the pushViewController method I just edited into step 4. – danh Dec 30 '15 at 17:09
  • Thanks for all the time and help on this. Still not working. I will look up the SO links you have given. I have also placed the code I am using in the edit above. If something pops out form the code, pls let me know. Thanks one again. – Smart Home Dec 30 '15 at 18:24
  • Yes. Something does jump out. Please see the edit. Try just setting the title on the listVC. `listVC.title = @"Chat";` and remove that line where you reference the backBarButtonItem – danh Dec 30 '15 at 18:29
  • Thanks. That fixed it! BTW, is there any quick and easy reference to learn about working on UI programmatically including auto-layout. I am focussing more on core frameworks, but I am ending up spending a lot of time with this UI stuff. So, I might as well bite the bullet once to get at least basics right. – Smart Home Dec 30 '15 at 18:33
  • Glad it worked, but ugh. Sorry! Maybe its just me, but I'm perpetually flummoxed by back navigation items – danh Dec 30 '15 at 18:34
  • I am perpetually flummoxed by anything UI, esp auto-layout! I think my edit went through after your comment, so just repeating the Q. Do you know any reference book (short/sweet variety the better) to learn about working on UI programmatically including auto-layout. – Smart Home Dec 30 '15 at 18:38
  • 1
    Its a huge library, so short or sweet is out of the question. I did my learning on the mean streets, and the best reference material I found (not that its very good) came directly from the deathstar: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIKit_Framework/ Here for auto layout: https://developer.apple.com/library/tvos/documentation/UserExperience/Conceptual/AutolayoutPG/index.html (ps I realize this is a terrible answer. It is, IMO, the least terrible of many alternatives) – danh Dec 30 '15 at 18:47