Quantcast
Channel: Recent Threads — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 204402

Restore focus after rotate

$
0
0

I have the following code sample, and things look good in the debugger, but it's not putting the focus back to the control that had it prior to the rotation, and the keyboard isn't displaying either. Has anyone done something similar? Any help here? Thanks.

using System;
using MonoTouch.Dialog;
using MonoTouch.UIKit;

namespace MTDRotate
{
    public class HomeScreen: DialogViewController
    {
        public HomeScreen(): base(null, true)
        {
            Title = "Rotate Focus";
            Root = CreateRoot();
        }

        public RootElement CreateRoot()
        {           
            RootElement rootElement = new RootElement("Details");
            Section section = new Section("Test");
            section.Add(new EntryElement("ID", "", ""));
            section.Add(new EntryElement("Name", "", ""));
            rootElement.Add(section);
            return rootElement;
        }

        private UIView focus = null;
        public override void WillRotate(MonoTouch.UIKit.UIInterfaceOrientation toInterfaceOrientation, double duration)
        {
            focus = this.View.FindFirstResponder();
            if (focus != null)
                focus.ResignFirstResponder();
            base.WillRotate(toInterfaceOrientation, duration);
        }

        public override void DidRotate(MonoTouch.UIKit.UIInterfaceOrientation fromInterfaceOrientation)
        {
            // If base.DidRotate is called first, BecomeFirstResponder() returns false, otherwise true.
            if (focus != null)
            {
                if (focus.CanBecomeFirstResponder)
                    focus.BecomeFirstResponder();
                focus = null;
            }
            base.DidRotate(fromInterfaceOrientation);
        }
    }

    public static class ViewExtensions
    {
        public static UIView FindFirstResponder(this UIView view)
        {
            if (view.IsFirstResponder)
            {
                return view;
            }
            foreach (UIView subView in view.Subviews)
            {
                var firstResponder = subView.FindFirstResponder();
                if (firstResponder != null)
                    return firstResponder;
            }
            return null;
        }
    }
}

Viewing all articles
Browse latest Browse all 204402

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>