Forum Xamarin.iOS

Moving from one viewcontroller to another viewcontroller.

PreranaPrerana INMember ✭✭

Hi all,

I am a complete newbie in Mac development.I want to achieve the following functionality:-

I have a view with a two textfields and a login button.When the user clicks on login button another view should open.Now my main view is in MainWindow.xib file..
I added another view : Add-> New File-> Xamarin.Mac-> Cocoa View with controller, which generated the following files:-

1.) NewView.cs

2.) NewView.xib

3.) NewViewController.cs

After searching quite alot on net i didnt get the exact way to achieve this functionality. I referred this thread on forum http://forums.xamarin.com/discussion/6042/use-custom-viewcontrollers-in-my-interface#latest but could not get the exact functionality of the sample project attached in the thread. Why is custom view exactly used???....Can someone tell me how to simply navigate from one view controller to another and where what code is to be written??
Thanks a ton in advance!

Posts

  • PreranaPrerana INMember ✭✭

    Any code snippet,link or sample code would be of great help!!...Thank in advance.

  • SteveFlackSteveFlack GBMember ✭✭
    edited November 2013

    If you're going to be swapping the window contents a lot I would probably put the view switching code in the window controller.

    You basically have a container view in the window and as you want to swap out the views you add/remove them as subviews of this view. The view controller for the new view is kept as a reference by the window controller. As you swap the subviews of the container view, you resize the incoming view by setting its frame to the container view's bounds - you would also need to set the resize mask appropriately. Alternatively you could add auto layout constraints to handle the resizing.

  • NikolayIvanetsNikolayIvanets USMember ✭✭

    @Prerana, here is sample how to swap views in window: https://github.com/mono/monomac/tree/master/samples/Preferences

  • PreranaPrerana INMember ✭✭

    Hey thanks @Steve,

    Got it working. Code snippet


    this.View.Superview.AddSubview(new SecondViewController().View);

    this.View.RemoveFromSuperview();


    The above code has to be written in the ViewController of your first view.

    Study of using the Auto-Layout in progress...:)

    Thanks again..:)

  • PreranaPrerana INMember ✭✭

    thank you @Nikolay..:)

Sign In or Register to comment.