[英]How to the get UIViewController which presented a UINavigationController
[英]How to get the name of the UIViewController that presented the custom AlertView?
我已經實現了自定義的alertview, delegate
方法如下:
#import <UIKit/UIKit.h>
@class MyAlertViewController;
@protocol MyAlertViewControllerDelegate <NSObject>
@required
-(void) alertViewControllerClickedPrimaryButton:(MyAlertViewController *)alertViewController;
@optional
-(void) alertViewControllerClickedSecondaryButton:(MyAlertViewController *)alertViewController;
@end
我在許多視圖控制器中調用了這些委托方法,並且還制作了一個SharedViewController
,用於所有ViewControllers
可以說,從MasterViewController
調用了其中一個委托方法,該方法繼承了SharedViewController
,我將在SharedViewController
使用以下方法:
#pragma mark My Alert Delegate
-(void) alertViewControllerClickedPrimaryButton:(MyAlertViewController *)alertViewController{
//here how would I know that this alert is presented from MasterViewController ?
}
如果我做[self class]
,我會得到名字SharedViewController
。
從MAsterVIewController
調用delegate
方法時,如何在SharedViewController
獲得MasterViewController
的名稱?
編輯:我向委托方法添加了一個名為UIViewController
參數,現在看起來像這樣:
-(void) alertViewControllerClickedSecondaryButton:(MyAlertViewController *)alertViewController :(UIViewController *)controllerName;
以及需要發送什么作為controllerName委托?
我有這行來調用委托方法:
[_delegate alertViewControllerClickedSecondaryButton:self ]; //previous call
我將其更改為:
[_delegate alertViewControllerClickedSecondaryButton:self :<#(UIViewController *)#>]; // I have no idea which parameter to pass in here
有任何想法嗎?
〜 編輯 〜
MasterViewController
最后,勾選“使用情節提要ID”復選框√
現在,在將自定義AlertVC提交到導航堆棧之后,在您的Alert Delegate方法中:
-(void) alertViewControllerClickedPrimaryButton:(MyAlertController *)alertViewController
{
/* ... all other code
...
...
*/
// Print the name of the Presenting View Controller
NSLog(@"Presenting ViewController = %@", self.presentingViewController.restorationIdentifier);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.