How to create a custom UIStoryBoard - objective-c

I would like to create my own UIStoryBoard and force the system to use it (inspired by Jody's answer).
How can I do it?
Creating a class #interface MyStoryboard : UIStoryboard is not being invoked.

You were on the right path subclassing UIStoryboard: all you need to do now is to plug it into your application. The simplest way of doing it is in your application delegate's application:didFinishLaunchingWithOptions: method:
MyStoryboard.h
#interface MyStoryboard : UIStoryboard
-(id)instantiateViewControllerWithIdentifier:(NSString *)identifier;
#end
MyStoryboard.m
#implementation MyStoryboard
-(id)instantiateViewControllerWithIdentifier:(NSString *)identifier {
NSLog(#"Instantiating: %#", identifier);
return [super instantiateViewControllerWithIdentifier:identifier];
}
#end
MyAppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
UIStoryboard *storyboard = [MyStoryboard storyboardWithName:#"<identifier-of-your-storyboard>" bundle:nil];
self.window.rootViewController = [storyboard instantiateInitialViewController];
[self.window makeKeyAndVisible];
return YES;
}
With this code in place, you will see calls of NSLog every time the instantiateViewControllerWithIdentifier: method is called.

Related

How do I manually select an initial storyboard without the plist from the App Delegate?

I need to be able to select a storyboard with its initial UIViewController upon application launch per boolean choice. Each storyboard works... per .plist (original) setup.
So I removed the default initial storyboard entry in the app's plist; and attempted to do it manually.
What I got is a black screen on my device.
The variables 'storyboard', 'controller', 'window' are all non-nil. Yet I get no screen. Why?
Here's the code:
#implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UIColor *blissRed = [UIColor colorWithRed:228.0/255.0 green:0.0 blue:27.0/255.0 alpha:1.0];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.tintColor = blissRed;
BOOL introStoryboard = YES; // ...artificial condition for discussion.
UIStoryboard *storyboard;
UIViewController *controller;
if (introStoryboard) {
storyboard = [UIStoryboard storyboardWithName:#"Intro" bundle:nil];
controller = [storyboard instantiateViewControllerWithIdentifier:kIntroStoryboardBeginning];
} else {
storyboard = [UIStoryboard storyboardWithName:#"Bliss" bundle:nil];
controller = [storyboard instantiateViewControllerWithIdentifier:kBlissStoryboardBeginning];
}
[self.window setRootViewController:controller];
return YES;
}
#end
you just forgot the following line before return
[self.window makeKeyAndVisible];
I was in the situation once. I resolved it by doing-
[navigationController pushViewController:controller animated:YES];
So what u need to do is -
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UIColor *blissRed = [UIColor colorWithRed:228.0/255.0 green:0.0 blue:27.0/255.0 alpha:1.0];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.tintColor = blissRed;
BOOL introStoryboard = YES; // ...artificial condition for discussion.
UIStoryboard *storyboard;
UIViewController *controller;
UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController;
if (introStoryboard) {
storyboard = [UIStoryboard storyboardWithName:#"Intro" bundle:nil];
controller = [storyboard instantiateViewControllerWithIdentifier:kIntroStoryboardBeginning];
} else {
storyboard = [UIStoryboard storyboardWithName:#"Bliss" bundle:nil];
controller = [storyboard instantiateViewControllerWithIdentifier:kBlissStoryboardBeginning];
}
[navigationController pushViewController:controller animated:YES];
return YES;
}

how I use MFSideMenu?

I'm trying to create a MFSideMenu however I never used it and do not know what it missing. Can anyone write a short tutorial on how to finalize it?
-(void)viewDidLoad{
MFSideMenuContainerViewController *container = [MFSideMenuContainerViewController
containerWithCenterViewController:centerViewController
leftMenuViewController:leftMenuViewController
rightMenuViewController:rightMenuViewController];
self.window.rootViewController = container;
[self.window makeKeyAndVisible];
}
In AppDelegate.h
#import "MFSideMenuContainerViewController.h"
In AppDelegate.m
MFSideMenuContainerViewController *container = (MFSideMenuContainerViewController *)self.window.rootViewController;
UINavigationController *navigationController = [self.storyboard instantiateViewControllerWithIdentifier:#"navigationController"]; //I have instantiated using storyboard id.
UIViewController *leftSideMenuViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"sideMenuViewController"]; //I have instantiated using storyboard id.
[container setLeftMenuViewController:leftSideMenuViewController];
[container setCenterViewController:navigationController];
In Your controller.m add:
- (IBAction)menuPressed:(id)sender
{
[self.menuContainerViewController toggleLeftSideMenuCompletion:nil];
}
- (MFSideMenuContainerViewController *)menuContainerViewController
{
return (MFSideMenuContainerViewController*)self.navigationController.parentViewController;
}
Put this code into your AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// init center, left, and right view controllers
MFSideMenuContainerViewController *container = [MFSideMenuContainerViewController
containerWithCenterViewController:centerViewController]
leftMenuViewController:leftMenuViewController
rightMenuViewController:rightMenuViewController];
self.window.rootViewController = container;
[self.window makeKeyAndVisible];
return YES;
}

receiver type 'AppDelegate' for instance message does not declare a method with selector ERROR

Can't seem to nail this issue here. Any idea what needs to be changed? I'm using Xcode 4.2.
Error: AppDelegate.m:23:6: error: receiver type 'AppDelegate' for instance message does not declare a method with selector 'setupHUD' [4]
#implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
LoginViewController *loginVC = [[LoginViewController alloc] init];
UINavigationController *navCon = [[UINavigationController alloc] initWithRootViewController:loginVC];
navCon.navigationBarHidden = YES;
self.window.rootViewController = navCon;
[self setupHUD];
[self.window makeKeyAndVisible];
return YES;
}
- (void) setupHUD
{
//setup progress hud
self.HUD = [[MBProgressHUD alloc] initWithFrame:self.window.bounds];
[self.window addSubview:self.HUD];
self.HUD.dimBackground = YES;
self.HUD.minSize = CGSizeMake(150.f, 150.f);
self.HUD.delegate = self;
self.HUD.isCancelButtonAdded = YES;
self.HUD.labelText = #"Loading";
}
Prior to Xcode 4.3, you need to forward declare methods called within the same #implementation block, so make sure to add the declaration of setupHUD to either the class extension (the list of declarations inside AppDelegate.m that starts with #interface AppDelegate ()), or the #interface block in AppDelegate.h.
For example, in AppDelegate.m:
#interface AppDelegate ()
// Other declarations...
- (void)setupHUD;
#end
Or in AppDelegate.h:
#interface AppDelegate : NSObject <UIApplicationDelegate>
// Other declarations
- (void)setupHUD;
#end

How to set view controller as root view in Cocoa?

I have an AppDelegate and mainWindow.xib. I created another viewController and called from AppDelegate and it is running good. Now my doubt is whether its possible to make the view controller as root with out adding it to mainWindow.xib. Whether its must to load our view with mainWindow.xib?
I am calling view controller like this
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
self.view = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
[self.window.contentView addSubview:self.view.view];
self.view.view.frame = ((NSView*)self.window.contentView).bounds;
}
Try using
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
self.viewControllerObj = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.rootViewController = self.viewControllerObj;
[self.window makeKeyAndVisible];
return YES;
}
Try this:
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
self.view = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.rootViewController = self.view;
}
1.add this to your appdelegate.h
#property (strong, nonatomic) UIWindow *window;
2.add this to your appdelegate.m in didFinishLaunching method
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
self.viewController = [[[ViewController alloc] initWithNibName:#"ViewController" bundle:nil] autorelease];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;

Adding a UINavigationController

I'm trying to add a UINavigationController to my ViewController. And when I launch the app, it gives me just a black screen and dont init the app.
This is my AppDelegate.m :
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UIViewController *rootView = [[ViewController alloc]
initWithNibName:#"ViewController"
bundle:nil];
self.navController = [[UINavigationController alloc] initWithRootViewController:rootView];
[[self window] setRootViewController:self.navController];
//template code
[self.window makeKeyAndVisible];
[rootView release];
return YES;
}
I'm following this article: http://simplecode.me/2011/09/04/an-introduction-to-uinavigationcontroller/
What's wrong? Thanks!
Try to use ARC adn storyboard If you are targeting IOS 5+, it would be a lot easier and you wouldn even have to code, just drag and drop the navigation controller to storyboard.
havent test but try this:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UIViewController *rootView = [[ViewController alloc]
initWithNibName:#"ViewController"
bundle:nil];
UINavigationController *navigationController=[[UINavigationController alloc] initWithRootViewController:rootView];
self.window.rootViewController =nil;
self.window.rootViewController = navigationController;
[self.window makeKeyAndVisible];
return YES;
}
Just testing with a project and used this code...
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.viewController = [[CCViewController alloc] initWithNibName:#"CCViewController" bundle:nil];
UINavigationController *navControl = [[UINavigationController alloc] initWithRootViewController:self.viewController];
self.window.rootViewController = navControl;
[self.window makeKeyAndVisible];
return YES;
}
Worked fine.