self.window.rootViewController = self.someController not working? - objective-c

I came across this post here (link below) and it states that you can replace:
[window addSubview: someController.view];
with:
self.window.rootViewController = self.someController;
My Base SDK for All Configurations is set to Latest iOS (currently set to iOS 4.2), however when I try to build the project I get this error: Request for member, 'mainMapView' in something not a structure or union.
Adding it with the commented out addSubview: works fine though.
This is the code in question...
#import "MakeView2AppDelegate.h"
#import "MainMap.h"
#implementation MakeView2AppDelegate
#synthesize window;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
MainMap *mainMapView = [[MainMap alloc] initWithNibName:nil bundle:nil];
//[self.window addSubview:mainMapView.view];
self.window.rootViewController = self.mainMapView;
[self.window makeKeyAndVisible];
return YES;
}
self.window.rootViewController vs window addSubview

This is because you're looking for mainMapView as a property of the app delegate. Change that line to:
self.window.rootViewController = mainMapView;
This is assuming that the MapView class inherits from UIViewController, however. Does it?

In your example, you are calling self.mainMapView, but unless mainMapView is a property on the class, this won't work. If you remove the "self." from it, it will work fine.

Since iOS4 this has been the default behaviour in the templates in Xcode4. It's probably better to use addSubview for backwards compatibility as the other method does not work with iPhone OS 3.x

you have not create property for mainMapView.
so you can not write self.mainMapView
you have to use mainMapView.
for more help see this :
Understanding your (Objective-C) self
http://useyourloaf.com/blog/2011/2/8/understanding-your-objective-c-self.html

Related

Xcode - UIStoryboard black screen

