【问题标题】:Passing Data and go from VC tab index 4 to VC tab index 2传递数据并从 VC 选项卡索引 4 转到 VC 选项卡索引 2
【发布时间】:2018-09-27 03:44:15
【问题描述】:

当我单击搜索按钮时,我希望传递数据并从 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

【问题讨论】:

    标签: ios objective-c uinavigationcontroller uitabbarcontroller segue


    【解决方案1】:

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

    1. 在 vc 类中添加通知观察者
    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

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

    【讨论】:

    • 如何定向到 Classes VC?
    • 您的 ClassesVC 位于索引 2 右侧,所以这里是执行该操作的代码 self.tabBarController?.selectedIndex = 2 //这是索引
    • 如何将我的sURL 传递给Classes VC。我如何通过Classes VC 接收sURL
    • 我认为您对 NSNotifications 不了解,可能先阅读教程,以便您了解一些情况。我已经在这里写了上面的代码是如何传递 NotificationCenter.default .post(name: NSNotification .Name(rawValue: "testKey"), object: nil, userInfo: ["myData": sURL ])
    • 因为类 vc 已经在 viewDidLoad 中设置了一个观察者,所以只要你发布一些东西,它就会在 classesVC 中收到。然后您需要执行第 3 步来更改视图并转到 classesVC。
    猜你喜欢
    • 2019-09-25
    • 2016-09-29
    • 1970-01-01
    • 2016-05-11
    • 1970-01-01
    • 2015-02-12
    • 2014-11-22
    • 1970-01-01
    相关资源
    最近更新 更多