如何初始化VC或Class iOS,ObjectiveC

时间:2018-04-18 08:46:43

标签: ios objective-c nsnotificationcenter uitabview

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

在首次启动应用时,由于SecondVC尚未初始化,因此无法将数据传递给SecondVCNSNotificationCenter无法正常运行。只有在SecondVC初始化后,NSNotificationCenter才能正常运行。

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

或者我如何以编程方式调用SecondVC的标签。

FirstVC

#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);
    }
}

2 个答案:

答案 0 :(得分:3)

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

只需从SecondViewController获取self.tabBarController并调用该方法。

第一个VC

- (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
}

答案 1 :(得分:0)

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

您的Singleton对象如下所示:

接口:

a_new = [sublist for sublist in a if sublist[-1] >= 3]

实现:

@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

和你的viewDidLoad在secondVC

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