Skip to content

Instantly share code, notes, and snippets.

@scottmcarthur
Created April 4, 2014 19:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scottmcarthur/9981470 to your computer and use it in GitHub Desktop.
Save scottmcarthur/9981470 to your computer and use it in GitHub Desktop.
UINavigationController Null in UIScrollView ScrollAnimationEnded event after ResignFirstResponder
using MonoTouch.Foundation;
using MonoTouch.UIKit;
using System.Drawing;
namespace PopTestApp
{
[Register("AppDelegate")]
public class AppDelegate : UIApplicationDelegate
{
UIWindow window;
UINavigationController navigationController;
public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions)
{
// Navigation controller
navigationController = new UINavigationController(new FirstController());
// Window
window = new UIWindow(UIScreen.MainScreen.Bounds);
window.RootViewController = navigationController;
window.MakeKeyAndVisible();
return true;
}
}
public class FirstController : UIViewController
{
public FirstController() {
Title = "First";
View.BackgroundColor = UIColor.Red;
}
public override void TouchesBegan(NSSet touches, UIEvent evt)
{
// Push the next controller
NavigationController.PushViewController(new SecondController(), false);
}
}
public class SecondController : UIViewController
{
readonly UIScrollView ScrollView;
readonly UITextField Field;
readonly UIButton Button;
public SecondController()
{
Title = "Second";
View.BackgroundColor = UIColor.Blue;
// Field will scroll and fail to pop the view when resigned
Field = new UITextField(new RectangleF(15,50,260,40)) { BackgroundColor = UIColor.White };
Field.ShouldReturn += (field) => {
ScrollView.SetContentOffset(new PointF(0, 200), true);
return true;
};
// Button will scroll and pop the view successfully
Button = new UIButton(new RectangleF(15,100,260,40)) { };
Button.SetTitle("Working Scroll & Pop", UIControlState.Normal);
Button.TouchUpInside += (sender, e) => ScrollView.SetContentOffset(new PointF(0, 200), true);
ScrollView = new UIScrollView { Frame = new RectangleF(new PointF(0, 0), View.Frame.Size), ContentSize = new SizeF(View.Frame.Width, View.Frame.Height + 1000) };
ScrollView.ScrollAnimationEnded += (sender, e) =>
// Pop to the root view controller
// This action works after the scroll trigger by the button
// This action fails (NavigationController is null) when resigning the UITextField - Why?
NavigationController.PopToRootViewController(true);
Add(ScrollView);
ScrollView.Add(Field);
ScrollView.Add(Button);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment