Reputation: 7839
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.
Upvotes: 0
Views: 504
Reputation: 22726
Possible you may have forgot to bind the IBOutlet to your WebsiteViewController. And for both Orientation support you can use
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// 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.
Upvotes: 1
Reputation: 2513
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
Upvotes: 2
Reputation: 29925
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.
Upvotes: 1