View in empty template - iphone

I need to create my own view with controller on empty template without help of Interface Builder. What i need to write in AppDelegate ?

XCode -> New project -> Select Window Based Template. Then Remove MainWindow.xib from resources, from info.plist remove this key "Main Nib file base name".In your main.m file
int retVal = UIApplicationMain(argc, argv, nil, #"AppDelegate"); // 4th parameter is name of your AppDelegate. In your AppDelegate.h remove property/synthesize for your IBOutlet UIWindow as you are not using IB at all..
// In your AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[window makeKeyAndVisible];
// In your AppDelegate.h AnotherViewController* mainViewController;
mainViewController = [[AnotherViewController alloc] init];
[[mainViewController view] setFrame:[UIScreen mainScreen].applicationFrame];
[window addSubView:[mainViewController view]];
}
// In your AnotherViewController implement
- (void)loadView
{
// Lets say you have a UITableView
UITableView* tableView = [[[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain] autorelease];
[self setView:tableView];
}

Related

"Application windows are expected to have a root view controller" conditional appearance

I'm writing an app for iPhone using Xcode 4.5 and iOS6. I'm also creating a new UIWindow to be able to manage the area of the status bar (to display messages there, etc.)
I'm using storyboards and my appDelegate method looks like this:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
return YES;
}
The message does not appear in the console when I put it in the method called viewDidAppear:
- (void)viewDidAppear:(BOOL)animated {
if (!window) {
window = [[SGStatusBar alloc] initWithFrame:CGRectZero];
window.frame = [[UIApplication sharedApplication] statusBarFrame];
window.alpha = 0.5f;
[self.view.window makeKeyAndVisible]; // has to be main window of app
window.hidden = NO;
}
}
The same method, put in the viewDidLoad gives a warning in the console:
2012-12-27 11:34:20.838 NewApp[433:c07] Application windows are expected to have a root view controller at the end of application launch
Is this because I've created a new UIWindow? Why the difference between those two methods is so big?
And, most importantly, how can I get rid of this warning while putting the code in the viewDidLoad method?
EDIT:
I have encountered the same problem here, but it's not the way I'd like to solve it (it's actually the way I'm solving it right now)
I've tried setting my current ViewController as my window's root view controller by doing this:
ViewController *vcB = [[UIViewController alloc] init];
window.rootViewController = vcB;
But I keep getting a warning that says:
Incompatible pointer types initializing 'ViewController *__strong' with an expression of type 'UIViewController *'
Set window.rootViewController property .
Add the following code to your delegate.h and delegate.m files.
AppDelegate.h
#property (strong, nonatomic) UIWindow *window;
#property (strong, nonatomic) YourViewController *viewController;
AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
self.viewController = [[[YourViewcontroller alloc] initWithNibName:#"YourViewcontroller" bundle:nil] autorelease];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
Hope it works.

How to assign a viewcontroller as a self viewcontroller to the appdelegate class?

I have a view controller class LoginViewcontroller where i need to add that view cotroller as a self viewcontroller to the appdelegate class. I need something like when i try to access self.viewcontroller, my LoginViewController should respond. Any help on this?
Write down this code:
AppDelegate.h File
#import "LoginViewController.h"
Create Object of LoginViewController class
LoginViewController *loginVC;
Create Property of LoginViewController
#property (nonatomic, retain) LoginViewController *loginVC;
AppDelegate.m File
#synthesize loginVC;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
self.loginVC = [[[LoginViewController alloc] initWithNibName:#"LoginViewController" bundle:nil] autorelease];
self.window.rootViewController = self.loginVC;
[self.window makeKeyAndVisible];
return YES;
}
If what you're trying to do is setup the root view controller for your application, then:
self.rootViewController = loginViewController;
If that's not what you need, please provide some more info as to what you´re trying to accomplish.

Starting an App up with a different XIB

I want to start my app up with a different Xib. How would I do this?
Thanks
If you're using interface builder:
Under SupportingFiles, in -info.plist, look for a key named "Main nib file base name". Change that to the XIB you want it to load first
You can also take that entry out of the plist altogether an in main.m give it your appDelegate's name:
int retVal = UIApplicationMain(argc, argv, nil, #"HelloViewAppDelegate");
Then in your appDelegate, you can manually load your first view controller based on your code and logic. Personally, I like this better because it's much clearer to - here's my delegate and code to load it. It doesn't have all the bindings in IB I need to remember.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
CGRect screenBounds = [[UIScreen mainScreen] applicationFrame];
CGRect windowBounds = screenBounds;
windowBounds.origin.y = 0.0;
// init window
[self setWindow: [[UIWindow alloc] initWithFrame:screenBounds]];
// init view controller
_mainViewController = [[MainViewController alloc] init];
[[self window] addSubview:[_mainViewController view]];
[self.window makeKeyAndVisible];
return YES;
}
EDIT:
Answering your comment below. You pasted this invalid code:
// init view controller
ViewController = [[ViewController alloc] init];
[[self window] addSubview:[ViewController view]];
That's not valid. you need an instance variable name. By referring to it as "ViewController" your attempting to call class member variables. If your class is called ViewController then it should be:
// notice the instance myviewController is of type ViewController
ViewController *myViewController = [[ViewController alloc] init];
// notice calling view against instance (myViewController)
[[self window] addSubview:[myViewController view]];
At this point, if it's not compiling you need to edit your question and paste your main.m and appDelegate exactly as is into the question.
The MainWindow.xib file is just a shell that provides the application's UIWindow. While you can put a UIViewController in MainWindow.xib, you can also just have an unconnected UIViewController outlet on your app delegate and pick which nib to load at runtime in your application:didFinishLaunchingWithOptions: method, e.g.:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)options {
if (case1)
self.viewController = [[[MyViewController1 alloc] initWithNibName:nil bundle:nil] autorelease];
else if (case 2)
self.viewController = [[[MyViewController2 alloc] initWithNibName:nil bundle:nil] autorelease];
else
self.viewController = [[[MyViewController2 alloc] initWithNibName:nil bundle:nil] autorelease];
[window addSubview:self.viewController.view];
[window makeKeyAndVisible];
return YES;
}

iphone simulator message "My Universal App on Iphone" was does it mean?

I run the simulator on a very simple app- a navigation controller contained in a view controller. The application is a view based app with no modification done to the header, and the below code in the implementation.
I get this message "My Universal App on Iphone" superimposed over the subview of the navigation controller and I literally saw one result on google that mentioned it! How do I get rid of this message?
#import "[header filename]"
#implementation tutorial_navigationcontrollerAppDelegate
#synthesize window=_window;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
//ViewController *viewController = [[ViewController alloc] init];
//viewController.title = #"Hello Nav";
//UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
UIViewController *x = [[UIViewController alloc] init];
x.title = #"XXX";
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:x];
self.window.rootViewController = navigationController;
[self.window makeKeyAndVisible];
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application {}
- (void)applicationDidEnterBackground:(UIApplication *)application {}
- (void)applicationWillEnterForeground:(UIApplication *)application {}
- (void)applicationDidBecomeActive:(UIApplication *)application {}
- (void)applicationWillTerminate:(UIApplication *)application {}
- (void)dealloc
{
[_window release];
[super dealloc];
}
#end
I noticed the same thing while adding a view controller. While I can't explain the label doesn't disappear like it should, I did notice that if you change the controller's view in any way it will remove the text.
UIViewController *x = [[UIViewController alloc] init];
x.view.backgroundColor = [UIColor whiteColor];
Edit: My previous answer did work to a degree, but it turns out it's actually pretty straightforward. Check out MainWindow_iPhone.xib and MainWIndow_iPad.xib. Just delete the labels sitting in the view!

Loading a view controller & view hierarchy programmatically in Cocoa Touch without xib

It seems like all of the Cocoa Touch templates are set up to load a nib.
If I want to start a new project that's going to use a view controller, and load its view(hierarchy) programatically, not from a nib/xib, what are the steps to setting that up or adjusting a template.
I though all I had to do was implement -loadView, but I have trouble every time I try to do this.
It's reasonably simple to do completely programmatic user interface generation. First, you need to edit main.m to look something like the following:
int main(int argc, char *argv[])
{
NSAutoreleasePool *pool = [NSAutoreleasePool new];
UIApplicationMain(argc, argv, nil, #"MyAppDelegate");
[pool release];
return 0;
}
where MyAppDelegate is the name of your application delegate class. This means that an instance of MyAppDelegate will be created on launch, something that is normally handled by the main Nib file for the application.
Within MyAppDelegate, implement your applicationDidFinishLaunching: method similar to the following:
- (void)applicationDidFinishLaunching:(UIApplication *)application
{
window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
if (!window)
{
[self release];
return;
}
window.backgroundColor = [UIColor whiteColor];
rootController = [[MyRootViewController alloc] init];
[window addSubview:rootController.view];
[window makeKeyAndVisible];
[window layoutSubviews];
}
where MyRootViewController is the view controller for the primary view in your window. This should initialize the main window, and add the view managed by MyRootViewController to it. rootController is kept as an instance variable within the delegate, for later reference.
This should let you programmatically generate your user interface through MyRootViewController.
UIViews themselves do not have a hierarchy, UINavigationControllers do. So init one of those, and pop a UIViewController onto it's stack. This should do it, in the most basic way possible, with no XIB files at all. You should be able to build on this.
- (void)applicationDidFinishLaunching:(UIApplication *)application {
UINavigationController *navController = [[UINavigationController alloc] init];
UIViewController *viewController = [[UIViewController alloc] init];
// set the properties of viewController here, to make it look like you want
[navController pushViewController:viewController animated:NO];
[window addSubview:navController.view];
// Don't forget memory management
[navController release];
[viewController release];
[window makeKeyAndVisible];
}