Evaluating MonoTouch, trying to get autorotate working

classic Classic list List threaded Threaded
11 messages Options
Reply | Threaded
Open this post in threaded view
|

Evaluating MonoTouch, trying to get autorotate working

HairyJohn
Hi all,
Im trying out some basic functionality and I cant see what Im doing wrong.

Im looking for an example of a barebones MD solution that implements a custom view controller that returns true in ShouldAutorotateToInterfaceOrientation. Ive created an example, ShouldAutorotateToInterfaceOrientation gets called several times on start up, but didRotate is not called and the triangle I draw in my custom UIView as a test stays in portrait mode, as does the top status bar.

Im sure Im doing something wrong at a simple level, but can anyone point me at an example of how to get auto rotate to work? Ive searched high and low, thats how I found I need a customer view controller. I even created a test app with no UI so created my own static entrypoint and ui application delegate, created my own UIWindow to ensure that my custom controller was the top level.

Ideally I want to see in code how to do this, without needing to go through IB. But any simple working example would be great.

Much appreciated.

Thanks
John
Reply | Threaded
Open this post in threaded view
|

Re: Evaluating MonoTouch, trying to get autorotate working

eternalbard
I don't know if this is the correct way or if is what you need but, to
auto rotate, you just need this in each ViewController:

public override bool ShouldAutorotateToInterfaceOrientation
(UIInterfaceOrientation toInterfaceOrientation)
{
 return true;
}


And for some kind of control, you can use toInterfaceOrientation to see
what is happening and make decisions, where you can execute special things
like:

public override bool ShouldAutorotateToInterfaceOrientation
(UIInterfaceOrientation toInterfaceOrientation)
{
 if (toInterfaceOrientation == UIInterfaceOrientation.Portrait ||
toInterfaceOrientation == UIInterfaceOrientation.PortraitUpsideDown)
  return false;
 else
  return true;

}


The above code is forcing the page to stay in landcape. But here you can
check if is landscape or portrait and execute different things, or call a
method that make something special and pass as parameter the
toInterfaceOrientation.

Karl


On 29/04/11 08:06, "JohnnyNoMates" <[hidden email]> wrote:

>Hi all,
>Im trying out some basic functionality and I cant see what Im doing wrong.
>
>Im looking for an example of a barebones MD solution that implements a
>custom view controller that returns true in
>ShouldAutorotateToInterfaceOrientation. Ive created an example,
>ShouldAutorotateToInterfaceOrientation gets called several times on start
>up, but didRotate is not called and the triangle I draw in my custom
>UIView
>as a test stays in portrait mode, as does the top status bar.
>
>Im sure Im doing something wrong at a simple level, but can anyone point
>me
>at an example of how to get auto rotate to work? Ive searched high and
>low,
>thats how I found I need a customer view controller. I even created a test
>app with no UI so created my own static entrypoint and ui application
>delegate, created my own UIWindow to ensure that my custom controller was
>the top level.
>
>Ideally I want to see in code how to do this, without needing to go
>through
>IB. But any simple working example would be great.
>
>Much appreciated.
>
>Thanks
>John
>
>--
>View this message in context:
>http://monotouch.2284126.n4.nabble.com/Evaluating-MonoTouch-trying-to-get-
>autorotate-working-tp3483411p3483411.html
>Sent from the MonoTouch mailing list archive at Nabble.com.
>_______________________________________________
>MonoTouch mailing list
>[hidden email]
>http://lists.ximian.com/mailman/listinfo/monotouch




_______________________________________________
MonoTouch mailing list
[hidden email]
http://lists.ximian.com/mailman/listinfo/monotouch
Reply | Threaded
Open this post in threaded view
|

Re: Evaluating MonoTouch, trying to get autorotate working

HairyJohn
Hi Karl,
Thanks for the response, but Im already doing that, so I don't see what I could possibly be doing wrong.
Here is my Application class and my delegate descendent. I create an instance of my PrimaryViewController which returns true for ShouldAutorotateToInterfaceOrientation. The controller.View is a custom UIView which simply draws a triangle.  

        public class Application
        {
                static void Main (string[] args)
                {
                        UIApplication.Main (args, null, "AppController");
                }
        }
       
        [Register("AppController")]
        public class AppController : UIApplicationDelegate {
                UIWindow window;
 
                public override void FinishedLaunching (UIApplication app)
                {
                        window = new UIWindow (UIScreen.MainScreen.Bounds);
                        PrimaryViewController controller = new PrimaryViewController();
                        window.AddSubview(controller.View);
                        window.MakeKeyAndVisible ();
                }
        }
