Switch to a different Window - Cocoa Touch -

Discussion in 'iPhone/iPad Programming' started by oojustfakeoo, Aug 11, 2011.

  1. oojustfakeoo, Aug 11, 2011
    Last edited by a moderator: Aug 11, 2011

    macrumors newbie

    Joined:
    Aug 11, 2011
    #1
    i'm totally new to Objective-C and Cocoa Touch even if i have some basis of programming.

    I would like to know how to set up an Action that behave like this.

    When i press a button on the Main View, it opens a new window created before, then when i press a button on the 2nd window it switch back to the main one.
    I just miss the instruction to switch between windows

    ViewController.h

    Code:
        IBOutlet UIWindow *NewWindw;
        IBOutlet UIButton *NewButton;
    
    Up Here i've declared the button and the new window.

    ViewController.m

    Code:
     -(IBAction) LaunchWindowsF: (id) sender{
    
        **What here?**
      }
    
    Up Here i've declared the function that should load the new window, and connected it with the interface builder to the event TouchUpInside.

    What's the code to launch the window NewWindw?

    Thanks everyone!
     
  2. macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #2
    It would be very unusual to use multiple windows under iOS. Stop coding for a moment and read the iOS Application Programming Guide so at least you're aiming for the right goal. Then read the iOS Human Interface Guidelines so that you're design the right kind of UI for your app. iOS users expect their apps to behave a certain way which is philosophically different from desktop apps, and will quickly reject apps that don't meet their expectations.
     
  3. macrumors regular

    Joined:
    Aug 26, 2010
    #3

    I would have to agree with Jiminaus. Understanding iOS paradigms will save you time in the long run. Yes, creating your first app is exciting but having a good foundation will keep you interested in app making. Otherwise you will become frustated and possibly give up.
     
  4. macrumors 6502

    Joined:
    Jul 4, 2009
    #4
    You don't want to load a new window. You want to load a new view controller. I would do something like this:

    ViewControllerOne.h

    Code:
    #import <UIKit/UIKit.h>
    
    @interface ViewControllerOne : UIViewController {
        
    }
    
    -(IBAction)loadViewTwo:(id)sender;
    
    @end
    
    ViewControllerOne.m

    Code:
    #import "ViewControllerOne.h"
    #import "ViewControllerTwo.h"
    
    @implementation ViewControllerOne
    
    -(IBAction)loadViewTwo:(id)sender {
        ViewControllerTwo *viewTwo = [[ViewControllerTwo alloc] initWithNibName:@"ViewControllerTwo" bundle:nil];
        
        [self.view addSubview:viewTwo.view];
    }
    
    ViewControllerTwo.h

    Code:
    #import <UIKit/UIKit.h>
    
    @interface ViewControllerTwo : UIViewController {
        
    }
    
    -(IBAction)loadViewOne:(id)sender;
    
    @end
    
    ViewControllerTwo.m

    Code:
    #import "ViewControllerTwo.h"
    #import "ViewControllerOne.h"
    
    
    @implementation ViewControllerTwo
    
    -(IBAction)loadViewOne:(id)sender {
        ViewControllerOne *viewOne = [[ViewControllerOne alloc] initWithNibName:@"ViewControllerOne" bundle:nil];
        
        [self.view addSubview:viewOne.view];
    }
    
    By allocating and initializing a new view controller via code, you remove several steps from interface builder. All you have to do in interface builder is connect your buttons to the correct IBActions by control click + dragging to file's owner.

    You could also do it modally.
     
  5. thread starter macrumors newbie

    Joined:
    Aug 11, 2011
    #5
    Thank you very much, that's exactly what i meant! :)
    I'll try to do so, and let you know!
     

Share This Page