Last active
November 19, 2015 12:02
-
-
Save geirsagberg/8ad7fe0667df1a7475e8 to your computer and use it in GitHub Desktop.
MvvmCross load views from Storyboards using a custom attribute
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class StoryBoardContainer : MvxTouchViewsContainer | |
{ | |
protected override IMvxTouchView CreateViewOfType(Type viewType, MvxViewModelRequest request) | |
{ | |
var storyboardAttribute = viewType.GetCustomAttribute<FromStoryboardAttribute>(); | |
if (storyboardAttribute != null) { | |
var storyboard = UIStoryboard.FromName(storyboardAttribute.StoryboardName ?? viewType.Name, null); | |
var viewController = storyboard.InstantiateViewController(viewType.Name); | |
return (IMvxTouchView) viewController; | |
} | |
return base.CreateViewOfType(viewType, request); | |
} | |
} | |
public class FromStoryboardAttribute : Attribute | |
{ | |
public string StoryboardName { get; set; } | |
public FromStoryboardAttribute(string storyboardName = null) | |
{ | |
StoryboardName = storyboardName; | |
} | |
} | |
// Will look for a UIViewController with identifier "MyView" inside a Storyboard named "MyView.storyboard" | |
[FromStoryboard] | |
public class MyView : MvxViewController | |
{ | |
public MyView(IntPtr handle) : base(handle) {} | |
} | |
// Will look for a UIVIewController with identifier "MyOtherViewInSameStoryboard" inside a Storyboard named "MyView.storyboard" | |
[FromStoryboard(StoryboardName = "MyView")] | |
public class MyOtherViewInSameStoryboard : MvxViewController | |
{ | |
public MyOtherViewInSameStoryboard(IntPtr handle) : base(handle) {} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment