I can't get my iPhone app to display any of the content on startup.
Any idea what's wrong here?
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
HomeViewController *t = [[HomeViewController alloc] init];
[self.window addSubview:t.view];
[self.window makeKeyAndVisible];
// I tried the following in case the problem is my
//view controller but nothing shows up at all
UISegmentedControl *t = [[UISegmentedControl alloc] init];
[self.window addSubview:t];
[self.window makeKeyAndVisible];
}
Unless your view controller implements loadView, you should load the view from a NIB file with initWithNibName:bundle:.
A segmented control should be initialized with initWithItems:. You should also assign a frame to the control.
Related
I'm using xCode 4.3.2 and started a blank application.
I have a navigation controller and a simple logincontroller. I want the login controller to be my root view so it is this first thing that a user does when they login.
I'm using the following code and when I run the application it displays a black screen. I put in logging in the LoginViewController.m->ViewDidLoad and it is being run is there something im doing wrong the LoginViewController.xib is very simple it just contains a button right now that will switch to a tab view controller once I figure this out.
Thanks in advance.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions
{
UIViewController *loginController = [[LoginViewController alloc] initWithNibName:#"LoginViewController" bundle:nil];
navigationController = [[UINavigationController alloc] initWithRootViewController:loginController];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[self.window addSubview:navigationController.view];
[self.window makeKeyWindow];
return YES;
}
This is not right:
[self.window addSubview:navigationController.view];
change it to this:
self.window.rootViewController = navigationController;
I set up my app a while ago using a tutorial for setting up the navigationbar in interface builder, but no longer use interface builder in any of my app and would much like to change this 1 thing which does use interface builder to being coded in. So my question is, I have a navigationbar which works, and which appears on the first view of my app, HomeView. How would I make this happen just as it does now, programmatically?
In the AppDelegate.m file, add this:
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
RootViewController *rootViewController = [[RootViewController alloc] init];
UINavigationController *navController = [[UINavigationController alloc]
initWithRootViewController:rootViewController];
[window addSubview:[navController view]];
[self.window makeKeyAndVisible];
}
Be sure to add #import "RootViewController.h" at the top of the file.
Another way of adding the navigation bar programmatically, change the application:didFinishLaunchingWithOptions method of your app delegate like:
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
RootViewController *rootViewController = [[RootViewController alloc] init];
UINavigationController *navController = [[UINavigationController alloc]
initWithRootViewController:rootViewController];
self.window.rootViewController = navController;
[self.window makeKeyAndVisible];
}
I have custom view controller named DRTableViewController
In my app delegate, I use the following function to load
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
tvc = [[DRTableViewController alloc] init]; // tvc is created with xib
navCon = [[UINavigationController alloc] initWithRootViewController:tvc];
[self.window addSubview:[navCon view]];
[navCon release];
[self.window makeKeyAndVisible];
return YES;
}
but when I start my application, navigation controller appears but the view inside it is black,
when I use
[self.window addSubview:[tvc view]];
instead of [navCon view]; I can see my view without any problem
Thanks in advance
You need to retain your navigation controller so it is not released.
Create a property for you navigation controller and retain it in the application delegate.
A quick fix is to comment out the line,
[navCon release]
But this will introduce a memory leak.
I'm using addSubView twice in my app and both times it adds the view too high up the view, its off the screen at the top. See below... I have load the views like this as I nothing else works, cause me problems.
I just don't understand why they are showing off the screen?
What do I need to do to fix this ?
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[window addSubview:rootController.view];
[window makeKeyAndVisible];
lvc = [[GettingStartedViewController alloc]
initWithNibName:#"GettingStartedView" bundle:nil];
[window addSubview:lvc.view];
return YES;
}
And in my GettingStartedView ...
- (IBAction) showHelp:(id)inSender {
theController = [[HelpViewController alloc] initWithNibName:#"HelpView"
bundle:nil onPage:HelpPageGettingStarted];
[self.view addSubview:theController.view];
}
Set the location:
rootController.view.frame = CGRectMake(0, 20, 320, 460);
Check the wantsFullScreenLayout property of your root view controller, make sure it's not set.
I have noticed that when you create a new xcode project as a view based application it creates a view NIB and when that NIB is loaded it loads below the menu bar. I am trying to create a window based application and programmatically add a view to the window, but the view gets loaded underneath the menu bar so part of the view is being obstructed.
i have tried
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
MYViewController *viewController = [[MYViewController alloc] initWithNibName:#"MYViewController" bundle:nil];
[window addSubview:viewController.view];
[window makeKeyAndVisible];
return YES;
}
and i have also tried changing the "wantsFullScreenLayout" property of the view controller to FALSE;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
MYViewController *viewController = [[MYViewController alloc] initWithNibName:#"MYViewController" bundle:nil];
viewController.wantsFullScreenLayout = NO;
[window addSubview:viewController.view];
[window makeKeyAndVisible];
return YES;
}
any help would be appreciated
Thanks
found it!
[MYViewController.view setFrame:[[UIScreen mainScreen] applicationFrame]];