如何从UIViewController调用一个已经在uiavigationcontroller栈上的方法



我有一个UIViewController在UINavigationStack和从这个UIView我加载另一个视图不是堆栈,但作为一个子视图。我加载的这个视图只是应用的一个偏好视图我覆盖到显示的东西上。

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

我的问题是,有没有一种方法来调用方法在myViewController从偏好viewcontroller ?我正在尝试使用委托和协议,但它不工作,所以我希望有一个简单的方法来做到这一点,我还不知道或者也许我可以得到一些帮助与我的委托/协议…

这是我的代码看起来像委托和协议设置

//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
}

相关内容

  • 没有找到相关文章

最新更新