how I use MFSideMenu? - objective-c

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;
}

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;
}

Navigation Controller not working when enabling ARC in xcode 5 and using ios 7

I enabled ARC and using ios 7 for my app.With out using xib i am doing programming.But i am unable to navigate from one view controller to another view controller.Created obj in .h file for a class.
In .h file
#property(nonatomic,strong)CountriesViewController *countryViewController;
In .m file in a button action.
countryViewController = [[CountriesViewController alloc] init];
[self.navigationController pushViewController:countryViewController animated:YES];
You need to add navigation Controller in AppDelegate like this,
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.viewController = [[YourViewController alloc] initWithNibName:#"YourViewController" bundle:nil];
self.navigationController = [[UINavigationController alloc] initWithRootViewController:self.viewController];
self.window.rootViewController = self.navigationController;
[self.window makeKeyAndVisible];
return YES;
}
In your ViewController, navigate like this,
self.countryViewController = [[CountriesViewController
alloc]initWithNibName:#"CountriesViewController" bundle:nil];
[self.navigationController pushViewController:self.countryViewController animated: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.

Set title and add button to navigation controller

I created a navigation controller but i can not set title or add button to the navigation bar.How to do that?
This is the code of application DidFinishLauchingOption in file AppDelegate.m :
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:view];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
self.view = [[ViewController alloc] init];
self.window.rootViewController = self.view;
[self.window addSubview:navController.view];
[self.window makeKeyAndVisible];
return YES;
}
Thanks in advance.
You need to set the rootViewController property of your window object to the navigation controller, rather than your instance of `ViewController. This should point you in the right direction:
#implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Create and configure a window
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
// Create a view controller
UIViewController *viewController = [[ViewController alloc] init];
// Create a navigation controller and set its root view controller to the instance of `ViewController`
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:viewController];
// Add the navigation controller to the window
self.window.rootViewController = navController;
[self.window makeKeyAndVisible];
return YES;
}
// ...
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Set the view controller's title
self.title = NSLocalizedString(#"View Controller", #"");
// Add a navigation bar button
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:#selector(refreshButtonPressed:)];
}
- (void)refreshButtonPressed:(id)sender
{
// Do something when the refresh button is pressed.
}
// ...
#end
To create your initial setup, you create a navigation controller with your view controller and set it as the root view controller of your app delegate window:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//create window
[self setWindow:[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]];
//create and set root view controller
[[self window] setRootViewController:[[UINavigationController alloc] initWithRootViewController:[[RootViewController alloc] init]]];
//make window key and visible
[self.window makeKeyAndVisible];
//bail
return YES;
}
Then in your view controller, set the title and add your navigation item:
- (void)viewWillAppear:(BOOL)animated
{
//call parent implementation
[super viewWillAppear:animated];
//set view controller title
[self setTitle:#"Root View Controller"];
//add navigation bar button
[[self navigationItem] setRightBarButtonItem:[[UIBarButtonItem alloc] initWithTitle:#"Button Title" style:UIBarButtonItemStyleBordered target:self action:#selector(handleBarButtonItemEvents:)]];
}
And listen for button events via:
- (void)handleBarButtonItemEvents:(id)sender
{
//
}

How to create a custom UIStoryBoard

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.