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];
}
Related
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;
}
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.
I am facing some issues w.r.t XCode 6.1.1 , here is my code
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[[DBManager getSharedInstance] initWithdbFile];
[[DBManager getSharedInstance] createtableScripts];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
containerViewController =[[CprViewController alloc] init];
[containerViewController Setup];
[self.window setRootViewController:containerViewController.containerViewController];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
[self pickModel];
return YES;
}
Now the app works fine in older XCode i.e 5.1.1 but while simulating on XCode 6.1.1 the app launches the Launch image and there is no response i.e the execution ends at [self.window setRootViewController:containerViewController.containerViewController]; and it doesnt proceed to execute further lines of code. It shows no error in console. (The app however runs on device having latest OS (i.e iOS 8). Please help.
*Edited Post -- begins here
Thanks for helping out. Here’s the problem that i am facing,
This is in my AppDelegate file
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[[DBManager getSharedInstance] initWithdbFile];
[[DBManager getSharedInstance] createtableScripts];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; //Now Removed!!!
containerViewController =[[CprViewController alloc] init];
[containerViewController Setup];
self.window.rootViewController = containerViewController.containerViewController;
self.window.backgroundColor = [UIColor whiteColor];//Now Removed!!!
[self.window makeKeyAndVisible];//Now Removed!!!
[self pickModel];
return YES;
}
Now i have removed the three lines just as you suggested and now the execution continues
Now this is my code for [containerViewController Setup];
-(void)Setup
{
SideMenuViewController *leftMenuViewController = [SideMenuViewController homeBookViewController];
CprStartPageViewController *accounts=[[CprStartPageViewController alloc] initWithNibName:#"CprStartPageViewController" bundle:nil];
self.navController= [[CprNavigationViewController alloc]
initWithRootViewController:accounts];
self.containerViewController = [MFSideMenuContainerViewController containerWithCenterViewController:self.navController leftMenuViewController:leftMenuViewController
rightMenuViewController:nil];
[leftMenuViewController.passBookTable selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:YES scrollPosition:0];
}
The above code attaches a side menu (here i am attaching a left side menu) - No error in this code i.e the execution continues.
Now CprLoginViewController is my parentViewController
i.e the parentViewController for the 4 screen that i had mentioned and this is my initWithNibName for the 4 screens
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil parentViewController:(CprLoginViewController*) inVC
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil parentViewController:inVC];
if (self) {
// Custom initialization
}
return self;
}
and the error now occurs in CprLoginViewController where i am using a procedure called showNextPage to navigate between these 4 pages
-(void) showNextPage
{
if (currentPage > numberPages)
return;
CprLoginSubViewController *controller;
switch(currentPage)
{
case 0:
controller = [[Corppersondtl alloc] initWithNibName:#"Corppersondtl" bundle:nil parentViewController:self];
break;
case 1:
controller = [[CprLoginSub1ViewController alloc] initWithNibName:#"CprLoginSub1ViewController" bundle:nil parentViewController:self];
break;
case 2:
controller = [[CprLoginSub2ViewController alloc] initWithNibName:#"CprLoginSub2ViewController" bundle:nil parentViewController:self];
break;
case 3:
controller = [[CprLoginSub3ViewController alloc] initWithNibName:#"CprLoginSub3ViewController" bundle:nil parentViewController:self];
break;
case 4:
controller = [[CprLoginSub4ViewController alloc] initWithNibName:#"CprLoginSub4ViewController" bundle:nil parentViewController:self];
default:
break;
}
CGRect rBounds;
// add the required controller's view to scroll view
if (controller.view.superview == nil)
{
CGRect frame = self.scrollView.frame;
frame.origin.x = CGRectGetWidth(frame) * (currentPage);
frame.origin.y = 0;
controller.view.frame = frame;
[self addChildViewController:controller];
[self.scrollView addSubview:controller.view];
[controller didMoveToParentViewController:self];
// just scroll to appropriate page
rBounds = self.scrollView.bounds;
rBounds.origin.x = CGRectGetWidth(rBounds) * currentPage;
[self.scrollView scrollRectToVisible:rBounds animated:YES];
}
NSLog(#"scrollView is %lf", self.scrollView.frame.origin.y);
viewOriginalbounds = rBounds; //self.scrollView.frame;
currentPage++;
}
In the above code i am attaching a scrollview to scroll the screen upwards when keyboard appears for text inputs.
Now the line if (controller.view.superview == nil) is throwing exc_bad_access error.
Just delete that lines of code
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
I have created two ViewCrontroller using xib and set orientation of UIViews is LandscapeLeft.
and than add this code to
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
The following is my ideal result:
I'd like the window under the status bar in ios 7,
The first view, the result is what I want to get, however, when it jumps to the second view, there is a problem! The window move 20px to the left. how to fit it into the normal view?
3.Most impornatant condition is Autolayout is "TRUE".
my testing code:
https://www.dropbox.com/s/bj9wg4hwar1t8y0/statusbartesting.zip
Can anyone help to solve this problem?
You don't need to change window frame in AppDelegate.
I'll tell you code, please follow it
Appdelegate.m
- (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];
[self.window makeKeyAndVisible];
self.mxfirstciewcontroller = [[firstViewController alloc] initWithNibName:#"firstViewController" bundle:nil];
self.window.rootViewController = [[UINavigationController alloc] initWithRootViewController:self.mxfirstciewcontroller];
[(UINavigationController *)self.window.rootViewController setNavigationBarHidden:YES];
return YES;
}
In firstViewController.m
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[self.view setFrame:CGRectMake(0, 20, self.view.frame.size.width, self.view.frame.size.height)];
}
In secondViewController.m
- (void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
[self.view setFrame:CGRectMake(20, 0, self.view.frame.size.width, self.view.frame.size.height)];
}
- (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];
}