UITabBarController wait loading UIViewController until selected Xamarin - ios

Is it possible to call a method when a new UIViewController is selected in a UITabBarController ? Or is it possible to wait loading the whole UIViewController until the view is selecte?
I have a UITabBarController that looks like:
public sealed class VisualizationViewController : UITabBarController
{
private UIViewController tabVand, tabVarme, tabEl;
private UIToolbar toolbar;
public VisualizationViewController()
{
toolbar = new UIToolbar(new RectangleF(0, 0, (float)View.Frame.Width, 60));
toolbar.AutoresizingMask = UIViewAutoresizing.FlexibleWidth;
View.AddSubview(toolbar);
tabVand = new WaterVisualizationViewController();
tabVand.Title = "Vand";
tabVand.TabBarItem = new UITabBarItem("Vand", UIImage.FromFile("Images/Water.png"), 0); // Tag == initial order
tabVand.TabBarItem.SetFinishedImages( UIImage.FromFile("Images/Water.png"), UIImage.FromFile("Images/Water.png"));
tabVand.View.BackgroundColor = UIColor.LightGray;
tabVarme = new TemperatureViewController();
tabVarme.Title = "Varme";
tabVarme.TabBarItem = new UITabBarItem("Varme", UIImage.FromFile("Images/Heat.png"), 1); // Tag == initial order
tabVarme.TabBarItem.SetFinishedImages(UIImage.FromFile("Images/Heat.png"), UIImage.FromFile("Images/Heat.png"));
tabVarme.View.BackgroundColor = UIColor.LightGray;
tabEl = new ElVisualizationViewController();
tabEl.Title = "EL";
tabEl.TabBarItem = new UITabBarItem("EL", UIImage.FromFile("Images/Power.png"), 2); // Tag == initial order
tabEl.TabBarItem.SetFinishedImages(UIImage.FromFile("Images/Power.png"), UIImage.FromFile("Images/Power.png"));
tabEl.View.BackgroundColor = UIColor.LightGray;
var tabs = new[]
{
tabVand, tabVarme, tabEl
};
ViewControllers = tabs;
SelectedViewController = tabVand;
}
And right now, it loads all the Views, which is going to be an expensive call, when I move from dummy data to the real data each view should load. So I want to only make a data call when the view is selected in the UITabBarController.

So what's happening is that your ViewDidLoad() method is firing (assuming you're calling your webservice there) for each tab view controller in your constructor. Each View Did Load is firing because you're calling View.Background, which will load the view into memory. You don't want to do that here, but in the ViewDidLoad() method of each individual tab View Controller:
public sealed class VisualizationViewController : UITabBarController
{
private UIViewController tabVand, tabVarme, tabEl;
private UIToolbar toolbar;
public VisualizationViewController()
{
base.ViewDidLoad();
toolbar = new UIToolbar(new RectangleF(0, 0, (float)View.Frame.Width, 60));
toolbar.AutoresizingMask = UIViewAutoresizing.FlexibleWidth;
View.AddSubview(toolbar);
tabVand = new WaterVisualizationViewController();
tabVand.Title = "Vand";
tabVand.TabBarItem = new UITabBarItem("Vand", UIImage.FromFile("Images/Water.png"), 0); // Tag == initial order
tabVand.TabBarItem.SetFinishedImages( UIImage.FromFile("Images/Water.png"), UIImage.FromFile("Images/Water.png"));
//Don't do this, it will make its ViewDidLoad fire
//and you don't want that.
//tabVand.View.BackgroundColor = UIColor.LightGray;
tabVarme = new TemperatureViewController();
tabVarme.Title = "Varme";
tabVarme.TabBarItem = new UITabBarItem("Varme", UIImage.FromFile("Images/Heat.png"), 1); // Tag == initial order
tabVarme.TabBarItem.SetFinishedImages(UIImage.FromFile("Images/Heat.png"), UIImage.FromFile("Images/Heat.png"));
//tabVarme.View.BackgroundColor = UIColor.LightGray;
tabEl = new ElVisualizationViewController();
tabEl.Title = "EL";
tabEl.TabBarItem = new UITabBarItem("EL", UIImage.FromFile("Images/Power.png"), 2); // Tag == initial order
tabEl.TabBarItem.SetFinishedImages(UIImage.FromFile("Images/Power.png"), UIImage.FromFile("Images/Power.png"));
//tabEl.View.BackgroundColor = UIColor.LightGray;
var tabs = new[]
{
tabVand, tabVarme, tabEl
};
ViewControllers = tabs;
SelectedViewController = tabVand;
}
Then in each individual tab (UIViewController) implementation, call your web service in it's ViewDidLoad() method, and not in the constructor. Example:
public class WaterVisualizationViewController : UIViewController
{
public WaterVisualizationViewController()
{
//Don't call your web service here
}
public override ViewDidLoad()
{
//Set your background color here
//Call your web service
//Populate your UI with data
}
}
Now if your web service calls are expensive, you might see a little lag when tapping the tabs. For that you should consider using UIActivityIndicatorView to display a "Loading Data" type of indicator so that the user has feedback your app is doing something and not frozen. Alternatively, you can use BTProgressHUD from the Xamarin Component Store, which is really easy to use

Related

Adding a webview for each tab in UITabBarController?

How do I add a web view for each of these tabs in my UIViewController?
public class TabController: UITabBarController
{
UIViewController tab1, tab2, tab3, tab4, tab5, tab6, tab7;
public TabController()
{
tab1 = new UIViewController();
tab1.Title = "Test";
tab1.View.BackgroundColor = UIColor.Orange;
tab2 = new UIViewController();
tab2.Title = "Test2";
tab2.View.BackgroundColor = UIColor.Orange;
tab3 = new UIViewController();
tab3.Title = "Test3";
tab3.View.BackgroundColor = UIColor.Red;
tab4 = new UIViewController();
tab4.Title = "Test4";
tab4.View.BackgroundColor = UIColor.Red;
tab5 = new UIViewController();
tab5.Title = "Test5";
tab5.View.BackgroundColor = UIColor.Red;
tab6 = new UIViewController();
tab6.Title = "Test6";
tab6.View.BackgroundColor = UIColor.Red;
tab7 = new UIViewController();
tab7.Title = "Test7";
tab7.View.BackgroundColor = UIColor.Red;
var UIViewController = new UIViewController[] {
tab1, tab2, tab3, tab4, tab5, tab6, tab7
};
tab1.TabBarItem = new UITabBarItem(UITabBarSystemItem.Favorites, 0);
ViewControllers = UIViewController;
}
}
So for each tab, instead of "tab1.View.BackgroundColor = UIColor.Orange;", it
should be:
Webview + Title + Link so that each tab has a different page?
The easiest way would be to do this using the storyboard and assigning the viewcontroller classes to your tabs. However if you want to do it programatically I suppose you would need to do the following:
tab7 = new UIViewController();
tab7.Title = "Test7";
tab7.View.BackgroundColor = UIColor.Red;
//Create a webview
UIWebView webView = new UIWebView(View.Bounds); //Takes size as a constructor parameter
//Set it's navigation location
webView.Navigate(new URI("www.whatever.co.uk"));
//Add the webview to the subviews of your UIViewController
tab7.View.AddSubview(webView);
You'd do this for each tab.
On a side note it's worth mentioning that apples guidelines for application submissions to their store does tend to frown upon applications which are just web site representations, so worth bearing that in mind. The application must be able to do more than apples web browser could.

DialogViewController not showing navigationbar

I am instantiating a DialogViewController within a MvvmCross View like this
var myDialog = new MyDialogViewController();
var navController = new UINavigationController();
controller.NavigationBar.TintColor = UIColor.Black;
controller.PushViewController(myDialog, false);
Add(controller.View);
And this is how the MyDialogViewController looks like:
public class MyDialogViewController : DialogViewController
{
public MyDialogViewController()
: base(UITableViewStyle.Grouped, new RootElement("MyRoot"), true)
{
var root = new RootElement("MyRoot") {
new Section
{
new HtmlElement("MyWebsite", https://www.mywebsite.com")
}
};
Root.Add(root);
}
}
The dialog appears fine with the NavigationBar but if I select the Html element MyWebsite the Webview is displayed but without the NavigationBar and I am not able to navigate back.
The same thing occurs for elements that require to navigate to a new window, the navigationbar is not shown.
Any idea how to make the NavigationBar show after navigating to the WebView?
This worked for me:
var settings = new SettingsDialogViewController((SettingsViewModel)ViewModel, new RootElement("Settings"));
var window = UIApplication.SharedApplication.KeyWindow;
navigationController = window.RootViewController.ChildViewControllers[1].NavigationController;
navigationController.PushViewController(settings, true);
navigationController.NavigationBarHidden = false;

how to use flyout navigation in xamarin IOS

I'm new to Xamarin IOS and I have a project which needs this flyout navigation.
var navigation = new FlyoutNavigationController {
// Create the navigation menu
NavigationRoot = new RootElement ("Navigation") {
new Section ("Menu") {
new StringElement ("Recipe"),
new StringElement ("Recipe Basket"),
new StringElement ("Meal Planner"),
new StringElement ("Grocery List"),
new StringElement ("Social"),
new StringElement ("Videos"),
new StringElement ("News Feed"),
new StringElement ("Partners"),
}
},
// Supply view controllers corresponding to menu items:
ViewControllers = new [] {
new UIViewController { View = new UILabel { Text = "Animals (drag right)" } },
new UIViewController { View = new UILabel { Text = "Vegetables (drag right)" } },
new UIViewController { View = new UILabel { Text = "Minerals (drag right)" } },
},
};
This code just presents the UILabel. How can I do this to send it to a specific view controller?
Please help.
Thanks in advance.
Do what vinaykumar said, but you also want to open the FlyoutNavigationController from those UIViewControllers, so you have to pass that initial one you created to those controllers.
Otherwise if you just open your view controller, you won't be able to get the menu again.
I'm opening navigation controllers like so:
MainViewController in ViewDidLoad:
UIStoryboard board = UIStoryboard.FromName("MainStoryboard_iPhone", null);
UINavigationController nav = (UINavigationController)board.InstantiateViewController("nav");
SearchViewController searchVC = (SearchViewController)nav.ViewControllers[0];
// Passing flyoutnavigation here
searchVC.navigation = navigation;
SettingsViewController settingsVC = (SettingsViewController)board.InstantiateViewController("settings");
UINavigationController navSettings = new UINavigationController(settingsVC);
// Passing flyoutnavigation here
settingsVC.navigation = navigation;
navigation.ViewControllers = new[]
{
nav,
navSettings
};
In the other view controllers, (Settings and Search in my example), I have a public variable to store the FlyoutNavigationController (set in MainViewController) then add a button to open the FlyoutNavigationController that was passed:
// Set in MainViewController
public FlyoutNavigationController navigation;
public override void ViewDidLoad()
{
base.ViewDidLoad();
// Add button to allow opening the FlyoutNavigationController
NavigationItem.LeftBarButtonItem = new UIBarButtonItem(UIBarButtonSystemItem.Action, delegate
{
navigation.ToggleMenu();
});
}
you have to add the FlyoutNavigationController's View to your View
// Show the navigation view
navigation.ToggleMenu ();
View.AddSubview (navigation.View);
This code just presents the UILabel.
because you are only providing UILable as a view to your new UIViewController.
View = new UILabel { Text = "Animals (drag right)" }
How can I do this to send it to a specific view controller?
Provide your UIViewController instead of new UIViewController
for instance change the following code
ViewControllers = new [] {
new UIViewController { View = new UILabel { Text = "Animals (drag right)" } },
new UIViewController { View = new UILabel { Text = "Vegetables (drag right)" } },
new UIViewController { View = new UILabel { Text = "Minerals (drag right)" } },
},
to
ViewControllers = new [] {
new <your_UIViewController>(),
new <your_UIViewController>(),
new <your_UIViewController>(),
},
Hope this helps you, let me know if you faced any issue while implementing the same.

MVVMCross iOS: how to bind a command with Custom transition when switching to different View model

For MVVMCross ios, how can I use different TransitionalStyle such as FlipHorizontal style instead of the default sliding effect with "ShowViewModel"?
[Register("SearchResults")]
public class SearchResultsView : MvxTableViewController
{
public override void ViewDidLoad()
{
Title = "List";
base.ViewDidLoad();
var mapButton = new UIButton(new RectangleF(0, 0, 65, 30));
mapButton.SetBackgroundImage(UIImage.FromBundle("images/map_btn.png"), UIControlState.Normal);
mapButton.TouchUpInside += MapButtonClicked();
var rightButton = new UIBarButtonItem(mapButton);
NavigationItem.RightBarButtonItem = rightButton;
var bindings = this.CreateBindingSet<SearchResultsView, SearchResultsViewModel>();
//bindings.Bind(mapButton).To(x => x.ShowMapCommand); //how to do with binding command?
bindings.Apply();
}
private EventHandler MapButtonClicked()
{
return (sender, args) =>
{
var mapView = new SearchResultMapView {ModalTransitionStyle = UIModalTransitionStyle.FlipHorizontal};
var navigationController = new UINavigationController(mapView);
PresentViewController(navigationController, true, null);
};
}
}
ViewModels/Views are presented using a Presenter.
For Modal displays of navigation controllers, some people use the MvxModalNavSupportTouchViewPresenter.cs
You can use this presenter by overriding CreatePresenter in Setup:
protected override IMvxTouchViewPresenter CreatePresenter()
{
return new MvxModalNavSupportTouchViewPresenter(ApplicationDelegate, Window);
}
With this done then you should be able to achieve your transition effect by adding IMvxModalTouchView inheritance and by setting ModalTransitionStyle = UIModalTransitionStyle.FlipHorizontal; in the constructor for your SearchResultMapView.
public class SearchResultMapView
: MvxViewController, IMvxModalTouchView
{
public SearchResultMapView()
{
ModalTransitionStyle = UIModalTransitionStyle.FlipHorizontal;
}
// more code here....
}
Alternatively, if you wanted to completely customise the presentation of the view/viewcontroller, then implementing a custom presenter is fairly straight-forward to do - for more information, see some of the articles and tutorials on custom presenters in http://slodge.blogspot.co.uk/2013/06/presenter-roundup.html
#wing
All you need to do is add a close button to the new view of the pushed view controller and override it's touchupinside as follows:
private void CloseBtnTouchUpInside(object sender, EventArgs eventArgs)
{
DismissModalViewController(false);
}

MonoTouch for IPad: How to show another UIViewController in a UIPopoverController?

As title said, I want to show another UIViewController from an existing UIViewController which is hosted in UIPopoverController. I tried the following method:
_secondViewController = new SecondViewController();
this.ModalPresentationStyle = UIModelPresentationStyle.CurrentContext;
this.ModelInPopover = true;
this.PresentModelViewController(_secondViewController, true);
However, the secondViewController is shown in the main view controller, instead of the popover controller.
In this post somebody mentions that it cannot be done and it violates the HIG. However, I have seen this in other apps (e.g. Yahoo! Email) if I'm not mistaken.
I'm also thinking about another approach: If I could create a UINavigationController within the popover context, it might work by just adding new ViewController to the NavigationController. But how?
Remember that UINavigationController derives from UIViewController.
So, you can use the controller contained within UIPopover just like any other container... in this case it's best to use UINavigationController inside UIPopover to display ViewControllers.
Usage:
var _NavController = new NavController();
Popover = new UIPopoverController(_NavController);
Popover.PopoverContentSize = new SizeF(..., ...);
Popover.PresentFromRect(...);
NavController:
public class NavController : UINavigationController
{
UIViewController _FirstViewController;
UIViewController _SecondViewController;
public NavController()
: base()
{
}
public override void LoadView()
{
base.LoadView();
_FirstViewController = new UIViewController();
// Initialize your originating View Controller here.
// Only view related init goes here, do everything else in ViewDidLoad()
}
public override void ViewDidLoad()
{
base.ViewDidLoad();
// When a button inside the first ViewController is clicked
// The Second ViewController is shown in the stack.
_FirstViewController.NavButton.TouchUpInside += delegate {
PushSecondViewController();
};
this.PushViewController(_FirstViewController, true);
}
public void PushSecondViewController()
{
_SecondViewController = new UIViewController();
this.PushViewController(_SecondViewController, true);
}
}

Resources