I am using a UIStoryboard. I created my application empty, and then I added a storyboard. The storyboard is set as the main storyboard in the project settings and also as the 'Main Storyboard file base name' in the Info.plist.
My app delegate simply returns YES without any other code.
My main.m includes this line of code:
return UIApplicationMain(argc, argv, nil, NSStringFromClass([ARGAppDelegate class]));
I get this message in the console:
Application windows are expected to have a root view controller at the
end of application launch
My view controller is in the UIStoryboard, as the initial view controller.
Just wondering what I am doing wrong here...
My AppDelegate code:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
return YES;
}
Thats all the code I have, if you are using a storyboard you do not need to create a window right?
Thanks for the help, appreciated!
Update:
I actually changed my AppDelegate code, still does not make a difference:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard_iPhone" bundle:[NSBundle mainBundle]];
UIViewController *initViewController = [storyboard instantiateViewControllerWithIdentifier:#"ARGViewControllerID"];
NSLog(#"%#", initViewController); // Returns a valid ARGViewController object
[self.window setRootViewController:initViewController];
[self.window makeKeyAndVisible];
[self.window makeKeyWindow];
return YES;
}
Application windows are expected to have a root view controller at the end of application launch
sounds like you hadn't embedded in a navigation controller at the beginning of your storyboard
When -applicationDidFinishLaunching:withOptions: does not set up a window or storyboard in code, the next place the system looks is in the info.plist for a default XIB or StoryBoard. The 'Default Storyboard' field in your project info is either empty, or pointing at a non-existent .storyboard file. Click your project in the tree then the 'Summary' tab and fill in the field with the name of your storyboard file.
Unable to find solution after digging through forums and questions and playing with the code, I switched to xibs. Been happy with it since 4 hours ago when I made the transition. I hope I never have to see another error-filled and hard-to-use storyboard file again!

How do I know the window of my view?

ViewWillAppear is never called automatically I have to call them manually. ViewWillDisappear is often called though.
I do not know where to debug this.
I suppose the problem is because I created the application on 4.1 where people have to call viewWillAppear explicitly.
I suppose, because viewWillAppear will be called depending on its relation with window I can check if my viewController has an outlet to window.
How do I do so?
I suspected the problem is somewhere in my delegate:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[Crashlytics startWithAPIKey:#"a08863b514ef09558ba82fec070cc7468fdbeeae"];
if(getenv("NSZombieEnabled") || getenv("NSAutoreleaseFreedObjectCheckEnabled"))
{
NSLog(#"NSZombieEnabled/NSAutoreleaseFreedObjectCheckEnabled enabled!");
}
[self.window addSubview:self.navController.view]; //This seem to be the problem. I should have specified the viewController and not the view
[self.navController pushViewController:self.MainBadgerApplication animated:YES];
//[cachedProperties singleton].lastAnchor =[cachedProperties currentLocation];
[cachedProperties singleton].currentAnchor=[cachedProperties currentLocation];
self.MainBadgerApplication.selectedIndex=0;
[BNUtilitiesQuick tabBarController:self.MainBadgerApplication didSelectViewController:self.MainBadgerApplication.selectedViewController];
[self.window makeKeyAndVisible];
return YES;
}
I suspected that
[self.window addSubview:self.navController.view]; is the issue.
Also I've heard before ios5 you do have to call viewController explicitly. So should I create a different program for ios5 and ios4 (not like there is any danger in calling viewController twice for my program)
I suspected that [self.window addSubview:self.navController.view]; is the issue.
Probably. You should be doing this instead:
self.window.rootViewController = self.navController;
Just adding the view doesn't put your view controller into the hierarchy properly. See the WWDC 2011 view controller containment video for more information.

How to configure app so that root view controller is found?

I am new and am working on an exercise which involves starting with a navigation-based template. Since I am running Xcode 4.2 which no longer has that template, I have started with an empty application template, and then copied the directory structure of the completed app.
Since the empty app template starts only with an AppDelegate.h, .m files, I started adding other required files, including the MainWindow.xib, and the RootViewController.h, .m files. Did some tweaking of the #import directive so that it could see the right files, and could start alright.
However, when I try to run it on the iOS Simulator, I got this message: Applications are expected to have a root view controller at the end of application launch
Terminating in response to SpringBoard's termination.
Program ended with exit code: 0
What additional changes do I need to make so that the app can see the RootViewController?
Thank you.
In -[AppDelegate application:didFinishLaunchingWithOptions:], you need to set your window's rootViewController property.
You need to set the rootViewController property of AppDelegate's _window:
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
_window.rootViewController = self.myNavigationController;
[_window makeKeyAndVisible];
return YES;
}
To start a traditional navigation-based project in XCode 4.2, I find it easier to start with the single-view template. Then, in AppDelegate, I substitute the generated UIViewController with a UINavigationController.
self.window.rootViewController=self.yourviewControollerobj
You can set like this. First set the window bounds then add you navigation controller with root view controller.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
MainViewController *vc = [MainViewController new];
/*
* If you are using .xib you should create UIViewontroller like this
* MainViewController *vc = [MainViewController alloc] initWithNibName:#"MainViewController" bundle:nil]
*/
UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:vc];
[self.window setRootViewController:self.nc];
[self.window makeKeyAndVisible];
return YES;
}
MyViewController *rootCtr = [[MyViewController alloc] init];
[rootCtr.view addSubview:myView];
window.rootViewController = rootCtr;

applications expected to have a root view controller console

