简体   繁体   中英

How to get the name of the UIViewController that presented the custom AlertView?

I have implemented a custom alertview and the delegate methods are as follows:

#import <UIKit/UIKit.h>

@class MyAlertViewController;

@protocol MyAlertViewControllerDelegate <NSObject>

@required
-(void) alertViewControllerClickedPrimaryButton:(MyAlertViewController *)alertViewController;

@optional
-(void) alertViewControllerClickedSecondaryButton:(MyAlertViewController *)alertViewController;

@end

I have these delegate methods called in many view controllers.And Also I made one SharedViewController which I use it for all my ViewControllers

Lets say, one of these delegate method is called from MasterViewController which inherits SharedViewController ,I will have this method in the SharedViewController :

#pragma mark My Alert Delegate
-(void) alertViewControllerClickedPrimaryButton:(MyAlertViewController *)alertViewController{

     //here how would I know that this alert is presented from MasterViewController ?

}

If I do [self class] , I would get the name as SharedViewController .

How can I get the name for my MasterViewController in my SharedViewController ,when the delegate method is called from MAsterVIewController ?

Edit: I added one parameter called UIViewController to the delegate method,and now it looks like this:

 -(void) alertViewControllerClickedSecondaryButton:(MyAlertViewController *)alertViewController :(UIViewController *)controllerName;

and what needs to send to delegate as controllerName?

I have this line to call the delegate method:

[_delegate alertViewControllerClickedSecondaryButton:self ]; //previous call

and I changed this to:

[_delegate alertViewControllerClickedSecondaryButton:self :<#(UIViewController *)#>]; // I have no idea which parameter to pass in here

any ideas?

~ EDIT ~

  1. Select your storyboard file
  2. Choose MasterViewController
  3. In Utilities select Identity Inspector
  4. Make sure the Class selected is MasterViewController
  5. Just below the Custom Class area in the Storyboard ID textbox give it a custom name
  6. Lastly, Tick the 'Use Storyboard ID' box √

    • Do the same for SharedViewController (and for any other VC's you have potentially presenting your alertviewcontroller)

Now, in your Alert Delegate Method after your custom AlertVC has been presented onto the Navigation Stack:

-(void) alertViewControllerClickedPrimaryButton:(MyAlertController *)alertViewController 
{
/*  ... all other code
    ... 
    ... 
*/
    // Print the name of the Presenting View Controller
    NSLog(@"Presenting ViewController = %@", self.presentingViewController.restorationIdentifier);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM