Make files for a programmatic UINavigationController? - ios

I made a UINavigationController, and I have it working just fine but I need to work with it now. I need files that are in sync with the controller. I build everything programatically.
How do I get these files set up?
AppDelegate.h:
#property (strong, nonatomic) UIWindow *window;
#property (strong, nonatomic) UINavigationController *navigationController;
#property (strong, nonatomic) UIViewController *rvc;
AppDelegate.m:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.navigationController = [[UINavigationController alloc] init];
[[UINavigationBar appearance] setBarTintColor:[UIColor colorWithRed:0/255 green:126/255 blue:233/255 alpha:1]];
[self.window addSubview:[self.navigationController view]];
[self.navigationController pushViewController:self.rvc animated:YES];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}

Create a UIViewController subclass and make rvc property have type of that class. After that you can write your logic in the created subclass.
Proper didFinishLaunchingWithOptions implementation:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.navigationController = [[UINavigationController alloc] init];
[[UINavigationBar appearance] setBarTintColor:[UIColor colorWithRed:0/255 green:126/255 blue:233/255 alpha:1]];
self.window.rootViewController = self.navigationController;
self.rvc = [[<your_class_name> alloc] init];
[self.navigationController pushViewController:self.rvc animated:YES];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}

it's simple you can apply this code :
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UIViewController *vc = [[UIViewController alloc]init];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:vc];
[[UINavigationBar appearance] setBarTintColor:[UIColor colorWithRed:0/255 green:126/255 blue:233/255 alpha:1]];
self.window.rootViewController = navigationController;
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}

Related

Changes made to Navigation Controller are not shown

I copied the example from View Controller Catalog for iOS made some changes to the colour and expected to see them reflected on the simulator. Nothing happens ???
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
UIViewController *myViewController = [[MyViewController alloc] init];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:myViewController];
[navigationController setNavigationBarHidden:NO animated:YES];
navigationController.title = #"Hello";
navigationController.navigationBar.barStyle = UIBarStyleBlack ;
navigationController.navigationBar.translucent = NO;
navigationController.navigationBar.tintColor = [UIColor blackColor];
navigationController.navigationBar.barTintColor = [UIColor greenColor];
UIWindow *window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
window.rootViewController = navigationController;
[window makeKeyAndVisible];
return YES; }
What do I do wrong?
Several things: First: be sure than in the General information of your target app, in Main Interface field is blank. (If you donĀ“t find it, delete all *.storyboard files that you have).
Second: In your AppDelegate.h should be this property:
#property (strong, nonatomic) UIWindow *window;
And last: Change this in your code:
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.rootViewController = navigationController;
[self.window makeKeyAndVisible];
In order to know changes is better step by step. (.title is in ViewController).

How can i connect UITableView to my Appdelegate.m?

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
MainViewController *rootViewController = [[MainViewController alloc] init];
_naviControl = [[UINavigationController alloc]initWithRootViewController:rootViewController];
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
[self.window addSubview:_naviControl.view];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
In my AppDelegate ..!
Connect to MainViewController.m
The AppDelegate is not a UIViewController so connecting a UITableView to it conflicts with the MVC principles as described here: https://developer.apple.com/library/ios/documentation/general/conceptual/devpedia-cocoacore/MVC.html
what you want to do is make your MainViewController's view have a tableView as a subview
something like this:
(in MainViewController.m)
- (void)viewDidLoad {
[super viewDidLoad];
self.feedTableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
self.feedTableView.dataSource = self;
self.feedTableView.delegate = self;
[self.view addSubview:self.feedTableView];
}

Programmatically creating UINavigationController in iOS

I am new to iOS. And I want to use navigation controller in my application but I have no any idea how to do it. So can any one guide me step by step for creating navigation in my application.
In appDelegate.h
#property (strong, nonatomic) UINavigationController *navController;
and set the delegate UINavigationControllerDelegate and synthesise object in appDelegate.m
now,
appDelegate.m
you can set navigation controller in didFinishLaunchingWithOptions method
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
frstVwCntlr = [[firstViewController alloc] initWithNibName:#"firstViewController" bundle:nil];
self.navController = [[UINavigationController alloc] initWithRootViewController:self.frstVwCntlr];
self.window.rootViewController = self.navController;
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
In the above code , your firstViewController is set to UINavigationController and UINavigationController added to UIWindow like
self.window.rootViewController = self.navController
Hope this may help you
If you want to create everything programmatically you have to do it in AppDelegate.
But if you don't want to do it programmatically, then just select the ViewController in Storyboard then select menu options:
Editor > Embed In > Navigation Controller
You can creat UINavigationController in Appdelegate and set your first viewcontroller on it.
So for creating a UINavigationController programatically without using storyboards, go to your app delegate and do the following. Create two properties, window and viewController
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor=[UIColor clearColor];
self.viewController = [[YourFirstViewController alloc] initWithNibName:#"YourFirstViewController" bundle:nil];
UINavigationController *navController=[[UINavigationController alloc]initWithRootViewController:self.viewController];
self.window.rootViewController = navController;
[self.window makeKeyAndVisible];
// Override point for customization after application launch.
return YES;
}
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
ImageViewController2 *dealVC = (ImageViewController2 *)[storyboard instantiateViewControllerWithIdentifier:#"ImageViewController2"];
[self.navigationController pushViewController:dealVC animated:YES];
where ImageViewController2 is a class name
Here is the code that you should write in app delegate.
UIViewController *vc=[[UIViewController alloc]initWithNibName:#"vc1" bundle:nil];
UIView *view=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 568)];
view.backgroundColor=[UIColor redColor];
[vc setView:view];
self.navme=[[UINavigationController alloc]initWithRootViewController:vc];
self.window.rootViewController = self.navme;
For Swift 3.0, using filter:
let desiredController = self.navigationController!.viewControllers.filter { $0 is YourController }.first!
self.navigationController!.popToViewController(desiredController, animated: true)

UINavigationBar becomes transparent after switching views in uitabbar

In the (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions method, I initialized an instance of UINavigationController with GeneralOptionsTableViewController and an instance of GeneralOptionsMapViewController. The navigationBar color was set to green. However, when I switch between the UINavigationController with GeneralOptionsTableViewController and GeneralOptionsMapViewController, the navigationBar becomes transparent. I tried resetting the navigationbar color in the viewWillAppear method but it does not seem to work.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
GeneralObjectTableViewController *generalObjectTableViewController = [[GeneralObjectTableViewController alloc] init];
GeneralObjectMapViewController *generalObjectMapViewController = [[GeneralObjectMapViewController alloc]init];
UINavigationController *navigationController = [[UINavigationController alloc]initWithRootViewController:generalObjectTableViewController];
navigationController.navigationBar.barTintColor = [UIColor greenColor];
UITabBarController *tabBarController = [[UITabBarController alloc]init];
[tabBarController setViewControllers:[NSArray arrayWithObjects:navigationController, generalObjectMapViewController, nil]];
[self.window setRootViewController:tabBarController];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}

Can't add NavigationController on TableView using UISplitView

I am having a little hard time here, forgive me if you think my problem is so easy for you.
I am trying to create app using UISplitView. The 1st View on the left is a TableView and the other one on the right is just a normal view.
This is my Code for in AppDelegate.m for UISplitView.
#import "AppDelegate.h"
#import "MasterViewController.h"
#import "DetailViewController.h"
#implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
MasterViewController *masterVC = [[MasterViewController alloc]init];
DetailViewController *detailVC = [[DetailViewController alloc]init];
UISplitViewController *splitVC = [[UISplitViewController alloc]init];
[splitVC setViewControllers:[NSArray arrayWithObjects:masterVC,detailVC,nil]];
[self.window setRootViewController:splitVC];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
Now, I want to add a Navigation Bar on the TableView, I just don't know how to add if I am using SplitView,but I can when I am using a single TableView.
This is my Code in AppDelegate.m using a single View Application that uses TableView. (This is working)
#import "AppDelegate.h"
#import "ViewController.h"
#implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.viewController = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
//create UINavigationController
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:self.viewController];
self.window.rootViewController = nav;
[self.window makeKeyAndVisible];
return YES;
}
Hope you can understand what I'm trying to say. I can't post images since i don't have enough reputation. AGAIN.. The question is "How can I add Navigation Controller in my TableView if I used UISplitView?"
Do you think it will be easy for me if I use storyboards instead of using two XIB files?Hope you can help me.
Thanks in advanced!
try this code
in AppDelegate.h file
UINavigationController *detailNavigationController;
UINavigationController *masterNavigationController;
UISplitViewController *HomeSpilitView;
HomeSpilitViewController *HomeMster;
HomeDetailsViewController *HomeDetailsViewControllers;
in AppDelegate.m file
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
NSMutableArray *array = [NSMutableArray array];
HomeSpilitView = [[[UISplitViewController alloc] init]autorelease];
HomeMster = [[HomeSpilitViewController alloc] initWithNibName:#"HomeSpilitViewController" bundle:nil];
masterNavigationController = [[[UINavigationController alloc] initWithRootViewController:HomeMster] autorelease];
HomeMster.title=#"Title home";
masterNavigationController.navigationBar.tintColor =[UIColor colorWithRed:255/255.0 green:108/255.0 blue:61/255.0 alpha:0.1];
[array addObject:masterNavigationController];
HomeDetailsViewController *HomeDetailsViewControllers = [[HomeDetailsViewController alloc] initWithNibName:#"HomeDetailsViewController" bundle:nil];
detailNavigationController = [[[UINavigationController alloc] initWithRootViewController:HomeDetailsViewControllers] autorelease];
detailNavigationController.navigationBar.tintColor =[UIColor colorWithRed:255/255.0 green:108/255.0 blue:61/255.0 alpha:0.1];
HomeDetailsViewControllers.title=#"details title";
HomeMster.objHomeDetailsViewcontroller=HomeDetailsViewControllers;
HomeSpilitView.delegate = HomeDetailsViewControllers;
[array addObject:detailNavigationController];
[HomeSpilitView setViewControllers:array];
[self.window setRootViewController:HomeSpilitView];
[self.window makeKeyAndVisible];
return YES;
}

Resources