如何从已经在UINavigationController栈中的UIViewController调用一个方法
我有一个UIViewController在UINavigationStack和从这个UIView我加载另一个视图不到堆栈,但作为子视图。 我加载的这个视图只是一个应用程序的首选项视图,我把它叠加到所展示的任何东西上。
即
myViewController <- on the stack button touch loads as a subview to myViewController + prefrencesViewController
我的问题是,有没有办法从prefrencesViewController调用myViewController中的方法? 我试图使用委托和协议,但它不工作,所以我希望有一个简单的方法来做到这一点,我不知道或者也许我可以得到一些帮助我的委托/协议…
这是我的代码看起来像委托和协议设置
//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"); }
任何帮助将不胜感激
我不确定你是否按照下面将要介绍的步骤操作,但如果你不在这里的话就是这样的例子。
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 }