0

I'm having trouble identifying the destinationViewController using the SWRevealViewController

On certain ViewControllers I have used the prepareForSegue method to identify the destination as follows:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    if ([[segue identifier] isEqualToString:@"showHint"]) {
        HintsViewController *hintsVC = [segue destinationViewController];
        hintsVC.hintDelegate = self.controller;
        hintsVC.hintUsed = self.controller.hintUsed;
        hintsVC.firstLetterUsed = self.controller.firstLetterUsed;
        hintsVC.answersUsed = self.controller.answersUsed;
    }

These viewControllers are directly connected via Segues on the storyboard. And using this method I can identify the targetViewcontroller and set it as a delegate.

However I have a BarButtonItem in my FirstViewController that is connected to the MenuViewController as follows in the viewDidLoad method

   SWRevealViewController *revealViewController = self.revealViewController;
    if(revealViewController){
        [self.sideBarButton setTarget:self.revealViewController];
        [self.sideBarButton setAction:@selector(rightRevealToggle:)];

        revealViewController.rightViewRevealWidth = self.view.frame.size.width * 0.65;
    }

This shows the MenuViewController and it is on the MenuViewController that I want to set as delegate.

So ideally I should be able to do something like:

MenuViewController* menuVC = revealViewController.destinationViewController;
menuVC.delegate = self;

However I can't identify the MenuViewControler using the viewDidLoad Method and I'm unsure of how to refactor the sidebarbutton to use a named storyboard segue.

Linda Keating
  • 2,215
  • 7
  • 31
  • 63

2 Answers2

1

SWRevealViewController have a rear view controller and frontViewController. So you want to do this?

MenuViewController* menuVC = (MenuViewController*)revealViewController.rearViewController; menuVC.delegate = self;

tbilopavlovic
  • 1,096
  • 7
  • 13
  • Thanks. That was exactly what I was looking for. – Linda Keating Sep 10 '15 at 13:14
  • I still have a problem though - As I'm assigning the delegate in the viewDidLoad method when the MenuVC hasn't been instantiated so the two viewControllers don't have a reference to each other so that delegate methods don't fire. Not sure how to overcome it. Normally I would use prepareForSegue but I can't see a way to set up Segues for this particular case with SWReveal – Linda Keating Sep 10 '15 at 13:27
  • You can subclass reveal controller and in viewDidLoad method set rear and front view controller... Why do you need delegate from menu in your frontViewController? You can communicate through reveal controller.. – tbilopavlovic Sep 10 '15 at 13:35
  • One of the options on Menu is to Share the screen - so when the share is selected I need the Menu to be dismissed and a screengrab of the presentingviewcontroller to be made which I can then share. If I connect the shareButton to the presentingViewController via segue it will reload the presentingViewController but the content of the interface will be different. – Linda Keating Sep 10 '15 at 13:44
  • 1
    Then you can in MenuController on "share" method grab the revealViewController.frontViewController.view and dismiss menu by calling revealViewController toogle method... – tbilopavlovic Sep 10 '15 at 13:55
  • Oh that's a brilliant solution. Thanks. – Linda Keating Sep 10 '15 at 14:05
1
if self.revealViewController() != nil {
                menuButton.target = self.revealViewController()
                menuButton.action = #selector(SWRevealViewController.revealToggle(_:))
                self.revealViewController().delegate = self
self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
                self.revealViewController().rearViewRevealWidth = 
            }

Below are are delegates test in swift4:

func revealController(_ revealController: SWRevealViewController!, willMoveTo position: FrontViewPosition) {    }
func revealControllerPanGestureBegan(_ revealController:SWRevealViewController ){}
func revealController(_ revealController: SWRevealViewController, didMoveTo position: FrontViewPosition) {}
kalyan711987
  • 476
  • 9
  • 14