Adding a UINavigationController - objective-c

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.

Related

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

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

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;

How to ECSlidingViewController without storyboard?

I want to use ECSlidingViewController in my iOS 4.3 applications.
And I wonder how to apply this library without storyboard?
PLZ, how to? this is my code, but iOS simulator's screen is white only.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
//self.window.backgroundColor = [UIColor whiteColor];
FrontViewController *frontViewController = [[FrontViewController alloc] initWithNibName:#"FrontViewController" bundle:nil];
RearViewController *rearViewController = [[RearViewController alloc] initWithNibName:#"RearViewController" bundle:nil];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:frontViewController];
// create a DDMenuController setting the content as the root
//DDMenuController *menuController = [[DDMenuController alloc] initWithRootViewController:navigationController];
//menuController.leftViewController = rearViewController;
//RevealController *menuController = [[RevealController alloc] initWithFrontViewController:navigationController rearViewController:rearViewController];
ECSlidingViewController *slidingViewController = (ECSlidingViewController *)self.window.rootViewController;
slidingViewController.topViewController = navigationController;
slidingViewController.underLeftViewController = rearViewController;
self.window.rootViewController = slidingViewController;
[self.window makeKeyAndVisible];
return YES;
}
Here is your code
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:
(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
FrontViewController *frontViewController = [[FrontViewController alloc] initWithNibName:#"FrontViewController" bundle:nil];
RearViewController *rearViewController = [[RearViewController alloc] initWithNibName:#"RearViewController" bundle:nil];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:frontViewController];
ECSlidingViewController *slidingViewController = [[ECSlidingViewController alloc] init];
slidingViewController.topViewController = navigationController;
slidingViewController.underLeftViewController = rearViewController;
self.window.rootViewController = slidingViewController;
[self.window makeKeyAndVisible];
return YES;
}
ECSlidingViewController uses iOS5, Storyboard and ARC. To put efforts into reengineering this class, I suggest you choose other classes which are ready for lower iOS versions and not using Storyboard. Some similar examples are:
https://github.com/pkluz/ZUUIRevealController
https://github.com/mystcolor/JTRevealSidebarDemo
The primary issue in your code above is that the slidingViewController isn't being instantiated.
You need this line:
ECSlidingViewController *slidingViewController = [[ECSlidingViewController alloc] init];

didSelectRowAtIndexPath when select can't turn to other pages

if(!_personViewController)
{
_personViewController=[[PersonViewController alloc]initWithNibName:#"PersonViewController" bundle:nil];
}
_personViewController.user=_user;
[self.navigationController pushViewController:_personViewController animated:YES];
[PersonViewController release];
This is my code. I just want to when select the row can jump to personViewController page but it seems bad.
Just check it whether you write navigation controller in - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions and it is AppDelegate.m file.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
self.viewController = [[[ViewController alloc] initWithNibName:#"ViewController_iPhone" bundle:nil] autorelease];
UINavigationController *navControl = [[UINavigationController alloc] initWithRootViewController:self.viewController];
[self.window addSubview:[navControl view]];
[self.window makeKeyAndVisible];
return YES;
}
try this..
//_personViewController.user=_user;
or
_personViewController.user=[_user retain];