Maulik shah
Maulik shah

Reputation: 1704

How can i push pop in swreveal view controller in ios?

I have an app like Facebook. I want to push and pop in swreveal view controller. How can achieve this? Check following image.

enter image description here

Any help will be appreciated.

Upvotes: 0

Views: 1757

Answers (4)

Marat Ibragimov
Marat Ibragimov

Reputation: 1084

You can use UINavigationController's method :

- (void)setViewControllers:(NSArray<UIViewController *> *)viewControllers animated:(BOOL)animated NS_AVAILABLE_IOS(3_0);

but please note that it sets whole Navigation controller stack. so you probably will have to take your current view controller state from the property

@property(nonatomic,copy) NSArray<__kindof UIViewController *> *viewControllers; 

allocate a new

NSMutableArray *controllers = [NSMutableArray arrayWithArray:self.navigationController viewControllers];

[controllers addObject:VC1];
[controllers addObject:VC2];
[controllers addObject:VC3];
[self.navigationController setViewControllers:controllers animated:YES];

Upvotes: 0

Patrick Bodet
Patrick Bodet

Reputation: 681

I would have a storyboard like this:

enter image description here

In LoginViewController:

    @IBAction func skip(_ sender: Any) {
    // Show dashboard

    let sb = UIStoryboard(name: "Main", bundle: nil)
    let controller = sb.instantiateViewController(withIdentifier: "DashboardNavigationController")

    revealViewController().setFront(controller, animated: true)
}

@IBAction func validateUser(_ sender: Any) {
    // show profile

    // Here test for valid user name and password
    // If OK
    let sb = UIStoryboard(name: "Main", bundle: nil)
    let dashboardNC = sb.instantiateViewController(withIdentifier: "DashboardNavigationController") as! UINavigationController

    let profileVC = sb.instantiateViewController(withIdentifier: "ProfileViewController")
    dashboardNC .pushViewController(profileVC, animated: false)

    revealViewController().setFront(dashboardNC, animated: true)
}

@IBAction func dismissKeyboard(_ textField: UITextField) {
    textField.resignFirstResponder()
}

In MenuTableViewController (SideMenu):

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    tableView.deselectRow(at: indexPath, animated: true)

    // ...
    let dashboardNC = revealViewController().frontViewController as! UINavigationController
    let nc = revealViewController().frontViewController as! UINavigationController
    let vc = nc.topViewController

    if !(vc is ProfileViewController) {
        // Instantiate controller from story board
        let sb = UIStoryboard(name: "Main", bundle: nil)
        let controller = sb.instantiateViewController(withIdentifier: "ProfileViewController")
        dashboardNC.pushViewController(controller, animated: false)
    }

    // Push it with SWRevealViewController
    revealViewController().pushFrontViewController(dashboardNC, animated: true)
}

DashboardViewController:

class DashboardViewController: UIViewController {

@IBOutlet weak var revealButton: UIBarButtonItem!
@IBOutlet weak var rightButton: UIBarButtonItem!

override func viewDidLoad() {
    super.viewDidLoad()

    revealButton.target = revealViewController()
    revealButton.action = #selector(SWRevealViewController.revealToggle(_:))

    rightButton.target = revealViewController()
    rightButton.action = #selector(SWRevealViewController.rightRevealToggle(_:))

    revealViewController().tapGestureRecognizer()
    revealViewController().panGestureRecognizer()

    revealViewController().rearViewRevealOverdraw = 0
    revealViewController().rightViewRevealOverdraw = 0
}

Not Objective-C but you can easily translate!

Upvotes: 5

Dishant Rajput
Dishant Rajput

Reputation: 1357

if you want to push from swreveal to any controller

UIStoryboard*Storyboard=[AppDelegate storyBoardType];
    yourController  *your=(yourController*)[Storyboard instantiateViewControllerWithIdentifier:@"yourControllerId"];

    UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController: your];

    [navController setViewControllers: @[your] animated: YES];
    [self.revealViewController setFrontViewController:navController];
    [self.revealViewController setFrontViewPosition: FrontViewPositionLeft animated: YES];

        }

from any controller to swreveal view controller you have to add this line in your action click

 [self.revealViewController revealToggleAnimated:YES];

Upvotes: 0

Zaid Pathan
Zaid Pathan

Reputation: 16820

In your app delegate you have to manager Login/Dashboard through rootViewController of window.

Step 1: In your AppDelegate write a method, call for example setupRootVC()

func setupRootVC(){
    if userIsLoggedIn{
       window.rootViewController = yourSWRevealControllerObj
    }else{
       window.rootViewController = yourLoginControllerObj
    }
}

Step 2: Call setupRootVC() method from didFinishLaunchingWithOptions method.

Step 3: In your LoginVC, after user logged in successfully call AppDelegate's setupRootVC() method.

Step 4: Make sure frontViewController of SWRevealController is inside UINavigationController so that your can push and pop through it.

I suggest you to read more about iOS app architecture and how rootViewController and UINavigationController works. Hope that helps.

Upvotes: 0

Related Questions