Reply | Threaded
Open this post in threaded view
|

Re: Evaluating MonoTouch, trying to get autorotate working

Robert Jordan
On 29.04.2011 17:34, JohnnyNoMates wrote:
> Hi Karl,
> Thanks for the response, but Im already doing that, so I don't see what I
> could possibly be doing wrong.

Did you add the necessary UISupportedInterfaceOrientations key
to Info.plist? You can do it either with the MD's project's setting
dialog or manually.

Robert

_______________________________________________
MonoTouch mailing list
[hidden email]
http://lists.ximian.com/mailman/listinfo/monotouch
Reply | Threaded
Open this post in threaded view
|

Re: Evaluating MonoTouch, trying to get autorotate working

eternalbard
In reply to this post by HairyJohn
Hi Johnny,

Ah, now I see that your trying without IB, sorry... Mine was almost-all
made with IB, to mimic Visual Studio forms.. As I'm porting a windows
mobile application.

I never tried yet without IB... Did you tried to create that with IB and
check the code that Mono creates with that?

I think that have to pass to experts hehe.

Karl


On 29/04/11 12:34, "JohnnyNoMates" <[hidden email]> wrote:

>Hi Karl,
>Thanks for the response, but Im already doing that, so I don't see what I
>could possibly be doing wrong.
>Here is my Application class and my delegate descendent. I create an
>instance of my PrimaryViewController which returns true for
>ShouldAutorotateToInterfaceOrientation. The controller.View is a custom
>UIView which simply draws a triangle.
>
>    public class Application
>    {
>        static void Main (string[] args)
>        {
>            UIApplication.Main (args, null, "AppController");
>        }
>    }
>    
>    [Register("AppController")]
>    public class AppController : UIApplicationDelegate {
>        UIWindow window;
>
>        public override void FinishedLaunching (UIApplication app)
>        {
>            window = new UIWindow (UIScreen.MainScreen.Bounds);
>            PrimaryViewController controller = new
>PrimaryViewController();
>            window.AddSubview(controller.View);
>            window.MakeKeyAndVisible ();
>        }
>    }
>
>
>--
>View this message in context:
>http://monotouch.2284126.n4.nabble.com/Evaluating-MonoTouch-trying-to-get-
>autorotate-working-tp3483411p3483992.html
>Sent from the MonoTouch mailing list archive at Nabble.com.
>_______________________________________________
>MonoTouch mailing list
>[hidden email]
>http://lists.ximian.com/mailman/listinfo/monotouch




_______________________________________________
MonoTouch mailing list
[hidden email]
http://lists.ximian.com/mailman/listinfo/monotouch
Reply | Threaded
Open this post in threaded view
|

Re: Evaluating MonoTouch, trying to get autorotate working

HairyJohn
In reply to this post by Robert Jordan
Good point Robert, just checked and yes Ive got the following:

        <key>UISupportedInterfaceOrientations</key>
        <array>
                <string>UIInterfaceOrientationPortrait</string>
                <string>UIInterfaceOrientationPortraitUpsideDown</string>
                <string>UIInterfaceOrientationLandscapeLeft</string>
                <string>UIInterfaceOrientationLandscapeRight</string>
        </array>

So Im still feeling rather clueless as to what Im doing wrong.

John
Reply | Threaded
Open this post in threaded view
|

Re: Evaluating MonoTouch, trying to get autorotate working

David Moles
In reply to this post by HairyJohn
My understanding from <a href="http://developer.apple.com/library/ios/#documentation/uikit/reference/UIViewController_Class/Reference/Reference.html#//apple_ref/doc/uid/TP40006926-CH3-SW57">Apple's docs is that reorientation doesn't necessarily happen by magic:

"If the autoresizing properties of your views are configured correctly, that may be all you have to do. However, the UIViewController class provides additional hooks for you to implement additional behaviors as needed."

Maybe you need to look into UIView.AutoresizesSubviews and UIView.AutoresizingMask?

On Fri, Apr 29, 2011 at 4:06 AM, JohnnyNoMates <[hidden email]> wrote:
Hi all,
Im trying out some basic functionality and I cant see what Im doing wrong.

Im looking for an example of a barebones MD solution that implements a
custom view controller that returns true in
ShouldAutorotateToInterfaceOrientation. Ive created an example,
ShouldAutorotateToInterfaceOrientation gets called several times on start
up, but didRotate is not called and the triangle I draw in my custom UIView
as a test stays in portrait mode, as does the top status bar.

Im sure Im doing something wrong at a simple level, but can anyone point me
at an example of how to get auto rotate to work? Ive searched high and low,
thats how I found I need a customer view controller. I even created a test
app with no UI so created my own static entrypoint and ui application
delegate, created my own UIWindow to ensure that my custom controller was
the top level.

Ideally I want to see in code how to do this, without needing to go through
IB. But any simple working example would be great.

Much appreciated.

Thanks
John

--
View this message in context: http://monotouch.2284126.n4.nabble.com/Evaluating-MonoTouch-trying-to-get-autorotate-working-tp3483411p3483411.html
Sent from the MonoTouch mailing list archive at Nabble.com.
_______________________________________________
MonoTouch mailing list
[hidden email]
http://lists.ximian.com/mailman/listinfo/monotouch



--
David Moles

_______________________________________________
MonoTouch mailing list
[hidden email]
http://lists.ximian.com/mailman/listinfo/monotouch
Reply | Threaded
Open this post in threaded view
|

Re: Evaluating MonoTouch, trying to get autorotate working

HairyJohn
Well this is strange.
If I had an entry into the plist, then delete it and save the plist, then compile and run, the first rotation in the simulator works, and then it is stuck in landscape mode and doesn't change again.

Running again it doesn't rotate at all, but if I do the same as above and add an entry to the plist, delete it, and save it, it happens again. I can reproduce it every time.

If someone has 5 minutes could you create a hello world app that supports rotating? Im pulling my hair out here.

My thanks
John
Reply | Threaded
Open this post in threaded view
|

Re: Evaluating MonoTouch, trying to get autorotate working

James Hart
In reply to this post by HairyJohn
I've run into a similar situation, and I think the easiest solution is to make sure you hang on to a reference to the view controller.

i.e., in your application delegate, instead of putting the controller in a local variable, move it into a private field:

public class AppController : UIApplicationDelegate {
                UIWindow window;
                PrimaryViewController controller;
 
                public override void FinishedLaunching (UIApplication app)
                {
                        window = new UIWindow (UIScreen.MainScreen.Bounds);
                        controller = new PrimaryViewController();
                        window.AddSubview(controller.View);
                        window.MakeKeyAndVisible ();
                }
        }

If you think about it, unless the View itself is hanging on to a reference to the ViewController (usually not a good idea), if you only reference it in a local variable it becomes unreachable at the end of your FinishedLaunching method, and so eligible for garbage collection. If it gets garbage collected, it can no longer observe orientation changes, and then call your ShouldAutorotateToInterfaceOrientation implementation to decide what to do.

This also explains your "the first rotation in the simulator works" result - sometimes, the GC doesn't run immediately. But then when it does, once it's taken the viewcontroller away, nobody's left to, well.. control the view :)


James

-----Original Message-----
From: [hidden email] [mailto:[hidden email]] On Behalf Of JohnnyNoMates
Sent: Friday, April 29, 2011 11:35 AM
To: [hidden email]
Subject: Re: [MonoTouch] Evaluating MonoTouch, trying to get autorotate working

Hi Karl,
Thanks for the response, but Im already doing that, so I don't see what I
could possibly be doing wrong.
Here is my Application class and my delegate descendent. I create an
instance of my PrimaryViewController which returns true for
ShouldAutorotateToInterfaceOrientation. The controller.View is a custom
UIView which simply draws a triangle.  

        public class Application
        {
                static void Main (string[] args)
                {
                        UIApplication.Main (args, null, "AppController");
                }
        }
       
        [Register("AppController")]
        public class AppController : UIApplicationDelegate {
                UIWindow window;
 
                public override void FinishedLaunching (UIApplication app)
                {
                        window = new UIWindow (UIScreen.MainScreen.Bounds);
                        PrimaryViewController controller = new PrimaryViewController();
                        window.AddSubview(controller.View);
                        window.MakeKeyAndVisible ();
                }
        }


--
View this message in context: http://monotouch.2284126.n4.nabble.com/Evaluating-MonoTouch-trying-to-get-autorotate-working-tp3483411p3483992.html
Sent from the MonoTouch mailing list archive at Nabble.com.
_______________________________________________
MonoTouch mailing list
[hidden email]
http://lists.ximian.com/mailman/listinfo/monotouch
_______________________________________________
MonoTouch mailing list
[hidden email]
http://lists.ximian.com/mailman/listinfo/monotouch
Reply | Threaded
Open this post in threaded view
|

