如何初始化 VC 或 Class iOS, Objective C
How to initialise a VC or Class iOS, ObjectiveC
当在 FirstVC
处单击按钮时,它将使用 NSNotificationCenter
传递数据并触发 SecondVC
在应用程序初始启动期间,由于SecondVC
尚未初始化,因此无法将数据传递给SecondVC
。 NSNotificationCenter
无法正常运行。只有在 SecondVC
初始化后,NSNotificationCenter
才能正常运行。
所以我需要在某处初始化SecondVC
。会在 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
吗?
或者如何以编程方式调用 SecondVC
的选项卡。
第一VC
#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);
}
}
您在 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;
}
在我看来,您不需要在这种情况下使用通知或单例。
简单地说,从self.tabBarController
获取SecondViewController
并调用方法。
第一个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
}
当在 FirstVC
处单击按钮时,它将使用 NSNotificationCenter
SecondVC
在应用程序初始启动期间,由于SecondVC
尚未初始化,因此无法将数据传递给SecondVC
。 NSNotificationCenter
无法正常运行。只有在 SecondVC
初始化后,NSNotificationCenter
才能正常运行。
所以我需要在某处初始化SecondVC
。会在 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
吗?
或者如何以编程方式调用 SecondVC
的选项卡。
第一VC
#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);
}
}
您在 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;
}
在我看来,您不需要在这种情况下使用通知或单例。
简单地说,从self.tabBarController
获取SecondViewController
并调用方法。
第一个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
}