单击按钮时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);
}
}