Re: Evaluating MonoTouch, trying to get autorotate working

eternalbard
Good point.. Each view in my application I need to create a variable for
the entire class or problems appear... On windows we can do using (Form
form1 = new Form()) {} but not here...

Karl

On 29/04/11 16:50, "James Hart" <[hidden email]> wrote:

>I've run into a similar situation, and I think the easiest solution is to
>make sure you hang on to a reference to the view controller.
>
>i.e., in your application delegate, instead of putting the controller in
>a local variable, move it into a private field:
>
>public class AppController : UIApplicationDelegate {
>        UIWindow window;
>        PrimaryViewController controller;
>
>        public override void FinishedLaunching (UIApplication app)
>        {
>            window = new UIWindow (UIScreen.MainScreen.Bounds);
>            controller = new PrimaryViewController();
>            window.AddSubview(controller.View);
>            window.MakeKeyAndVisible ();
>        }
>    }
>
>If you think about it, unless the View itself is hanging on to a
>reference to the ViewController (usually not a good idea), if you only
>reference it in a local variable it becomes unreachable at the end of
>your FinishedLaunching method, and so eligible for garbage collection. If
>it gets garbage collected, it can no longer observe orientation changes,
>and then call your ShouldAutorotateToInterfaceOrientation implementation
>to decide what to do.
>
>This also explains your "the first rotation in the simulator works"
>result - sometimes, the GC doesn't run immediately. But then when it
>does, once it's taken the viewcontroller away, nobody's left to, well..
>control the view :)
>
>
>James
>
>-----Original Message-----
>From: [hidden email]
>[mailto:[hidden email]] On Behalf Of JohnnyNoMates
>Sent: Friday, April 29, 2011 11:35 AM
>To: [hidden email]
>Subject: Re: [MonoTouch] Evaluating MonoTouch, trying to get autorotate
>working
>
>Hi Karl,
>Thanks for the response, but Im already doing that, so I don't see what I
>could possibly be doing wrong.
>Here is my Application class and my delegate descendent. I create an
>instance of my PrimaryViewController which returns true for
>ShouldAutorotateToInterfaceOrientation. The controller.View is a custom
>UIView which simply draws a triangle.
>
>    public class Application
>    {
>        static void Main (string[] args)
>        {
>            UIApplication.Main (args, null, "AppController");
>        }
>    }
>    
>    [Register("AppController")]
>    public class AppController : UIApplicationDelegate {
>        UIWindow window;
>
>        public override void FinishedLaunching (UIApplication app)
>        {
>            window = new UIWindow (UIScreen.MainScreen.Bounds);
>            PrimaryViewController controller = new
>PrimaryViewController();
>            window.AddSubview(controller.View);
>            window.MakeKeyAndVisible ();
>        }
>    }
>
>
>--
>View this message in context:
>http://monotouch.2284126.n4.nabble.com/Evaluating-MonoTouch-trying-to-get-
>autorotate-working-tp3483411p3483992.html
>Sent from the MonoTouch mailing list archive at Nabble.com.
>_______________________________________________
>MonoTouch mailing list
>[hidden email]
>http://lists.ximian.com/mailman/listinfo/monotouch
>_______________________________________________
>MonoTouch mailing list
>[hidden email]
>http://lists.ximian.com/mailman/listinfo/monotouch




_______________________________________________
MonoTouch mailing list
[hidden email]
http://lists.ximian.com/mailman/listinfo/monotouch
Reply | Threaded
Open this post in threaded view
|

Re: Evaluating MonoTouch, trying to get autorotate working

HairyJohn
In reply to this post by James Hart
James, you were exactly right here!

Schoolboy error on my side, but holding onto a reference prevented the view controller from being GC'ed, so I am now cooking with gas!

Many thanks guys for the input and help you have given me, I can now continue with my evaluation unhindered. Well, until I make another schoolboy error that is.

Cheers
John

PS - I have to say that now I am up and running with MonoDevelop (still eval haven't bought yet) I am finding it much much easier to develop for iOS, Im loving the Dictionary<> support and some of the basic niceties that C# offers that are sadly lacking in what I call Objectionable C. Square brackets around almost everything feels just so 1980, doesn't it?