Calling a function from a different view controller for iphone

后端 未结 3 1853
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-13 04:53

I have a problem where I want to call a function defined in one view controller from another controller. I\'v try what seems llke a hundred different setups and nothing seem

3条回答
  •  天命终不由人
    2021-01-13 05:23

    /* Ok, so based on a suggestion to use Notifications, I solved my problem.  It's
    actually so simple, it ridiculous I had so much trouble with it.   Thought I'd 
    post it just in case some other newbie hits the same type issue.
    */
    
    // in the SwitchViewController.m  I added this.  This sets it up so 
    // when the dealNotification
    // is triggered, the HideBar function I have defined in SwitchViewController 
    // gets called.
    [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(HideBar) name:@"dealNotification" object: nil];
    
    
    // in the GameViewController.m where I want to call the function in the other controller,
    // I added this and it send the notification to run the function
    [[NSNotificationCenter defaultCenter] postNotificationName:@"dealNotification" object: nil];
    

提交回复
热议问题