self.window setRootViewController not working on xcode 6.1.1 - ios

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

Related

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.

Customize View Controller Stack on didFinishLaunching

I am trying to present a tutorial/login/introduction view controller for my app on first launch using the following code:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
UIViewController *firstController = [[UIViewController alloc] init];
[self.window setRootViewController:firstController];
if ([self shouldShowIntro]) {
IntroViewController *introViewController = [[IntroViewController alloc] init];
[firstController presentViewController:introViewController animated:NO completion:nil];
}
return YES;
}
This works fine, but there is an annoying visual effect that takes place... Before you see the IntroViewController, there is a split second where the firstController is visible. I tried presenting the IntroViewController before setting the rootViewController of the window, but this (not surprisingly) results in the following warning:
Warning: Attempt to present <IntroViewController: 0x7fd8eb3362f0> on <UIViewController: 0x7fd8eb335180> whose view is not in the window hierarchy!
How can I modally present the IntroViewController without this annoying visual flash? I want IntroViewController to already be showing after the launch screen disappears and be able to be dismissed modally.
The following works for me with no flicker running on iOS 8.1 on an iPhone 6 and in the simulator. I used SDK version 8.1 and I think your SDK level is different because I got different warnings and results than you using your provided code.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
UIViewController *firstController = [[UIViewController alloc] init];
firstController.view.backgroundColor = [UIColor blueColor];
[self.window setRootViewController:firstController];
dispatch_async(dispatch_get_main_queue(), ^{
UIViewController* modalViewController = [[UIViewController alloc] init];
modalViewController.view.backgroundColor = [UIColor grayColor];
[firstController presentViewController: modalViewController animated: NO completion: nil];
});
// dismiss after a few seconds..
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[firstController dismissViewControllerAnimated: YES completion: nil];
});
return YES;
}
Update with MMDrawerController:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
MMDrawerController *drawerController = [[MMDrawerController alloc] init];
drawerController.centerViewController = [[UIViewController alloc] init];
drawerController.centerViewController.view.backgroundColor = [UIColor blueColor];
drawerController.rightDrawerViewController = [[UIViewController alloc] init];
drawerController.rightDrawerViewController.view.backgroundColor = [UIColor greenColor];
drawerController.openDrawerGestureModeMask = MMOpenDrawerGestureModeAll;
drawerController.closeDrawerGestureModeMask = MMOpenDrawerGestureModeAll;
[self.window setRootViewController:drawerController];
dispatch_async(dispatch_get_main_queue(), ^{
UIViewController* modalViewController = [[UIViewController alloc] init];
modalViewController.view.backgroundColor = [UIColor grayColor];
[drawerController presentViewController: modalViewController animated: NO completion: nil];
});
// dismiss after a few seconds..
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[drawerController dismissViewControllerAnimated: YES completion: nil];
});
return YES;
}
Also, per you comment it sounds as if having the pre-loaded view is key. Try invoking [firstController view] and [modalController view] before you present to ensure they're loaded.
try moving this line below the code where you set your view controllers.
[self.window makeKeyAndVisible];
something like this:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
UIViewController *firstController = [[UIViewController alloc] init];
[self.window setRootViewController:firstController];
if ([self shouldShowIntro]) {
IntroViewController *introViewController = [[IntroViewController alloc] init];
[firstController presentViewController:introViewController animated:NO completion:nil];
}
[self.window makeKeyAndVisible];
return YES
}
Put
if ([self shouldShowIntro]) {
IntroViewController *introViewController = [[IntroViewController alloc] init];
[firstController presentViewController:introViewController animated:NO completion:nil];
}
in to the viewDidAppear function of FirstViewController rather than in the AppDelegate. That should suppress the warning and perform the operation smoothly.

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

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

IOS UINavigationController, pushViewController not working

Good evening,
I currently have two UIViewControllers. My appDelegate looks like this
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
struct CGRect rect = [[UIScreen mainScreen] bounds];
rect.origin.x = rect.origin.y = 0.0f;
_viewController = [[sandboxViewController alloc] init];
UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:_viewController];
_window = [[UIWindow alloc] initWithFrame:rect];
[_window makeKeyAndVisible];
[_window addSubview:nc.view];
return YES;
}
The viewController looks like this:
- (void)loadView {
self.view = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 480.0f)];
self.view.backgroundColor = [UIColor whiteColor];
self.navigationItem.title = #"Master View";
}
- (void)viewDidLoad
{
[super viewDidLoad];
UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeInfoLight];
[infoButton addTarget:self action:#selector(switchView:) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:infoButton];
}
- (void)switchView:(id)obj {
if(![self navigationController])
NSLog(#"navigationController IS NIL!!!");
if(!_secondView)
_secondView = [[secondViewController alloc] init];
[self.navigationController pushViewController:_secondView animated:YES];
}
By clicking on the info button, that was added to the right side on the navigation bar, I want to switch to the secondView. This, however, is not happening because navigationController logs as nil ! What am I missing here?
Any help is truly appreciated!
You don't have to create the window, it should already exist.
//_window = [[UIWindow alloc] initWithFrame:rect]; //remove this line
[self.window makeKeyAndVisible]; //use the ivar
[self.window addSubview:nc.view];

Resources