0

我在 UINavigationStack 上有一个 UIViewController ,从这个 UIView 我加载另一个视图不是到堆栈上而是作为子视图。我加载的这个视图只是应用程序的首选项视图,我覆盖在所显示的内容上。

IE

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");

}

任何帮助将不胜感激

4

1 回答 1

1

我不确定您是否遵循我将在下面介绍的步骤,但如果您不这样做,这里就是示例。

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
}
于 2013-06-06T21:14:10.853 回答