Change from Portrait to Landscape on button click

Discussion in 'iOS Programming' started by mngoose, Jun 4, 2012.

  1. mngoose macrumors member

    Joined:
    May 26, 2012
    #1
    ALl,

    I am seriously stuck.

    I have to ViewControllers and the first one is in portrait and the second is designed in landscape mode. I can't get the second one in the emulator to change from portait to landscape. I am using xcode 4 and have checked the box on the right side to show layout in landscape but the emulator won't show it.

    This would be after a button click to send information back to my home server and will eventually be populating the screen with variables that are parsed out from the other thread. I have beaten my head against the wall trying different things found on the web but nothing seems to be working at all.

    Thanks
    john
     
  2. jnoxx macrumors 65816

    jnoxx

    Joined:
    Dec 29, 2010
    Location:
    Aartselaar // Antwerp // Belgium
    #2
    err, did you actually turn the emulator to landscape? (Command +arrow to the right/left)

    why would you want to change your screen with a button click (portrait/landscape), why don't you just really turn the emulator/ipad itself.. (there is code for this build in).
     
  3. mngoose thread starter macrumors member

    Joined:
    May 26, 2012
    #3
    lol yes i did .. although that took me a while to figure out how to do that... :)

    I have two views .. one is portrait and one is in landscape because of the specific layout that I need after it hits the server that I point it to and gets the information that its going to display.

    I have tried every variation of code that I can find or cipher out myself and the emulator is acting like its locked in portrait mode. the plist file supports all layouts includeing upside down and left and right landscape, the code does the same, but I just can't get the emulator to show me the second layout in landscape mode.
    I have rebuilt the app 4 times (which is a huge PITA) and nothing I do seems to work. I am sure that I have something simple stupid going on here but for the life of me I can't see the forrest for the trees.

    I have tried this code
    Code:
    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    {
            return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft);
        
    }
    
    And this
    Code:
     (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)Orientation
    
    {
        if ((Orientation != UIInterfaceOrientationPortrait) ||
            (Orientation != UIInterfaceOrientationLandscapeLeft) ||
            (Orientation != UIInterfaceOrientationLandscapeRight))
            
            return YES;
        return NO;
    }
    
    Thanks in advance for any help

    john
     
  4. ppilone macrumors 6502

    Joined:
    Jan 20, 2008
    #4
    How are you transitioning to the second view controller? What container are these view controllers being presented in (e.g. UINavigationController, UITabBarController, none, both)?
     
  5. idelovski macrumors regular

    Joined:
    Sep 11, 2008
    #5
    That second method looks like something that would always return YES, and maybe you can try to force it with something like this:

    Code:
    [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait
                                                      animated:NO];
     
  6. mngoose thread starter macrumors member

    Joined:
    May 26, 2012
    #6
    First of all thanks for the suggestions and the help I appreciate it ---

    --ppilone... I have a single xib file with two views (this is one solution that I have tried and its not working out well).

    But I also have two views in a storyboard with two UIViewcontrollers on another version of the application that I am doing and I can switch between the two with a modal push button with touchup inside connections to change views back and forth... one being set in portrait and one being in landscape.



    Idelovski - thanks I'll try that ..

    John
     
  7. mngoose thread starter macrumors member

    Joined:
    May 26, 2012
    #7
    Guys,

    I am stuck.

    I can't get the application to rotate even using the default code. I am using XCODE 4.3 and regardless of what I do my second view (which is activated through a button push and rotates to the other screen that I have designed)

    ViewController 1 is set into portrait with the storyboard metric settings and UIViewController 2 is set into landscape mode with the metrics.

    Every time I run the emulator it starts with the first view in portrait (which its supposed to) and then I active the button that goes to my server pulls the data back and parses it out JSON ways. When it rotates to the other view controller it gives me the layout that I designed in the IDE but it won't rotate to landscape even though I "rotate" left and right the emulator.

    This is seriously driving me nuts.


    Any thoughts or ideas as to what I am doing wrong would be great. I don't know what portions of the code that you would need to see in order to help but ask an ye shall receive .

    Thanks in advance
    John
     
  8. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #8
    Sounds like you're making progress, yeah? ;)

    Seriously though, you still haven't answered the question as to how you are transitioning to the second view. Also, what does the latest version of your shouldAutorotateToInterfaceOrientation: method look like?
     
  9. idelovski macrumors regular

    Joined:
    Sep 11, 2008
    #9
    That was the right question, dejo. If he's pushing the second cotroller via navigation controller, then each view controller is taking care of itself in -shouldAutorotateToInterfaceOrientation: and if the second controller is just added as a subview to the first, then its method -shouldAutorotateToInterfaceOrientation: is never being used.

    He should put NSLog() everywhere and I mean EVERYWHERE.
     
  10. mngoose thread starter macrumors member

    Joined:
    May 26, 2012
    #10
    dejo - thanks for poking fun at me ... that actually mad me laugh :)
    gotta mess with the new guys and I understand and have been programing things long enough to never take offense at it .

    Actually you and idevlovski answered the question I am using subviews and not a navigation controller (still working on getting that to work properly in another version of the coding that I am doing. I think that I understand what idevlovski is talking about which make sense. and yes I have NSLogs all over the place as I figure out more and more of the variables that I can see.

    Code:
    
    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    {
        if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
            return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
        } else {
            return YES;
        }
    }
    
    That is the current version of the statements that I have been playing with. along with the other ones that I have tried nothing seems to work but I get what you guys are saying about the navi controller and when not using it the subviews never hit that -BOOL statement to make the changes and it makes sense to me.

    I guess that I will look at getting the navi controller working properly and go that route and will google changing orientation with subviews to give me more detailed information.

    thanks for the pointers guys. As always appreciate the help.

    John
     
  11. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #11
    You don't necessarily need to use a navigation controller (presenting the second view modally might also be an option) but adding it as a subview is almost certainly not going to help you.
     
  12. mngoose thread starter macrumors member

    Joined:
    May 26, 2012
    #12
    Dejo,

    I am doing the transition from one view controller to the next with a model button link from one viewer to the next but it s acting like it is not release the portrait part of the view metrics when it goes over to the over screen. I have even tried to turn inferring off and other metrics that are in there that I can find that might make the changes more smoothly but it isn't working right. I am sure that it is something stupid and I can't see the forrest for the trees like normal when I am doing coding.. just staying at it for three days gets annoying when you spend hours on a single problem.

    John
     
  13. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #13
    We're probably going to need to see the code for this transition before we can provide any further help.

    Also, to be clear, setting the orientation for the view in IB does not actually control how it will be presented. It's just there to allow you to simulate how the layout will be handled.
     
  14. mngoose thread starter macrumors member

    Joined:
    May 26, 2012
    #14
    here is the code that I have for the button

    Code:
    
    -(IBAction)ViewController; // connect to button
    -(IBAction) retrieve : (id) sender;      // connect to button
    -(BOOL) textFieldShouldReturn: (UITextField *)textfield;
    @end
    
    
    
    i am assuming that I would have to put something in there to set the orientation of the screen once the button is clicked (have no idea what is supposed to go there) and I have tried moving the -BOOL movement button over to there from the .m file with no success.

    Let me know what else you want to see

    Thanks for the help
    John
     
  15. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #15
    That just looks like the declarations from your @interface (i.e. .h) file. We need to see the code from your @implementation (i.e. .m) file.

    Besides that, what is your ViewController IBAction? It's poorly named, for one. And which of the two "// connect to button"s is connected to the button you're concerned with in this thread?
     
  16. mngoose thread starter macrumors member

    Joined:
    May 26, 2012
    #16
    As I am normally an android / html / php programmer and still working my way through sifting through the differences for the android to IOS model its so much fun and by that I really mean not so much.... :) .
    That why I am using generics for right now and will clean up the code when I get it all working and changed out .

    Code:
    //
    //  ticket1ViewController.m
    //  ticket15
    //
    //  Created by John Schimanski Jr on 5/28/12.
    //  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
    //
    
    #import "ViewController.h"
    #import "Foundation/NSJSONSerialization.h"
    
    @interface ViewController ()
    
    @end
    
    @implementation ViewController
    @synthesize PhoneTextField;
    @synthesize PinTextField;
    @synthesize ConfirmTextField;
    
    @synthesize PhoneText;
    @synthesize PinText;
    @synthesize ConfirmText;
    @synthesize phonelabel;
    @synthesize pinlabel;
    @synthesize confirmlabel;
    
    @synthesize json1;
    @synthesize localError;
    
    // json parsed variables
    @synthesize location;
    @synthesize firstname;
    @synthesize lastname;
    @synthesize email;
    @synthesize confirmcode;
    @synthesize datetime;
    @synthesize phonenumber;
    @synthesize pincode;
    @synthesize retrieved;
    @synthesize item;
    @synthesize desc;
    @synthesize price;
    @synthesize quantity;
    @synthesize sku;
    @synthesize salesrep;
    @synthesize companyLogo;
    @synthesize lastorder;
    @synthesize skuimage;
    
    
    
    
    
    
      
        
    }
    - (IBAction)retrieve : (id) sender {
        PhoneText = PhoneTextField.text;
        PinText   = PinTextField.text;
        ConfirmText = ConfirmTextField.text;
        NSLog (@"%@", PhoneText);
        NSLog (@"%@", PinText);
        NSLog (@"%@", ConfirmText);
        NSString *strURL = [NSString stringWithFormat:@"http://x.x.x.x/apps/mobile.php?Fone=%@&pincode=%@&confirm=%@&edit=Register", PhoneText, PinText,ConfirmText ];
        
        NSData *dataURL = [NSData dataWithContentsOfURL:[NSURL URLWithString : strURL]];
        
        NSString *strResult = [[NSString alloc] initWithData:dataURL encoding:NSUTF8StringEncoding];
        
        NSData * datajson = [strResult dataUsingEncoding: NSUTF8StringEncoding];
        
        id my2ConvertedJSONData = [NSJSONSerialization JSONObjectWithData : datajson options: NSJSONReadingMutableContainers error:&localError];
        
        if ([my2ConvertedJSONData isKindOfClass:[NSDictionary class]]) {
            NSLog(@"DICTIONARY");
            NSLog(@"dict3: %@", my2ConvertedJSONData);
        }
          
        
    }
    - (BOOL) textFielfShouldReturn:(UITextField *)textfield
    { [textfield resignFirstResponder];
        return YES;
    }
    
    
    
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
    	// Do any additional setup after loading the view, typically from a nib.
    }
    
    - (void)viewDidUnload
    {
        [super viewDidUnload];
        // Release any retained subviews of the main view.
    }
    
    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    {
        if ([[UIDevice currentDevice] userInterfaceIdiom] != UIUserInterfaceIdiomPhone) {
            return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
        } else {
            return YES;
        }
    }
    
    @end
    

    The only thing that I took out of my code for this view is my variables that are all synthesized and put into a JSON Parsed out NSString for formatting later.
    I didn't think that it was important plus there are somethings in there that I don't need to broadcast right now.

    Like I said I am sure that its something simple stupid but every time I walk through a simple tutorial with storyboards and even with an xib it still isn't working right even with a step by step manual .

    the IAB are linked to a button on the portrait screen and then the other one is linked to the back button on another screen.


    thanks for the help
    John
     
  17. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #17
    I'm still not seeing any code that relates to the transition from the first view to the second, unless of course, you have that all hooked up in the storyboard. As such, I'm afraid I can't be of any further assistance. Sorry.
     
  18. mngoose thread starter macrumors member

    Joined:
    May 26, 2012
    #18
    Dejo,

    It is hooked up in the storyboard areas of the IB and the Xcode software. I appreciate the help so far and the Forums have been a great resource for me.

    How would I put the code in to handle the transition for the landscape portion in the IBaction code to force the transition? Is that even possible?

    Thanks for the pointers
    John
     
  19. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #19
    Although I have a basic understanding of storyboards, I've yet to use them to any serious extent. Therefore, I don't think I can really help you any more. Sorry.
     

Share This Page