Xcode 4 won't show app's main screen - ios

After switching to Xcode 4, I thought I would be able to build and run my application exactly like I could in Xcode 3.
Turns out I can't.
Xcode 4 has a funny way of never showing the app's view controller, which is strange.
I can tell Apple's going to eventually force us to switch, which will result in my app being inoperable.
It gets to application:didFinishLaunchingWithOptions: without any errors, before hanging. Eventually the application crashes on the device - but stays on Default.png forever in the simulator.
I thought I could go and edit the application:didFinishLaunchingWithOptions: method the instantiate an instance of the view controller itself and add it to the window - only to reveal that didn't work either.
After numerous failed attempts at this - creating separate UIWindows for the main view controller - I decided the add it to a navigation controller.
Luck then struck me - but only in the simplest of forms. I looked in the log and see that applicationDidBecomeActive: had been called.
But, as usual, no such luck with any sort of view being displayed.
I then decided to see if I could add a UIView with a blue background color and a few UI elements (buttons, labels, etc...) to the window and see if that would work.
Funnily enough it did.
But why not for the main view controller? Not once in Xcode 4 have I successfully had it run my app (even opening it after it has been built fails). I've tried changing the compiler to the same as in Xcode 3, no luck.
I am honestly really confused as to why the application's view controller won't display.
For anyone who wants to give it an attempt as to why it isn't working that would be gratefully appreciated.
Here's the code for the AppDelegate, if you need the code for the view controller I can paste it here, however it's over 2000 lines.
Anyway, here's the .m file:
#import "DocumentationAppDelegate.h"
#import "DocumentationViewController.h"
#implementation DocumentationAppDelegate
#synthesize window;
#synthesize viewController;
#synthesize navigationController;
- (void)applicationWillEnterForeground:(UIApplication *)application {
NSLog(#"In method %#, which is in class %#.", NSStringFromSelector(_cmd), NSStringFromClass([self class]));
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
NSLog(#"In method %#, which is in class %#.", NSStringFromSelector(_cmd), NSStringFromClass([self class]));
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
NSLog(#"In method %#, which is in class %#.", NSStringFromSelector(_cmd), NSStringFromClass([self class]));
DocumentationViewController *vc = [[DocumentationViewController alloc] init];
UINavigationController *controller = [[UINavigationController alloc] initWithRootViewController:vc];
controller.navigationBarHidden = YES;
UIWindow *win = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[win addSubview:controller.view];
[win makeKeyAndVisible];
return YES;
}
- (void)applicationWillTerminate:(UIApplication *)application {
}
- (void)dealloc {
[window release];
[super dealloc];
}
#end
and the .h
#import <UIKit/UIKit.h>
#class DocumentationViewController;
#interface DocumentationAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
DocumentationViewController *viewController;
UINavigationController *navigationController;
}
#property (nonatomic, retain) IBOutlet UINavigationController *navigationController;
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet DocumentationViewController *viewController;
#end
If any one could help me here it would be tremendously appreciated.

Your app delegate already has the properties window, viewController and navigationController. So you can have the application:didFinishLaunchingWithOptions: method like this,
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
viewController = [[DocumentationViewController alloc] init];
navigationController = [[UINavigationController alloc] initWithRootViewController:vc];
navigationController.navigationBarHidden = YES;
[self.window addSubview:navigationController.view];
[self.window makeKeyAndVisible];
return YES;
}

Related

iOS - pushViewController not working after conversion to xCode 9 / iOS 11

I saw my iOS app isn't allowed to be downloaded from the app store with the most recent upgrade to iOS 11, so I'm trying to update it. I have it in Xcode 9, and I've managed to now get the app started on my iPhone 6. However, I can't seem to get it to display after my initial screen display.
For example, the initial display successfully displays and shows a button called "start". That should pop up another display when you press it, but it doesn't (although it is reaching the code which displays it).
Here's the code, in a class named "StartController.m", which should be calling up the display:
NSLog(#"nav controller = %#", self.navigationController);
// push question controller onto the stack
[self.navigationController pushViewController:questionController animated:YES];
// the nav controller owns it now, we can release it
[questionController release];
Debugging shows this code is successfully called when the button is pressed, but after the release of questionController, nothing happens.
Here's the declaration of the questionController in StartController.h:
#interface StartController : UIViewController <ADBannerViewDelegate>{
QuestionController *questionController;
And here's the declaration of the same in StartController.m:
#implementation StartController
#synthesize questionController, settingsController, nextLevelViewController, appState;
The initial view (with the "start" button) is successfully displayed with this code in the "didFinishLaunchingWithOptions" method of the app delegate:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
// create the start controller
self.startController = [[StartController alloc] init];
// transfer to it - not animated because it's the first view
[navController pushViewController:startController animated:NO];
// add the current nav controller view to the window view heirarchy
// [window addSubview:navController.view];
[self.window setRootViewController:startController];
// release becasause now the navController has it
[startController release];
// show the start controller
[self.window makeKeyAndVisible];
return YES;
}
For reference, here's the declaration of the navController in the app delegate header:
#interface QuizAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
UINavigationController *navController;
StartController *startController;
}
#property (readonly) BOOL iPad;
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) StartController *startController;
#property (nonatomic, retain) IBOutlet UINavigationController *navController;
And here's the declaration in the implementation:
#implementation QuizAppDelegate
#synthesize window;
#synthesize startController;
#synthesize navController;
I tried not to post too much code for fear of obscuring the problem, but I'll add it if requested. Any ideas what the problem might be?

iOS - Tabbed aplication with table view and subview

I have one problem. in my app (it is tabbed style), I have one viewcontroller with some text and second with table view (RSS reader). When I have just the RSS and it is set to single view app, subview form rss works, but when I set up the tabbed app and click to some post in table view, subview didnt show up... Can anybody help me please?
Here are my codes:
AppDelegate.h
#import <UIKit/UIKit.h>
#interface MWFeedParserAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
UINavigationController *navigationController;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet UINavigationController *navigationController;
#end
AppDelegate.m
#import "MWFeedParserAppDelegate.h"
#import "ViewController1.h"
#import "RootViewController.h"
#implementation MWFeedParserAppDelegate
#synthesize window;
#synthesize navigationController;
#pragma mark -
#pragma mark Application lifecycle
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after app launch
UITabBarController *tbc = [[UITabBarController alloc]init];
ViewController1 *vc1 = [[ViewController1 alloc]init];
RootViewController *vc2 = [[RootViewController alloc]init];
[vc1.tabBarItem setTitle:#"Tab1"];
[vc2.tabBarItem setTitle:#"Tab2"];
[tbc setViewControllers:[NSArray arrayWithObjects:vc1, vc2, nil]];
[window addSubview:[navigationController view]];
[window makeKeyAndVisible];
[window setRootViewController:tbc];
return YES;
}
- (void)applicationWillTerminate:(UIApplication *)application {
// Save data if appropriate
}
#pragma mark -
#pragma mark Memory management
- (void)dealloc {
[navigationController release];
[window release];
[super dealloc];
}
#end
From the dealloc, I see you are not using arc.
You have some memory leaks; be sure to release vc1 and vc2 in your didFinishLaunchingWithOptions, the tab bar controller will retain them.
You probably don't need navigationController property, recommend you delete that until you know you'll need it.
I think you'll want to add your RSS view (vc2?) to a nav controller before adding to the tab bar controller like this:
[tbc setViewControllers:[NSArray arrayWithObjects:vc1, [[[UINavigationController alloc] initWithRootViewController:vc2] autorelease], nil]];
And delete this line:
[window addSubview:[navigationController view]];
Best of luck!!
edit Spelled out a tad more:
ViewController1 *vc1 = [[[ViewController1 alloc] init] autorelease];
RootViewController *vc2 = [[[RootViewController alloc] init] autorelease];
UINavigationController *navController = [[[UINavigationController alloc] initWithRootViewController:vc2] autorelease];
UITabBarController *tbc = [[[UITabBarController alloc] init] autorelease];
[tbc setViewControllers:#[vc1, navController]];
[window makeKeyAndVisible];
[window setRootViewController:tbc];

how to instantiate my ViewController in iOS 3.1? (it already works in iOS 4.2.1)

Using Xcode 4.2, my app runs in iOS 5.0 simulator. It runs on a 3G iPhone with iOS 4.2.1. It does not run on an iPod with iOS 3.1.3.
This is boilerplate code I got from any number of tutorials, but on the iOS 3.1.3 device, after displaying my Default.png, this line fails:
self.window.rootViewController = self.viewController;
with 'Unrecognized selector sent to instance' in my ykAppDelegate.m here:
#import "ykAppDelegate.h"
#import "ykViewController.h"
#implementation ykAppDelegate
#synthesize window;
#synthesize viewController;
#pragma mark -
#pragma mark Application lifecycle
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
// Set the view controller as the window's root view controller and display.
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
With a more than cursory glance at the code, I notice viewController isn't apparently instantiated (except for #synthesize); it's just declared in my ykAppDelegate.h:
#import <UIKit/UIKit.h>
#class ykViewController;
#interface ykAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
ykViewController *viewController;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet ykViewController *viewController;
#end
Is there a small tweak I can make so this will work in iOS 3.1?
In pre-iOS5 operating systems UIWindow doesn't have a property named 'rootViewController'. An idiomatic solution is to simply add the view controller's view as a subview to your application's key window:
[window addSubview:self.myViewController.view];
This worked for me
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
// Set the view controller as the window's root view controller and display.
[window addSubview:self.viewController.view];
[self.window makeKeyAndVisible];
return YES;
}

Basic error in iPad application launch

I have a file named MMAppDelegate.m:
#import "MMAppDelegate.h"
#implementation MMAppDelegate
#synthesize window;
#synthesize viewController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
viewController = [[MainViewController alloc] init];
[window addSubview:viewController.view];
// Override point for customization after application launch.
[window makeKeyAndVisible];
return YES;
}
//and more standard methods
MMAppDelegate.h:
#import <UIKit/UIKit.h>
#import "MainViewController.h"
#interface MMAppDelegate : UIResponder <UIApplicationDelegate> {
UIWindow *window;
MainViewController *viewController;
}
#property (nonatomic, retain) UIWindow *window;
#property (nonatomic, retain) MainViewController *viewController;
#end
MainViewController.m:
#import "MainViewController.h"
#implementation MainViewController
- (void)viewDidLoad {
NSLog(#"view did load main view controller");
[super viewDidLoad];
self.view.backgroundColor = [UIColor redColor];
}
#end
And yet when I run the program, I get a black screen, and the output:
2012-02-12 15:44:48.160 MoreMost[44076:f803] view did load main view controller
2012-02-12 15:44:48.163 MoreMost[44076:f803] Applications are expected to have a root view controller at the end of application launch
What is the problem?
Change this line:
[window addSubview:viewController.view];
to this:
window.rootViewController = viewController;
And you'll need to create the window, like this:
window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
You don't need/want to add the view controller's view as a subview. You want to make your controller the root view controller and iOS will take care of the rest:
window.rootViewController = viewController;
That is, as of iOS 4. (Not sure the answer if you're going back earlier than that.)
You also need to create the window before you use it. Something like
window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];
Not sure if you aren't or if you just didn't show it ...

Why does my iOS app crash when trying presentModalViewController?

I've been banging my head against this all day, it seems like something simple but I can't figure it out.
I've got an iOS app that I created using the "View-based Application" template in XCode. Here is essentially the code I have:
AppDelegate.h:
#import <UIKit/UIKit.h>
#interface AppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
MainViewController *viewController;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet MainViewController *viewController;
#end
AppDelegate.m:
#import "AppDelegate.h"
#import "MainViewController.h"
#implementation AppDelegate
#synthesize window, viewController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[self.window addSubview:viewController.view];
[self.window makeKeyAndVisible];
return YES;
}
- (void)dealloc {
[viewController release];
[window release];
[super dealloc];
}
#end
MainViewController.h:
#import <UIKit/UIKit.h>
#interface MainViewController : UIViewController {
}
-(IBAction) button:(id)sender;
#end
MainViewController.m:
#import "MainViewController.h"
#import "ModalViewController.h"
#implementation MainViewController
...
-(IBAction) button:(id)sender {
ModalViewController *mvc = [[[ModalViewController alloc] initWithNibName:NSStringFromClass([ModalViewController class]) bundle:nil] autorelease];
[self presentModalViewController:mvc animated:YES];
}
#end
There's nothing of interest in the ModalViewController.
So the modal view should display when the button is pressed. When I press the button, it hangs for a second then crashes back to the home screen with no error message.
I am stumped, please show me what I'm doing wrong!
There's nothing of interest in the ModalViewController.
Although there may not be anything of interest, there could still be something causing the problem.
Does your View Controller override the function loadView?
A problem that has got me a few times is if you don't call [super loadView]; first in your overriden loadView method. Not doing this causes a crash when moving into that View Controller.
Good luck in trying to solve this one!
N

Resources