传递数据并从 VC 选项卡索引 4 转到 VC 选项卡索引 2

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

我希望在单击“搜索”按钮时传递数据并从 Search VC(选项卡索引 4)转到 Classes VC(选项卡索引 2)。 Search VC 嵌入在 Tab View Controller 中。 Classes VC 嵌入在 Tab View ControllerNavigation Controller 中。见附图。

我尝试在以下代码中使用 seague,但我丢失了 Tab Bar

假设我创建了一个名为 SearchToClass 的海域,从 Search VCClasses 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

这是您可以执行的操作。如果要将数据从搜索 vc 传递到 类 vc.

  1. 在类vc
  2. 中添加通知观察者
NotificationCenter.default
                .addObserver(forName:NSNotification
                    .Name(rawValue: "testKey"),
                             object:nil, queue:nil, using: yourMethodToBeCalled) //set notification observer
  1. 当您搜索了该项目并想将其传递给 类 vc
NotificationCenter.default
            .post(name: NSNotification
                .Name(rawValue: "testKey"),
                  object: nil, userInfo: ["myData": true, "myList": myList])
  1. 切换到选项卡
    self.tabBarController?.selectedIndex = 2

    注意:使用标签栏控制器时不需要 segue