-1

单击按钮时FirstVC,它将传递数据并SecondVC使用 NSNotificationCenter触发

在应用程序的初始启动过程中,由于SecondVC尚未初始化,因此无法将数据传递给SecondVC. NSNotificationCenter无法正常运行。只有经过SecondVC初始化后,NSNotificationCenter才能正常运行。

所以我需要在SecondVC某个地方初始化。会在- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions吗?

或者我如何以编程方式调用SecondVC.

第一风投

#import "Search.h"

#import "Classes.h"
#import "MyTabBarController.h"

@interface Search(){

    AppDelegate *appDelegate;
    CERangeSlider* _rangeSlider;
    NSString *sURL, *strResult, *sRemaining, *sStartTime, *sEndTime,     *sSelectedLat, *sSelectedLong;
}

@end

@implementation Search

- (void)viewDidLoad
{
    [super viewDidLoad];

}


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

    self.tabBarController.selectedIndex = 1;

    sURL = @"Testing 123";

    NSDictionary *userInfo = [NSDictionary dictionaryWithObject:sURL forKey:@"theURL"];

     [[NSNotificationCenter defaultCenter] postNotificationName:@"toClasses" object:nil userInfo:userInfo];

}

@end

第二个VC

- (void)viewDidLoad
{
[super viewDidLoad];

[[NSNotificationCenter defaultCenter]
 addObserver:self
 selector:@selector(receiveTestNotification:)
 name:@"toClasses"
 object:nil];

    dtDate = [[NSMutableArray alloc] init]; //=== Mutable array to store the dates generated
    self.currentPageIndex = 0;

    [self setupSegmentButtons];

    NSDate *now = [NSDate date];
    NSDateFormatter *dateFormatter=[[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"dd/MM/YYYY"];
    sDtDate = [dateFormatter stringFromDate:now];

    [self LoadClasses];
}

-(void)viewWillAppear:(BOOL)animated {

    //--- Hide the Top Navigation Controller Bar at the current View
    [[self navigationController] setNavigationBarHidden:YES animated:YES];

}

//--- Top Navigation Controller reappear on the next VC
-(void)viewDidDisappear:(BOOL)animated{

    [[self navigationController] setNavigationBarHidden:NO animated:YES];
}

-(void) receiveTestNotification:(NSNotification*)notification
{
    if ([notification.name isEqualToString:@"toClasses"])
    {
        NSDictionary* userInfo = notification.userInfo;
        NSLog (@"Successfully received userInfo! %@", userInfo);
        NSString* sFromSearch = [NSString stringWithFormat: @"%@", userInfo];
        NSLog (@"Successfully received test notification! %@", sFromSearch);
    }
}
4

2 回答 2

2

在我看来,您不需要在这种情况下使用通知或单例。

简单地说,获取SecondViewControllerself.tabBarController调用该方法。

第一风投

- (IBAction)btnSearch:(UIButton *)sender {
  self.tabBarController.selectedIndex = 1;

  sURL = @"Testing 123";

  UINavigationController* secondNav = (UINavigationController*)self.tabBarController.viewControllers[1];
  SecondViewController* secondViewController = [secondNav.viewControllers firstObject];

  [secondViewController handleString:sURL];
}

第二个VC

- (void)handleString:(NSString*)string {
  // Do whatever you want with string passed from First VC
}
于 2018-04-18T09:45:59.677 回答
0

您在 viewDidLoad 中添加了观察者,因此即使您在用户点击按钮并发送通知之前创建它也不会工作。因为观察者不会被注册。在这种情况下,我建议您不要使用观察者发送数据。您可以将此数据保存在其他地方并在 seconVC 加载时使用它。例如在单例对象中。

您的 Singleton 对象如下所示:

界面:

@interface DataManager : NSObject
@property (nonatomic, strong) NSDictionary *userInfo;
+ (DataManager *) getInstance;
@end

执行:

@implementation DataManager

+ (DataManager *) getInstance {
    static DataManager *appManager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        appManager = [[DataManager alloc] init];
    });

    return appManager;
}

@end

现在您可以在任何地方访问该对象,并且可以确保只创建一个实例。

这是您的按钮单击方法:

- (IBAction)btnSearch:(UIButton *)sender {
    self.tabBarController.selectedIndex = 1;
    sURL = @"Testing 123";
    NSDictionary *userInfo = [NSDictionary dictionaryWithObject:sURL forKey:@"theURL"];
    [DataManager getInstance].userInfo = userInfo;
}

和你在 secondVC 中的 viewDidLoad

- (void)viewDidLoad {
    [super viewDidLoad];

    NSDictionary *userInfo = [DataManager getInstance].userInfo;
}
于 2018-04-18T09:22:46.490 回答