ken.ng
ken.ng

Reputation: 241

How to create a storyboard and link it to an existing UIViewController?

I'm quite new to the Xamarin.iOS platform.

I created an empty Xamarin.iOS project then create my main UIViewController, which is UIViewController1 and then set it up in the AppDelegate.cs:

            // If you have defined a root view controller, set it here:
            var view1 = new UIViewController1();
            Window.RootViewController = view1;

            // make the window visible
            Window.MakeKeyAndVisible();

In the UIViewController1 I hard-code all the labels and buttons and stuff. Like this:

  var frame = new CGRect(10, 10, 300, 30);
                var Label = new UILabel(frame);
View.AddSubView(Label);

But now when everything is confirmed to be working correctly, I want to create a storyboard for my UIViewController1 and move all of the elements onto it for future maintenance, so after creating an Empty Storyboard how can I link it to my current UIViewController1?

The other solutions that I found just don't work. All I got is a blank white screen.

Upvotes: 2

Views: 1981

Answers (1)

Ax1le
Ax1le

Reputation: 6643

Open your empty Storyboard, on the left window you will see the Toolbox(if you didn't see it, Ctrl + Alt + X to open it). Select a ViewController then put it on the Storyboard.

Here you will see a Storyboard with only one ViewController in it like:

enter image description here

Click the yellow button at left bottom of the ViewController. In the Property window we can choose its Class, click the down arrow to select:

enter image description here

In this way, we have bound the ViewController to the UIViewController1. But we also need to create a initial method in this class:

public UIViewController1 (IntPtr handle) : base(handle)
{
}

If you want to use the Storyboard as your initial Window, open your info.plist, select the Main Interface to your Storyboard. Then in the FinishedLaunching() remove all your code about initializing the Window:

//var view1 = new UIViewController1();
//Window.RootViewController = view1;
// make the window visible
//Window.MakeKeyAndVisible();

Moreover instead of constructing the ViewController with var view1 = new UIViewController1();, we should use UIStoryboard.FromName("StoryboardName", null).InstantiateViewController("ViewControllerID");. This ViewControllerID can be set in the property window I post above called Storyboard ID;

Upvotes: 3

Related Questions