如何从已经在UINavigationController堆栈上的UIViewController调用一个方法 [英] How to call a method from a UIViewController thats already on the UINavigationController stack

查看:171
本文介绍了如何从已经在UINavigationController堆栈上的UIViewController调用一个方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



ie

  myViewController<  - 在堆栈按钮上触摸负载作为myViewController的子视图
+ prefrencesViewController

我的问题是,是否有一种方法调用一个方法在myViewController从prefrencesViewController?我试图使用委托和协议,但它不工作,所以我希望有一个简单的方法来做这个我不知道,或者也许我可以得到一些帮助我的委托/协议...

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



// prefrencesViewController.h

  @protocol GetPrefrencesViewControllerDelegate< NSObject> 
- (void)reloadViewFromSavedPrefrences;
@end

//代理和协议
@property(nonatomic,weak)id&GetPrefrencesViewControllerDelegate>代表;

// prefrencesViewController.m

  //代理和协议
@synthesize delegate;

// ..内按钮动作
[[self delegate] reloadViewFromSavedPrefrences];

// myViewController.h

  #importprefrencesViewController.h

@interface myViewController:UIViewController< UITabBarDelegate,GetGUIEncodedData,GetPrefrencesViewControllerDelegate> {

// prefrencesViewController设置
prefrencesViewController * pvc;

@property(强,非原子)prefrencesViewController * pvc;

// myViewontroller.h

  @synthesize pvc; 

- (void)viewDidLoad
{
// ..
[pvc setDelegate:self];
}

//委托和预处理..已保存按下重新加载视图。
- (void)reloadViewFromSavedPrefrences {

NSLog(@WORKED);

}

任何帮助将不胜感激

解决方案

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



PresentedViewController.h

  // import stuff 
@protocol PresentedViewControllerDelegate< NSObject>
- (void)methodThatSouldBeImplementedByOtherController; //你可以添加参数
@end

@interface PresentedViewController:UIViewController {
//实例变量
}
@property(nonatomic,assign( ARK的一周))id< PresentedViewControllerDelegate>委托
//公共方法这里

PresentedViewController.m

  @implementation PresentedViewController 
@synthesize delegate;

//方法实现这里

- (IBAction)buttonThatWillCallTheDelegate:(id)sender {

if([self.delegate responsesToSelector:@selector (methodThatSouldBeImplementedByOtherController)]){
[self.delegate methodThatSouldBeImplementedByOtherController];
}
}

ControllerThatWillPresent.h / p>

  @interface ControllerThatWillPresent:UIViewController< PresentedViewControllerDelegate> {
//实例变量
}

//某些方法可能是

ControllerThatWillPresen.m

  @implementation ControllerThatWillPresen 

- (void)methodThatWillShowTheVC {
PresentedViewController * vc = [PresentedViewController alloc] init]; // initWithNibname ...
vc.delegate = self;
// presentVc,pushVc,addChild ...
}

- (void)methodThatSouldBeImplementedByOtherController {
//在代理方法中执行东西
}


I have a UIViewController on a UINavigationStack and from this UIView I load another view not onto the stack but as a subview. This view that I load is just a preferences view for the app that I overlay onto what ever is showing.

i.e.

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

My question is, is there a way to call a method thats in myViewController from prefrencesViewController? I am trying to use delegates and protocols but its not working, so I am hoping there is either an easy way to do this I don't know about yet or maybe I could get some help with my delegate/protocol...

This is what my code looks like for delegate and protocol set up

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

}

any help would be greatly appreciated

解决方案

I'm not sure that you are following the steps that I will present below but if you don't here is the example.

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
}

这篇关于如何从已经在UINavigationController堆栈上的UIViewController调用一个方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