Navigating to a view controller after selecting a TKSideDrawerItem

4 posts, 0 answers
  1. dotStaff
    dotStaff avatar
    3 posts
    Member since:
    Jun 2015

    Posted 22 Sep 2015 Link to this post

    I'm trying to show a view controller after selecting a TKSideDrawerItem in the Drawer. I think this post got me close http://www.telerik.com/forums/side-drawer-push-navigation-controller but I'm using C# and don't quite understand the syntax.

     In the DidSelectItem method of the SideDrawerDelegate how do I show the new view controller ?

  2. dotStaff
    dotStaff avatar
    3 posts
    Member since:
    Jun 2015

    Posted 23 Sep 2015 Link to this post

     

     

     

    // The UIApplicationDelegate for the application. This class is responsible for launching the
        // User Interface of the application, as well as listening (and optionally responding) to
        // application events from iOS.
        [Register("AppDelegate")]
        public partial class AppDelegate : UIApplicationDelegate
        {
            // class-level declarations
            UIWindow window;
     
            public static TKSideDrawerController sideDrawerController;
     
            //
            // This method is invoked when the application has loaded and is ready to run. In this
            // method you should instantiate the window, load the UI into it and then make the window
            // visible.
            //
            // You have 17 seconds to return from this method, or iOS will terminate your application.
            //
            public override bool FinishedLaunching(UIApplication app, NSDictionary options)
            {
                //Create a new instance of the menu
                UINavigationController navigationController = new UINavigationController(new Menu());
     
                //Create a new inistance of the side drawer controller
                AppDelegate.sideDrawerController = new TKSideDrawerController(navigationController);
                 
                //Set up the nav bar
                UINavigationBar navBar = new UINavigationBar(new CGRect(0, 0, UIScreen.MainScreen.Bounds.Width, 64));
                UINavigationItem navItem = new UINavigationItem("Getting Started");
                UIBarButtonItem showSideDrawer = new UIBarButtonItem(new UIImage("menu.png"), UIBarButtonItemStyle.Plain, this, new Selector("ShowSideDrawer"));
                navItem.LeftBarButtonItem = showSideDrawer;
                navBar.Items = new UINavigationItem[] { navItem };
     
                //Add the nav bar to the side drawer
                AppDelegate.sideDrawerController.View.AddSubview(navBar);
     
                // create a new window instance based on the screen size
                window = new UIWindow(UIScreen.MainScreen.Bounds);
     
                //Create an instance of the storyboard
                UIStoryboard Storyboard = UIStoryboard.FromName("MainStoryboard_iPhone", null);
     
                //Set the initial view controller
                 UIViewController initialViewController = Storyboard.InstantiateInitialViewController() as UIViewController;
     
                // If you have defined a view, add it here:
                window.RootViewController = initialViewController;
     
                // make the window visible
                window.MakeKeyAndVisible();
     
                return true;
            }
     
            [Export("ShowSideDrawer")]
            public void ShowSideDrawer()
            {
                //Show the side drawer
                sideDrawerController.SideDrawer.Show();
            }
        }     

     

    Menu Class:

     

    public partial class Menu : UIViewController, ITKSideDrawerDelegate
        {
            static bool UserInterfaceIdiomIsPhone
            {
                get { return UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Phone; }
            }
     
            public Menu()
                : base(UserInterfaceIdiomIsPhone ? "Menu_iPhone" : "Menu_iPad", null)
            {
            }
     
            public override void DidReceiveMemoryWarning()
            {
                // Releases the view if it doesn't have a superview.
                base.DidReceiveMemoryWarning();
     
                // Release any cached data, images, etc that aren't in use.
            }
     
            public override void ViewDidLoad()
            {
                base.ViewDidLoad();
     
                //Set the delegate for the SideDrawer
                AppDelegate.sideDrawerController.SideDrawer.Delegate = new SideDrawerDelegate(this);
     
                //Create primary section
                TKSideDrawerSection sectionPrimary = AppDelegate.sideDrawerController.SideDrawer.AddSection("Primary");
                sectionPrimary.AddItem("Social");
                sectionPrimary.AddItem("Promotions");
     
                //Create secondary section
                TKSideDrawerSection sectionLabels = AppDelegate.sideDrawerController.SideDrawer.AddSection("Labels");
                sectionLabels.AddItem("Important");
                sectionLabels.AddItem("Starred");
                sectionLabels.AddItem("Sent Mail");
                sectionLabels.AddItem("Drafts");
            }
     
            class SideDrawerDelegate : TKSideDrawerDelegate
            {
                Menu owner;
     
                //Set the instaance of the menu
                public SideDrawerDelegate(Menu owner)
                {
                    this.owner = owner;
                }
     
                //Fires when a menu item is clicked
                public override void DidSelectItem(TKSideDrawer sideDrawer, NSIndexPath indexPath)
                {
                    string meow = sideDrawer.Title;
     
                    //Create story board instance
                    UIStoryboard Storyboard = UIStoryboard.FromName("MainStoryboard_iPhone", null);
     
                    //Set the initial view controller
                    UIViewController initialViewController = Storyboard.InstantiateViewController("Home");
     
                    //Show the new ViewController
                    owner.NavigationController.ShowViewController(initialViewController, null);
     
                }
            }
        }

     

    In the LoginViewController after the user is authenticated:

    //Create a new window object
                                    UIWindow window = new UIWindow(UIScreen.MainScreen.Bounds);
                                     
                                    //Set the SideDrawerController as the root view controller and show it
                                    window.RootViewController = AppDelegate.sideDrawerController;
                                    window.MakeKeyAndVisible();

     

  3. DevCraft banner
  4. dotStaff
    dotStaff avatar
    3 posts
    Member since:
    Jun 2015

    Posted 23 Sep 2015 Link to this post

    The post above is how I got it working for anyone who needs its.
  5. Yoanna
    Admin
    Yoanna avatar
    44 posts

    Posted 24 Sep 2015 Link to this post

    Hello, dotStaff, 

    thank you for sharing your approach.

    It is great that you have successfully used TKSideDrawer in your project. 
    A new help article that covers the topic will be uploaded in our upcoming release.
    The below C# code shows another example of using TKSideDrawer with UINavigationController:

            public override bool FinishedLaunching (UIApplication application, NSDictionary launchOptions)
            {
                MyViewController vc = new MyViewController();
                UINavigationController navController = new UINavigationController (vc);
     
                TKSideDrawerController sideDrawerController = new TKSideDrawerController (navController);
                this.Window = new UIWindow (UIScreen.MainScreen.Bounds);
                this.Window.RootViewController = sideDrawerController;
                return true;
    }

    In MyViewController class:

    public override void ViewDidLoad ()
            {
                base.ViewDidLoad ();         
                this.SideDrawer = TKSideDrawer.FindSideDrawer(this);
                this.SideDrawer.Delegate = this;
                UIBarButtonItem showSideDrawerButton = new UIBarButtonItem ("Show", UIBarButtonItemStyle.Done, this, new Selector("ShowSideDrawer"));
                this.NavigationItem.RightBarButtonItem = showSideDrawerButton;
             
                TKSideDrawerSection section = this.SideDrawer.AddSection ("Section");
                section.AddItem ("Social");
                section.AddItem ("Promotions");
            }
     
            [Export("ShowSideDrawer")]
            public void ShowSideDrawer() {
                this.SideDrawer.Show ();
            }
     
            [Foundation.Export ("sideDrawer:didSelectItemAtIndexPath:")]
            public void DidSelectItem (TelerikUI.TKSideDrawer sideDrawer, Foundation.NSIndexPath indexPath)
            {
                ViewController vc = new ViewController ();
                vc.View.BackgroundColor = UIColor.Red;
                this.NavigationController.PushViewController (vc, true);
            }

    If you have any questions do not hesitate to contact us.
    Regards,
    Yoanna
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Back to Top
DevCraft banner