Search code examples
iosobjective-cuinavigationcontrolleruitabbarcontrollersegue

Passing Data and go from VC tab index 4 to VC tab index 2


I wish to pass data and go from Search VC (tab index 4) to Classes VC (tab index 2) when I click on the Search Button. Search VC is embedded in a Tab View Controller. Classes VC is embedded in both a Tab View Controller and a Navigation Controller. See the attached pic.

Pics

I have tried using seague on the following code, but I lost my Tab Bar

Assuming I created a seague called SearchToClass from Search VC to Classes VC

#import "Search.h"    
#import "Classes.h"
#import "ClassNavigationController.h"

@interface Search(){

    NSString *sURL;
}

@end

@implementation Search

- (void)viewDidLoad
{
    [super viewDidLoad];
}

- (IBAction)btnSearch:(UIButton *)sender {

NSLog(@"This is the slider %@", lblStart.text);
sURL = @"&Start=";
sURL = [sURL stringByAppendingString:sStartTime];
sURL = [sURL stringByAppendingString:@"&End="];
sURL = [sURL stringByAppendingString:sEndTime];

[self performSegueWithIdentifier:@"SearhToClass" sender:self];

//======== I have also tried the following but couldn't pass the data and I lost the Tab Bar as well======================================
//ClassNavigationController *fromSearch =  (ClassNavigationController*)[storyboard instantiateViewControllerWithIdentifier:@"Classes"];
//[fromSearch setModalPresentationStyle:UIModalPresentationFullScreen];
//[self presentViewController:fromSearch animated:NO completion:nil];
//===================================================================
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    Classes *target = segue.destinationViewController;

    if ([segue.identifier isEqualToString:@"SearchToClass"]) {
    
        NSLog(@" To ClassesDet sURL : %@", sURL);
        target.urlFromSearch = sURL;
    }
 }
 @end

Solution

  • Here is what you can do. If you want data to be passed from search vc to classes vc.

    1. Add notification observer in classes vc
    NotificationCenter.default
                    .addObserver(forName:NSNotification
                        .Name(rawValue: "testKey"),
                                 object:nil, queue:nil, using: yourMethodToBeCalled) //set notification observer
    
    1. When you have searched for the item and want to pass it to classes vc
    NotificationCenter.default
                .post(name: NSNotification
                    .Name(rawValue: "testKey"),
                      object: nil, userInfo: ["myData": true, "myList": myList])
    
    1. Change to tab
      self.tabBarController?.selectedIndex = 2

      Note: No need for a segue when you are using a tab bar controller