简体   繁体   中英

How to call a method from a UIViewController thats already on the UINavigationController stack

I have a UIViewController on a UINavigationStack and from this UIView I load another view not onto the stack but as a subview. This view that I load is just a preferences view for the app that I overlay onto what ever is showing.

ie

myViewController <- on the stack button touch loads as a subview to myViewController
+ prefrencesViewController 

My question is, is there a way to call a method thats in myViewController from prefrencesViewController? I am trying to use delegates and protocols but its not working, so I am hoping there is either an easy way to do this I don't know about yet or maybe I could get some help with my delegate/protocol...

This is what my code looks like for delegate and protocol set up

//prefrencesViewController.h

@protocol GetPrefrencesViewControllerDelegate <NSObject>
-(void)reloadViewFromSavedPrefrences;
@end

//delegates and protocols
@property (nonatomic, weak) id <GetPrefrencesViewControllerDelegate> delegate;

//prefrencesViewController.m

//delegates and protocols
@synthesize delegate;

//.. inside button action
[[self delegate] reloadViewFromSavedPrefrences];

//myViewController.h

#import "prefrencesViewController.h"

@interface myViewController : UIViewController <UITabBarDelegate, GetGUIEncodedData, GetPrefrencesViewControllerDelegate> {

// prefrencesViewController set up
    prefrencesViewController *pvc;

@property (strong, nonatomic) prefrencesViewController *pvc;

//myViewontroller.h

@synthesize pvc;

- (void)viewDidLoad
{
    //..
    [pvc setDelegate:self];
}

//Delegate and prefrences.. Saved pressed reload the view here.
-(void)reloadViewFromSavedPrefrences {

    NSLog(@"WORKED");

}

any help would be greatly appreciated

I'm not sure that you are following the steps that I will present below but if you don't here is the example.

PresentedViewController.h

//import stuff
@protocol PresentedViewControllerDelegate <NSObject>
-(void)methodThatSouldBeImplementedByOtherController; //you can add params
@end

@interface PresentedViewController : UIViewController {
 //instance variables
}
@property (nonatomic, assign(week for ARK)) id<PresentedViewControllerDelegate>delegate
//public methods here

PresentedViewController.m

@implementation PresentedViewController 
@synthesize delegate;

//method implementation here

-(IBAction)buttonThatWillCallTheDelegate:(id)sender {

   if([self.delegate respondsToSelector:@selector(methodThatSouldBeImplementedByOtherController)]) {
    [self.delegate methodThatSouldBeImplementedByOtherController];
   }
}

ControllerThatWillPresent.h

@interface ControllerThatWillPresent : UIViewController <PresentedViewControllerDelegate> {
   //instance variables
}

//some methods maybe

ControllerThatWillPresen.m

@implementation ControllerThatWillPresen 

-(void)methodThatWillShowTheVC {
     PresentedViewController *vc = [PresentedViewController alloc] init]; //initWithNibname...
    vc.delegate = self;
   //presentVc, pushVc, addChild ... 
}

-(void)methodThatSouldBeImplementedByOtherController {
 //do stuff in delegate method
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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