How can i connect UITableView to my Appdelegate.m? - ios

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

Related

Adding a UISegmentedControl to UINavigationBar

I try to add a UISegmentedControl to the UINavigationBar but when running, the UISegmentedControl is not showing.
here is a code
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.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
MainViewController2 *mainVC = [[MainViewController2 alloc] init];
self.window.rootViewController = mainVC;
return YES;
}
MainViewController2.m
#import "MainViewController2.h"
#interface MainViewController2 ()
#end
#implementation MainViewController2
#synthesize firstVC;
#synthesize segment;
- (void)viewDidLoad {
[super viewDidLoad];
[self initSegment];
}
-(void)initSegment{
segment.frame =CGRectMake(10,100,199,100);
segment = [[UISegmentedControl alloc]initWithItems:#[#"seg1", #"seg2", #"seg3"]];
self.navigationItem.titleView = segment;
[self.view addSubview:firstVC];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
You can directly add segment in navigation bar as a title view, using storyboard.
Here is programatically using your code:
Note: Don't forget to embed your view controller with navigation controller. (Either programatically or using storyboard)
- (void)viewDidLoad {
[super viewDidLoad];
[self initSegment];
}
-(void)initSegment{
UISegmentedControl *segment = [[UISegmentedControl alloc]initWithItems:#[#"seg1", #"seg2", #"seg3"]];
self.navigationItem.titleView = segment;
}
Add following code to your AppDelegate.m
//
// AppDelegate.m
// OBJC_Test
//
// Created by Krunal on 10/10/17.
// Copyright © 2018 Krunal. All rights reserved.
//
#import "AppDelegate.h"
#interface AppDelegate ()
#end
#implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
/*
UIViewController *viewController = [[UIViewController alloc] init];
viewController.view.frame = self.window.bounds;
viewController.view.backgroundColor = [UIColor whiteColor];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
self.window.rootViewController = navigationController;
[self.window makeKeyAndVisible];
*/
//or try this according to your code
MainViewController2 *mainVC = [[MainViewController2 alloc] init];
mainVC.view.frame = self.window.bounds;
mainVC.view.backgroundColor = [UIColor whiteColor];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:mainVC];
self.window.rootViewController = navigationController;
[self.window makeKeyAndVisible];
return YES;
}
Add this code to your AppDelegate.m in the didFinishLaunchingWithOptions function to embed a navigation controller programmatically.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
MainViewController2 *mainVC = [[MainViewController2 alloc] init];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:mainVC];
self.window.rootViewController = navigationController;
[self.window makeKeyAndVisible];
return YES;
}
Below code might be useful to you
AppDelegate.h
//Declare UINavigationcontroller
#property (strong, nonatomic) UINavigationController *navigationController;
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.window.backgroundColor = [UIColor whiteColor];
MainViewController2 *mainVC = [[MainViewController2 alloc] init];
self.navigationController = [[UINavigationController alloc] initWithRootViewController:mainVC];
self.window.rootViewController = self.navigationController;
[self.window makeKeyAndVisible];
return YES;
}
In MainViewController2.m use below code
#import "MainViewController2.h"
#interface MainViewController2 ()
#end
#implementation MainViewController2
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[self setUpSegmentalController];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)setUpSegmentalController{
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 250, 44)];
//view.backgroundColor = [UIColor redColor];
NSArray *itemArray = [NSArray arrayWithObjects: #"One", #"Two", #"Three", nil];
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:itemArray];
segmentedControl.frame = CGRectMake(0, 0, 250, 44);
segmentedControl.segmentedControlStyle = UISegmentedControlStylePlain;
[segmentedControl addTarget:self action:#selector(segmentControlAction:) forControlEvents: UIControlEventValueChanged];
segmentedControl.selectedSegmentIndex = 1;
[view addSubview:segmentedControl];
self.navigationItem.titleView = view;
}
- (void)segmentControlAction:(UISegmentedControl *)segment
{
NSLog(#"selectedSegmentIndex-----%ld",segment.selectedSegmentIndex);
if(segment.selectedSegmentIndex == 0)
{
// code for the first button
}
}
Use this code.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
MainViewController2 *mainVC = [[MainViewController2 alloc] init];
self.window.rootViewController = mainVC;
[self.window makeKeyAndVisible];
return YES;
}

How to programmatically create UIView so that it does not overlap the status bar

I am using the following code in my app delegate to programmatically create the application's window and root controller:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.rootViewController = [[UIViewController alloc] init];
self.window.rootViewController.view = [[UIView alloc] init];
self.window.rootViewController.view.backgroundColor = [UIColor redColor];
return TRUE;
}
The code works fine, however the view associated to the root view controller overlaps the status bar, which I would like to avoid (see picture below).
How should I proceed? In particular I want to have a lightgray background for the status bar (similar to the apps created with a storyboard) and also handle orientation change correctly.
Thanks
Well, you can do something as following:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.rootViewController = [[UIViewController alloc] init];
self.window.rootViewController.view.backgroundColor = [UIColor whiteColor];
UIView *redView = [[UIView alloc] initWithFrame:CGRectMake(0,
[UIApplication sharedApplication].statusBarFrame.size.height,
[UIScreen mainScreen].bounds.size.width,
[UIScreen mainScreen].bounds.size.height)];
redView.backgroundColor = [UIColor redColor];
[self.window.rootViewController.view addSubview:redView];
[self.window makeKeyAndVisible];
return TRUE;
}
should look like this:

New UIViewController frame is 320x480 instead of larger screen size

I updated Xcode today.
AppDelegate.m:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.vc = [[ViewController alloc] init];
UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:self.vc];
self.window.rootViewController = nc;
[self.window makeKeyAndVisible];
return YES;
}
ViewController.m:
- (void)viewDidLoad {
[super viewDidLoad];
CGRect f = self.view.frame;
f =[[UIScreen mainScreen] bounds];
self.view.frame = f;
self.view.backgroundColor = [UIColor redColor];
}
The frame is showing as 320x480. I can clearly see more space around the view, but it's black. Why is the view not filling out? There is no XIB.

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

Recursive calls to viewDidLoad, crashing

in loggerViewController.m:
- (void)viewDidLoad
{
[super viewDidLoad];
UIView* mainView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
[self.view addSubview:mainView]; // <-- Problem is here
}
loggingViewController is an iVar of my appDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
.
.
loggingViewController = [[loggerViewController alloc] init];
[loggingViewController.view setBackgroundColor:[UIColor blueColor]];
// [loggingViewController loadView];
[self.view addSubview:loggingViewController.view];
}
I was expecting my AppDelegate to call loggingViewController, which in turn, set up it's own subviews inside and it would be done. But instead the viewDidLoad gets called recursively I don't understand why?
Try it like this,
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
loggingViewController = [[loggerViewController alloc] init];
self.window.rootViewController = loggingViewController;
[self.window makeKeyAndVisible];
}
The reason for recursive calling is that your self.view is nil and hence it is try to call again and again when you are trying to add it as a subview of appdelegate's view.
- (void)viewDidLoad
{
[super viewDidLoad];
UIView* mainView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
[self.view setBackgroundColor:[UIColor blueColor]];
[self.view addSubview:mainView];
}

Resources