I am getting a message within the console when I run my app that says:
2011-11-16 19:17:41.292 Juice[8674:707] Applications are expected to have a root view controller at the end of application launch
I have heard from others that this has to do with the method didFinishLaunchingWithOptions
If anyone has any suggestions for why I am getting this error, it would be much appreciated.
My code for the method:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
[window addSubview:tabBarController.view];
[window makeKeyAndVisible];
[[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationFade];
return YES;
}
You should replace the
[window addSubview:tabBarController.view];
to
[self.window setRootViewController:tabBarController];
Maybe you built your project with 'Empty Application' and forgot to set the rootViewController in your didFinishLaunchingWithOptions (which exists in your AppDelegate.m).
However, if you build your project with 'Single View Application' or some other type, the project will set the rootViewController via xib by default (which might be a MainWindow.xib in your project).
I had the same problem on iOS 5, after adding a storyboard to an "empty" project. It turns out I had to remove all the lines in AppDelegate.m that set values to self.window.
- (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];
return YES;
}
If you have MainWindow.xib, make sure you set Main Interface in Target's summary to MainWindow.
The way I got this error Applications are expected to have a root view controller at the end of application launch to disappear, was to ensure the loadView method in my root view controller was calling [super loadView]. Hope this helps someone.
Try using self.window instead of window (if your setup has window being synthesized with something like #synthesize window=_window;):
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
    // Override point for customization after application launch.
    [self.window addSubview:tabBarController.view];
    [self.window makeKeyAndVisible];
    [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationFade];
    return YES;
}
2nd possibility:
In your main.m make sure the last argument is the name of the App Delegate. In your case, it looks like it should be:
retVal = UIApplicationMain(argc, argv, nil, #"JuiceAppDelegate");
Solution:
As #marcus13 said in the comments below.. This was fixed was found in this SO answer: Applications are expected to have a root view controller at the end of application launch - by by moving the UIAlertView methods from -(void)viewDidLoad to -(void)viewDidAppear:(BOOL)animated
Another cause:
I was in IB attaching File Owner to a new small ImageView I'd dragged onto the View. I hadn't called it an IBOutlet in the .h file, so when I ctrl-dragged to it, the new Imageview wasn't listed as a possible connection. The only possibility displayed in the little black box was View. I must have clicked, inadvertently. I made a few changes then ran the program and got the Root Controller error. The fix was reconnecting File Owner to the bottom View in the xib - IB screen.
I just ran into this issue while building a new project from scratch. I added a StoryBoard and build my whole interface, but i did not select a template.
When doing it this way, you have to make sure of 3 main things:
Always select your initial controller (TabBarcontroller or NavigationController) as the initial view in your Storyboard.
Change the code in your Appdelegate.m from this
- (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];
return YES;
}
to this
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
return YES;
}
Check your [ProjectName]-Info.plist file. If there is no key named "Main storyboard file base name", you have to manually add it and set it's value to the name of your storyboard file (without the extension).
After i did all of these steps, my application ran perfectly.
I also had the same problem. All I got was a black screen. Turns out I had inadvertently removed:
[self.window makeKeyAndVisible];
from my code. Hope this helps someone!
I had two outlets assigned to "view" in the storyboard's root view controller. Right-click on "view controller" and make sure there's only one "view".
I also had the same error while developing an app that uses sqlite Database.
I was showing alertView when the db file transfer failed.
This was a mistake since you cannot show any popovers/alertview/actions without any rootViewController set!
I fixed it by ensuring that any function that creates and shows these alerts/popovers/actionsheets are called after
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
[someObject functionthatDisplayAlerts];
I know this post is old but I ran into this today.
It's because I created a UIAlertView in didFinishLaunchingWithOptions.
Assuming then we should not be doing this because I commented it out and the error went away. I removed my comments and the error came back.
The app doesn't crash, I just get that logged error.
I had the same problem with my App. It appeared when I added another view controller to my project and tried to set it as the root view controller in AppDelegate. I tried several solutions, but none of them could fix the problem. Finally I found the cause: I had two localized versions of the MainWindow.xib file (One for german and another for english localization). So I deleted the english file and reconnected the IBOutlets in MainView.xib. This solved the problem.
I'm not sure if this will help anybody else, but if you have used interface builder to create your mainWindow and have done all the linking between the delegate make sure you don't have the following code within application:didFinishLaunching ...
[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]
I was having the same error until I removed the above line. Hope that helps!
EDIT: Doing the above now has my viewControllers viewDidAppear method being called twice ?
If you are using Storyboard, but created an empty project, you probably forgot to set the Main storyboard to your *.storyboard file in the Summary tab in your project settings. It helped me to solve this problem.
If you are starting from an empty you have to make this addition to your AppDelegate.m file, to "point" the window to the rootViewController (self.window.rootViewController = [[[ViewControllerName alloc] initWithNibName:#"ViewControllerName" bundle:nil] autorelease];)
Like so:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
self.window.rootViewController = [[[ViewControllerName alloc] initWithNibName:#"ViewControllerName" bundle:nil] autorelease];
[self.window makeKeyAndVisible];
return YES;
}

UISplitViewController programmatically without nib/xib

I usually create my projects without IB-stuff. The first thing I do is to strip off all references to xibs, outlets updated plist, etc and so forth. No problems, works great (in my world)!
Now, I just installed 3.2 and tried to develop my first iPad app. Following same procedure as before, I created a UISplitView-based application project and stripped off all IB-stuff. Also, I followed the section in Apple's reference docs: Creating a Split View Controller Programmatically, but nevertheless, the Master-view is never shown, only the Detail-view is (no matter what the orientation is). I really have tried to carefully look this through but I cannot understand what I have missed.
Is there a working example of a UISplitViewController without the nibs floating around somewhere? I have googled but could not find any. Or do you know what I probably have missed?
Declare your splitviewcontroller in your delegate header, use something like this in your didfinishlaunching
ensure you add the UISplitViewControllerDelegate to the detailedViewController header file and that you have the delegate methods aswell. remember to import relevant header files
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
splitViewController = [[UISplitViewController alloc] init];
rootViewController *root = [[rootViewController alloc] init];
detailedViewController *detail = [[detailedViewController alloc] init];
UINavigationController *rootNav = [[UINavigationController alloc] initWithRootViewController:root];
UINavigationController *detailNav = [[UINavigationController alloc] initWithRootViewController:detail];
splitViewController.viewControllers = [NSArray arrayWithObjects:rootNav, detailNav, nil];
splitViewController.delegate = detail;
[window addSubview:splitViewController.view];
EDIT - as per Scott's excellent suggestion below, don't add to the windows subview, instead
[self.window setRootViewController:(UIViewController*)splitViewController]; // that's the ticket
[window makeKeyAndVisible];
return YES;
}
//detailedView delegate methods
- (void)splitViewController:(UISplitViewController*)svc
willHideViewController:(UIViewController *)aViewController
withBarButtonItem:(UIBarButtonItem*)barButtonItem
forPopoverController:(UIPopoverController*)pc
{
[barButtonItem setTitle:#"your title"];
self.navigationItem.leftBarButtonItem = barButtonItem;
}
- (void)splitViewController:(UISplitViewController*)svc
willShowViewController:(UIViewController *)aViewController
invalidatingBarButtonItem:(UIBarButtonItem *)barButtonItem
{
self.navigationItem.leftBarButtonItem = nil;
}
I also prefer code to IB ;-)
Oldish thread, but thought I'd spare reader time + grief when the above technique fails to produce a UISplitViewController that responds correctly to device orientation change events. You'll need to:
Ensure all subordinate views respond properly in
shouldAutorotateToInterfaceOrientation. Nothing new here.
Rather than add the UISplitViewController's view to the main window,
[window addSubview:splitViewController.view]; // don't do this
instead set the main window's root controller to the UISplitViewController:
[self.window setRootViewController:(UIViewController*)splitViewController]; // that's the ticket
Adding the splitviewcontroller's view as a subview of the main window (barely) allows it to co-present with sibling views, but it doesn't fly with UISplitViewController's intended use case. A UISplitViewController is a highlander view; there can only be one.
Swift 5.2
iOS 13
Both master and detail view controllers are embedded in navigation controllers
let splitViewController = UISplitViewController()
splitViewController.delegate = self
let masterVC = MasterViewController()
let detailVC = DetailViewController()
let masterNavController = UINavigationController(rootViewController: masterVC)
let detailNavController = UINavigationController(rootViewController: detailVC)
splitViewController.viewControllers = [masterNavController,detailNavController]
You can put this code in your AppDelegate's (or in SceneDelegate if your target is iOS 13.0+)didFinishLaunchingWithOptions function. Just remember to make the splitViewController your rootViewController like this
self.window!.rootViewController = splitViewController
I had just met the same problem.
make sure that your child viewController of splitview can Autorotate to interface orientation.
you can change the function in your childViewController like this:
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}
then the master view will be shown.