简体   繁体   English

从AppDelegate发送通知到ViewController

[英]Send a notification from AppDelegate to ViewController

need your help. 需要你的帮助。 I implement these Delegate Method to the AppDelegate.m: 我将这些Delegate方法实现到AppDelegate.m:

    -(BOOL)application:(UIApplication *)application
           openURL:(NSURL *)url
 sourceApplication:(NSString *)sourceApplication
        annotation:(id)annotation {
    if (url != nil && [url isFileURL]) {
        //Valid URL, send a message to the view controller with the url
    }
    else {
        //No valid url
    }
    return YES;

But now, I need the URL not in the AppDelegate but in my ViewController. 但是现在,我需要的URL不在AppDelegate中,而在ViewController中。 How I can "send" them the url or how I can implement these Delegate Method to the ViewController? 我如何“发送” URL或如何将这些Delegate方法实现到ViewController?

you can use NSNotificationCenter as shown below: 您可以使用NSNotificationCenter ,如下所示:

Firstly post Notification in your app delegate as : 首先在您的应用程序委托中将Notification发布为:

NSDictionary *_dictionary=[NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:[NSString stringWithFormat:@"%d",newIndex],nil] forKeys:[NSArray arrayWithObjects:SELECTED_INDEX,nil]];

[[NSNotificationCenter defaultCenter] postNotificationName:SELECT_INDEX_NOTIFICATION object:nil userInfo:_dictionary];

then register the ViewController you want to observe this notification as 然后将您要观察此通知的ViewController注册为

/***** To register and unregister for notification on recieving messages *****/
- (void)registerForNotifications
{
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(yourCustomMethod:)
                                                 name:SELECT_INDEX_NOTIFICATION object:nil];
}

/*** Your custom method called on notification ***/
-(void)yourCustomMethod:(NSNotification*)_notification
{
    [[self navigationController] popToRootViewControllerAnimated:YES];
    NSString *selectedIndex=[[_notification userInfo] objectForKey:SELECTED_INDEX];
    NSLog(@"selectedIndex  : %@",selectedIndex);

}

call this method in ViewDidLoad as : 在ViewDidLoad中将此方法称为:

- (void)viewDidLoad
{

    [self registerForNotifications];
}

and then on UnLoad remove this observer by calling this method : 然后在UnLoad上通过调用以下方法删除此观察器:

-(void)unregisterForNotifications
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:SELECT_INDEX_NOTIFICATION object:nil];
}


-(void)viewDidUnload
{
    [self unregisterForNotifications];
}

Hope it helps you. 希望对您有帮助。

You can post a local notification like this, where notification name will be used by the receiver to subscribe. 您可以像这样发布本地通知,接收者将在其中使用通知名称进行订阅。

[[NSNotificationCenter defaultCenter] 
    postNotificationName:@"Data" 
    object:nil];

And in your viewController subscribe the notification. 并在您的viewController中订阅通知。

[[NSNotificationCenter defaultCenter] 
    addObserver:self 
    selector:@selector(getData:) 
    notificationName:@"Data" 
    object:nil];

- getData:(NSNotification *)notification {
    NSString *tappedIndex = [[_notification userInfo] objectForKey:@"KEY"];
}

For More About the NSNotificationCenter Can go with this link 有关NSNotificationCenter的更多信息, 可以使用此链接

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

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