簡體   English   中英

如何從已經在UINavigationController堆棧上的UIViewController調用方法

[英]How to call a method from a UIViewController thats already on the UINavigationController stack

我在UINavigationStack上有一個UIViewController,從這個UIView中,我將另一個視圖加載到堆棧中而不是子視圖中。 我加載的該視圖只是我覆蓋在所顯示內容上的應用程序的首選項視圖。

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

}

任何幫助將不勝感激

我不確定您是否按照下面將要介紹的步驟進行操作,但是如果您不在此處,則為示例。

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
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM