How to present a (no nib) ViewController - objective-c

I've created a ViewController (without nib files), so how do I load this viewController in the app delegtate? my current code is:
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
viewController = [[MyCustomViewController alloc] init...]; // I use a custom init method
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
And how can I use the presentModalViewController method to show new view controllers of the same "CustomViewController" class?
THANKS for the help!
NOTE: By the way, I do see that this code does call my initialization method, and my ViewDidLoad method in my customViewController is being called however the screen is still black....

It looks like your properties are not set up correctly.
Check this line:
viewController = [[MyCustomViewController alloc] init...]; // I use a custom init method
self.window.rootViewController = self.viewController;
check that self.viewController is okay, and there's nothing stopping you from just doing:
self.window.rootViewController = viewController;

Whoops, I was just missing this code in my viewDidLoad Method:
self.view.frame = CGRectMake(0, 0, 320, 480);
self.view.backgroundColor = [UIColor whiteColor];
So everything was working, but the screen by default is black and without a frame.

I had to do just that in one of the apps I'm working on. Try typecasting your view controller
CDViewController *viewController = (CDViewController*)self.window.rootViewController;
viewController.managedObjectContext = self.managedObjectContext;
viewController.managedObjectModel = self.managedObjectModel;
// Override point for customization after application launch.
return YES;

Related

Second UIScreen causing problems with UIStoryboards

In my app delegate i set up another screen (for airPlay).
When i create a second window and assign the Second screen to it and run the app the TableView in my storyboard turns to a black colour. Its as if the tableView is not rendering correctly. I have isolated the problem to :
self.HDTVwindow.screen=[[UIScreen screens] objectAtIndex:1];
where the HDTVWindow is the second window for my airplay app. When i comment out this code the storyboard runs fine and my UITableView is nice and white. Am i confusing the storyboard by making two UIWindows in the appDelegate, even though they are assigned to different screens??
For my appDelegate see below.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
if ([[UIScreen screens] count]>1)
{
CGRect frame = [[UIScreen screens]objectAtIndex:1].bounds;
self.HDTVwindow = [[UIWindow alloc] initWithFrame:frame];
UIImage* astonLogo=[UIImage imageNamed:#"AstonUni720p.png"];
UIImageView* astonLogoView=[[UIImageView alloc] initWithImage:astonLogo];
astonLogoView.frame=frame;
[self.HDTVwindow addSubview:astonLogoView];
self.HDTVwindow.hidden = NO;
self.HDTVwindow.screen=[[UIScreen screens] objectAtIndex:1];//PROBLEM!!
}
// Set Up Storyboard
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UIViewController *mainViewController = [storyboard instantiateInitialViewController];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.rootViewController = mainViewController;
[self.window makeKeyAndVisible];
return YES;
}
The problem disapears when ran with the iPad and an apple TV, it seems there might a bug in the simulator. In future i shall always run my code on real ios devices.

Thread 1: EXC_BREAKPOINT (code=EXC_i386_BPT, subcode=0x0) error

I have an iPad app I am making, but it crashes on startup even though there are no errors or warnings, the output doesn't output anything besides "(lldb)", and it marks a pointer.
This is the image of the post build crash.
And here is the code:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
/* UIImage *myImage = [UIImage imageNamed:#"de_dust2.png"];
myImageView = [[UIImageView alloc] initWithImage:myImage];
myScrollView.contentSize = CGSizeMake(myImageView.frame.size.width, myImageView.frame.size.height);
myScrollView.maximumZoomScale = 4.0;
myScrollView.minimumZoomScale = 0.75;
myScrollView.clipsToBounds = YES;
myScrollView.delegate = self;
// [myScrollView addSubview:myImageView];*/
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.viewController = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
[self.viewController setRed:1];
[self.viewController setGreen:0];
[self.viewController setBlue:0];
[self.viewController setAlpha:1];
[self.viewController checkRotation];
return YES;
}
I now also noticed that the error was given at the [self.window makeKeyAndVisible]; line.
Your code looks ok, the crash could be caused by some exception thrown parsing the XIB file of the window ...
Take a look at the Window in Interface Builder and try to temporary remove all reference to any IBOutlet present in the View Controller ...

Status bar rotates separated from UIWindow

I have to create application with two UIWindow (please, don't ask why). First UIWindow's rootViewController supports all orientations. Second one - only portrait and upside down. So application delegate code looks like:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// part one
self.windowOne = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
self.windowOne.rootViewController = [[ViewControllerOne alloc] init]; // supports all orientations
self.windowOne.rootViewController.view = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"image1.png"]] autorelease];
[self.windowOne makeKeyAndVisible];
//~part one
// part two
self.windowTwo = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
self.windowTwo.rootViewController = [[ViewControllerTwo alloc] init]; // supports only portrait and upside down
self.windowTwo.rootViewController.view = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"image2.png"]] autorelease];
[self.windowTwo makeKeyAndVisible];
//~part two
return YES;
}
If only one of two parts is active (second one is commented) - everything is ok. But if windowOne has become key and visible before windowTwo, windowTwo rotates as ViewControllerTwo allows, but status bar behaves really weird: it rotates like windowOne is key and visible.
Is there any option to make status bar rotating as ViewControllerTwo says?
Albeit its a hack to me but what you could try is the following:
In your view controllers shouldAutorotateToInterfaceOrientation (for iOS5) or shouldAutorotate (as for iOS6) method add the following code line:
[[UIApplication sharedApplication] setStatusBarOrientation: interfaceOrientation animated: YES];
and test out how the app behaves after that.
to get the interface-orientation in the (new for iOS6) shouldAutorotate method do the following:
UIInterfaceOrientation interfaceOrientation = [[UIDevice currentDevice] orientation];
I didn't test this all out for myself, but it could work.
(Check out some post on how to support autorotation under iOS6)

App crash with "Unable to restore previously selected frame" message

I can't figure out why that code leads to app crash.
AppDelegate.h
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
self.rootViewController = [[[RootViewController alloc]init]autorelease];
[self.window setRootViewController:self.rootViewController];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
Here is RootViewController.m code
-(void)loadView
{
UIView *view = [[UIView alloc]initWithFrame:CGRectMake(10, 10, 10, 10)];
[view setBackgroundColor:[UIColor lightGrayColor]];
[self.view addSubview:view];
[view release];
}
I get that message in the debugger
Unable to restore previously selected frame.
loadView is supposed to set the view. It is called when self.view is nil. Now you're calling [self.view addSubview:view]; UIKit calls loadView, and that creates an infinite recursion. You're supposed to do self.view = view; here.
loadView is responsible to set the view first. you missed to do that. Instead you added a view to self.view.
Change the code by below line:
self.view = view;
instead of [self.view addSubview:view];
Also it is advisable to call [super loadView] before returning from the function.

Where to declare ViewControllers for a custom tab view?

I'm trying to figure out how to use a custom tab view I found called JMTabView by Jason Morrissey on GitHub. (I tried asking him directly but got no response.)
I know how to programmatically create a UITabBarController and assign view controllers. What I can't figure out is where to declare my four UITableViewControllers and other VC's for each of the four tabs in this example. The code:
// TabDemoAppDelegate.m -> do I declare them here?
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
TabDemoViewController * demoViewController = [[[TabDemoViewController alloc] initWithNibName:nil bundle:nil] autorelease];
self.navigationController = [[[UINavigationController alloc] initWithRootViewController:demoViewController] autorelease];
//[self.navigationController setNavigationBarHidden:YES];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[self.window addSubview:self.navigationController.view];
[self.window makeKeyAndVisible];
return YES;
}
// TabDemoViewController.m -> or somewhere in here?
-(void)addCustomTabView; { // this is a private method
JMTabView * tabView = [[[JMTabView alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height - 60., self.view.bounds.size.width, 60.)] autorelease];
tabView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleWidth;
[tabView setDelegate:self];
UIImage * standardIcon = [UIImage imageNamed:#"icon3.png"];
UIImage * highlightedIcon = [UIImage imageNamed:#"icon2.png"];
CustomTabItem * tabItem1 = [CustomTabItem tabItemWithTitle:#"One" icon:standardIcon alternateIcon:highlightedIcon];
CustomTabItem * tabItem2 = [CustomTabItem tabItemWithTitle:#"Two" icon:standardIcon alternateIcon:highlightedIcon];
CustomTabItem * tabItem3 = [CustomTabItem tabItemWithTitle:#"Three" icon:standardIcon alternateIcon:highlightedIcon];
CustomTabItem * tabItem4 = [CustomTabItem tabItemWithTitle:#"Four" icon:standardIcon alternateIcon:highlightedIcon];
[tabView addTabItem:tabItem1];
[tabView addTabItem:tabItem2];
[tabView addTabItem:tabItem3];
[tabView addTabItem:tabItem4];
[tabView setSelectionView:[CustomSelectionView createSelectionView]];
[tabView setItemSpacing:1.];
[tabView setBackgroundLayer:[[[CustomBackgroundLayer alloc] init] autorelease]];
[tabView setSelectedIndex:0];
[self.view addSubview:tabView];
}
He mentions blocks... if they're relevant, what are they and is this where I would declare the VC's? If so, how?
// You can run blocks by specifiying an executeBlock: paremeter
// #if NS_BLOCKS_AVAILABLE
// [tabView addTabItemWithTitle:#"One" icon:nil executeBlock:^{NSLog(#"abc");}];
// #endif
I'm not familiar with that particular library, but if it follows a similar pattern to UITabViewController I would create properties for your tab (if you need to reference them later) in your App Delegate's .h file and then create your instances in the .m. If you are just placing the tabs and don't need to reference them directly anymore you should be able to define them in the .m (probably in applicationDidFinishLaunching) and then assign them as tabs and let the tab controller take it from there.