gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 03:52:03 26 4
gpt4 key购买 nike

我在 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com