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

时间:2018-04-17 08:46:03

标签: ios objective-c uinavigationcontroller uitabbarcontroller segue

当我点击搜索按钮时,我希望传递数据并从Search VC(选项卡索引4)转到Classes VC(选项卡索引2)。 Search VC已嵌入Tab View ControllerClasses VC嵌入了Tab View ControllerNavigation Controller。见附图。

Pics

我尝试在以下代码中使用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

1 个答案:

答案 0 :(得分:0)

这是你能做的。如果您希望将数据从搜索vc传递到类vc。

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

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