簡體   English   中英

如何獲取呈現自定義AlertView的UIViewController的名稱?

[英]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

有任何想法嗎?

編輯

  1. 選擇您的情節提要文件
  2. 選擇MasterViewController
  3. 實用程序中,選擇身份檢查器
  4. 確保選擇的類是MasterViewController
  5. 情節提要ID文本框中的“自定義類別”區域下方,為其指定一個自定義名稱
  6. 最后,勾選“使用情節提要ID”復選框√

    • 對SharedViewController執行相同的操作 (對於任何其他可能已呈現出alertviewcontroller的VC而言)

現在,在將自定義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.

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