iphone - 如何从 UINavigationController 堆栈上已有的 UIViewController 调用方法

标签 iphone ios objective-c delegates protocols

我在 UINavigationStack 上有一个 UIViewController,并且从这个 UIView 加载另一个 View ,而不是加载到堆栈上,而是作为 subview 。我加载的这个 View 只是应用程序的首选项 View ,我将其叠加到所显示的内容上。

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

我的问题是,有没有办法从 prefrencesViewController 调用 myViewController 中的方法?我正在尝试使用委托(delegate)和协议(protocol),但它不起作用,所以我希望有一种我还不知道的简单方法可以做到这一点,或者也许我可以在我的委托(delegate)/协议(protocol)方面获得一些帮助...

这就是我的委托(delegate)和协议(protocol)设置代码的样子

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

关于iphone - 如何从 UINavigationController 堆栈上已有的 UIViewController 调用方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16971983/

相关文章:

iphone - 匹配字典数组 - 其中一个数组不完全等于另一个数组

objective-c - 不要备份到 iCloud 但仍然被拒绝

ios - 每周本地通知

ios - iOS Objective C 中的 Tokbox 屏幕共享开/关切换

ios - 无法在另一个 View Controller 中显示缩略图

ios - 打开 UIViewController,不显示 UINavigationController 导航栏

ios - iPhone 锁定时解析 saveInBackground?

iphone - 控制帧速率或限制 AVCaptureVideoPreviewLayer

iOS 显示标注而不是注释

objective-c - NSTextFinder 和正则表达式搜索