简体   繁体   English

从AppDelegate访问ViewController

[英]Access ViewController from AppDelegate

My problem is the following, 我的问题如下,

In one of my ViewControllers , when the user taps a button , I register the device for notifications with this code. 在我的一个ViewControllers ,当用户点击一个按钮时,我使用此代码注册设备以获取通知。

[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];

Then, in the AppDelegte , there are two methods. 然后,在AppDelegte ,有两种方法。 One that receives the token and one that gets the error. 一个接收令牌,另一个收到错误。

- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken
- (void)application:(UIApplication*)application didFailToRegisterForRemoteNotificationsWithError:(NSError*)error

And now comes the problem, in didRegisterForRemoteNotificationsWithDeviceToken I need to send the token to my server, and with it some data that the user has entered in the View , like its username. 现在出现了问题,在didRegisterForRemoteNotificationsWithDeviceToken我需要将令牌发送到我的服务器,并使用它输入用户在View输入的一些数据,如用户名。

How can I get this data? 我怎样才能获得这些数据?

The NSNotificationCenter will serve you well here. NSNotificationCenter将在这里为您提供良好的服务。 In your AppDelegate's didRegisterForRemoteNotificationsWithDeviceToken , do this: 在AppDelegate的didRegisterForRemoteNotificationsWithDeviceToken ,执行以下操作:

[[NSNotificationCenter defaultCenter] postNotificationName:@"RegistrationReceived"
                                                    object:token];

And, in your controller's viewDidLoad , 并且,在你的控制器的viewDidLoad

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

Be sure to implement -updateRegistrationInfo: (or whatever you want to name it) to receive the NSNotification and the token, which is passed in as the argument. 确保实现-updateRegistrationInfo:或任何你想要命名的东西)来接收NSNotification和令牌,它作为参数传入。 Also, unregister for the notification when you no longer need it. 此外,当您不再需要时,请取消注册通知。

- (void)updateRegistrationInfo:(NSNotification *)notification
{
    NSString *myObject = [notification object];
    ...
}

You can can add your ViewController as an instance variable to your AppDelegate class: 您可以将ViewController作为实例变量添加到AppDelegate类中:

@interface AppDelegate : NSObject <UIApplicationDelegate>
{
@private // Instance variables

    UIWindow *mainWindow; // Main App Window

    UINavigationController *navigationController;

    UIViewController *someViewController;
}

And then add some methods to someViewController that returns your requested data. 然后向someViewController添加一些返回所请求数据的方法。

You can assign someViewController in - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions of the AppDelegate class in this way: 您可以指定在someViewController - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions中的AppDelegate这样的类:

someViewController = [[UIViewController alloc] initWithNibName:nil bundle:nil]; 

navigationController = [[UINavigationController alloc] initWithRootViewController:someViewController];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM