Forum Xamarin.iOS

Correct way to create a reusable ViewController

FlyshewoorstFlyshewoorst DEMember ✭✭
edited October 2016 in Xamarin.iOS


I wasted hours finding out what's going on here. I've created a View Controller using Xamarin Studio's wizard. This gave me a


Everything's fine until here. I need to dynamically load that created ViewController (!). I used the following lines:

var nib = UINib.FromName("ViewController", NSBundle.MainBundle); var objects = nib.Instantiate(this, null);

As well as two other possibilities to load that View Controller. However, loading View Controllers using any built in Nib-Loading-functionality always results in loading the corresponding View only, which eventually causes these annoying 'not code-compilant' errors when outlets are required. I know that these errors are usually caused by broken xib's, but I retried the instantiation of a simple custom view controller multiple times, it simply doesn't work, which goes even for default generated controllers; the View is loaded, without it's View Controller. But I need to instantiate both from a xib.

So, what is the correct way to load a ViewController (not just the View)?

Best Answer


  • I don't know if this is the most appropriate way to do it, but you can make an instance of any ViewController inside of another., this allow you to have both View and ViewController i.e:

        public partial class MyViewController 
            public override void ViewDidLoad()
                var myOtherVC = new OtherViewController
                //you neeed to give it a frame
                //then just add it to any view of this viewController
Sign In or Register to comment.