0

I'm having a bit of an issue here switching between my view controllers.

I followed the advice on this page:

View Controllers: How to switch between views programmatically?

My setup is as follows; I have a splash screen with a button on it and that button loads my second view controller. I have IntroductionViewController as the root view and the button says "Load Website", this Load Website event I want to switch to WebsiteViewController.

On IntroductionViewController I have set up the button as an IBOutlet and IBAction which is NSLogging fine on Click so I know the method is working.

What I need is when you click Load Website it opens WebsiteViewController. What I have so far is credit of the question above:

IntroductionViewController.h

#import <UIKit/UIKit.h>
#import "WebsiteViewController.h"

@class WebsiteViewController;

@interface IntroductionViewController : UIViewController {
    IBOutlet UIButton *websiteButton;
    IBOutlet WebsiteViewController *websiteViewController;
}
@property (nonatomic, retain) UIButton *websiteButton;
@property (nonatomic, retain) WebsiteViewController *websiteViewController;

-(IBAction)loadWebsite:(id)sender;

@end

IntroductionViewController.m

#import "IntroductionViewController.h"

@implementation IntroductionViewController

@synthesize websiteButton;
@synthesize websiteViewController;

-(IBAction)loadWebsite:(id)sender{
    if(self.websiteViewController.view.superview == nil)
    {
        [self.view addSubview:websiteViewController.view];
    }   
}

The line: [self.view addSubview:websiteViewController.view]; doesn't do anything, but as I've said previously, this function is NSLogging fine. I'm not sure how to proceed with this.

Community
  • 1
  • 1
Dan Hanly
  • 7,829
  • 13
  • 73
  • 134
  • Why don't you just push the websiteViewController? – Francesco Puglisi May 25 '11 at 10:16
  • 1
    @Daniel just a thought. Have you bind the IBOutlet for the WebsiteViewController ? – Janak Nirmal May 25 '11 at 10:19
  • @Jennis, that would be the issue! However, both IntroductionViewController and WebsiteViewController are landscape and when WebsiteViewController is displayed, it's up on it's left edge and the right hand side of it is off the top of the iPad. How do I enable the orientation of both view controllers to sit landscape? – Dan Hanly May 25 '11 at 10:24
  • @Daniel Override - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { // Overriden to allow any orientation. return ((interfaceOrientation==UIInterfaceOrientationLandscapeLeft) || (interfaceOrientation==UIInterfaceOrientationLandscapeRight)); } In both of your viewcontroller. And also make view landscape in XIB file if required accordingly. – Janak Nirmal May 25 '11 at 10:30
  • @Jennis - this doesn't fix the issue because all the views are Landscape in the XIB and I have that code already lol – Dan Hanly May 25 '11 at 10:38
  • Post an answer and I'll mark you correct. – Dan Hanly May 25 '11 at 10:39
  • @Daniel Thax. I posted my answer. – Janak Nirmal May 25 '11 at 10:54

3 Answers3

2

Looks like you are not allocating your websiteViewController, without allocating it you have no instance of it to use or reference to it, logging it will simply return null.

I would also try presenting is a ModalViewController if you are not implementing UINavigationController. Here is an example of allocating your webview controller and presenting it as a modalView:

-(IBAction)loadWebsite:(id)sender{
    WebsiteViewController *webView = [[WebsiteViewController alloc] init];
    [self.view presentModalViewController:webView animated:YES];
    [webView release];
}

The default transition style is UIModalTransitionStyleCoverVertical but check out the documentation for more. You can set the transition style like so:

webView.modalTransitionStyle = UIModalTransitionStyleCoverVertical
Alex
  • 2,513
  • 5
  • 27
  • 42
1

I bet your WebsiteViewController object hasn't been instantiated. NSLog(@"%@", websiteViewController); I bet its null! :p You need to create the object - I don't know how to do this using interface builder.

Anyway, you're using the old way to switch between view controllers. The new way/ best way is to use a navigation controller to do it like this:

-(IBAction)loadWebsite:(id)sender{
    [self.navigationController pushViewController:websiteViewController animated:YES];  
}

But this won't work unless you've set up a navigation controller, which I don't know how to do in interface builder. This is why I hate interface builder and believe that you should stop learning with it! :p

Without interface builder this is what you should do:

In your app delegate .h file add this at the top:

#include "IntroductionViewController.h"

Then include this with the other @propertys in the code:

@property (nonatomic, retain) UINavigationController *navController;

Now in your app delegate .m file swap the application did finish launching with options with this:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.
    [self.window makeKeyAndVisible];

    // lets make the navigation controller by setting up a view controller first.
    IntroductionViewController *viewController = [[IntroductionViewController alloc] initWithNibName:nil bundle:nil];
    viewController.title = @"Home";
    navController = [[UINavigationController alloc] initWithRootViewController:viewController];
    [navController setNavigationBarHidden:NO]; // set to yes to hide the title bar
    [viewController release];

    [self.window addSubview:navController.view];

    return YES;
}

That will give you a navigation controller (you can hide the title bar easily... - see the comments). And show the first view controller (IntroductionViewController).

Now running the code I gave you above will work. The [self.navigationController pushViewController:websiteViewController animated:YES];. This gives the viewController to the navigationController and the navigationController does all the adding and removing views and such itself!

And you haven't had to use interface builder at all! :p (well... now you have to learn how to build views without dragging and dropping!).

Anyway... i hope that helped.

Thomas Clayson
  • 29,657
  • 26
  • 147
  • 224
  • Thank you, the reason I hadn't used the Navigation Controller method is because I didn't want a navigation controller bar. Now I know you can hide it, that saves me some effort. I hadn't linked the websiteViewController up to the interface builder object, which is why it returned NULL. – Dan Hanly May 25 '11 at 10:46
1

Possible you may have forgot to bind the IBOutlet to your WebsiteViewController. And for both Orientation support you can use

 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOr‌​ientation 
 { 
      // Overriden to allow any orientation. 
      return ((interfaceOrientation==UIInterfaceOrientationLandscapeLeft) || (interfaceOrientation==UIInterfaceOrientationLandscapeRight)); 
 }

One more thought for your orientation problem, you can setFrame to your view while displaying like

 - [yourViewController.view setFrame:CGRectMake(0,0,1024,768)] //iPad and 
 - [yourViewController.view setFrame:CGRectMake(0,0,480,320)] //iPhone.

May be this works.

Thax.

Janak Nirmal
  • 22,706
  • 18
  • 63
  • 99