Xcode App won't load view sometimes - ios

my project runs without storyboards, so im loading my view inside AppDelegate:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UIWindow *window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window = window;
self.window.rootViewController = [[SYLoginController alloc] init];
[self.window makeKeyAndVisible];
return YES;
}
It used to work on iPhone 6 sim 9.3 but now, on all simulators it only shows a black screen. But on my iPhone 6 Device it works. Whereas on another iPhone 6 it also shows a black screen. Those two iPhones are literally the same 16gb iPhone 6 bought at nearly the same time.
In SYLoginControllers viewDiDLoad I'm logging the text of one of the buttons. Only on my device it prints the text, on all simulators and the other iPhone it prints (null), so i assume that the xib is not properly loaded.
SYLoginController is a UIViewController, and the related xib holds a UIView with its FilesOwner set to SYLoginController. I really can't see why it only works on this particular device. Also i tried [[SYTabBarController alloc] initWithNibName:#"SYLoginController" bundle:nil], this also does not work...

Did you tried to wrap it arround a Navigation Controller?
SYLoginController *syVC = [[SYLoginController alloc]initWithNibName:#"SYLoginController" bundle:nil];
UINavigationController *nav = [[UINavigationController alloc] syVC];
self.window.rootViewController = nav;
Also check again if Custom Class is set to SYLoginController in the XIB-File. And the View is connected to the Files Owner.

Related

How to properly use a UINavigationController

I just recently started programming for iOS/iPhone. I thought I knew what I was doing until XCode5/iOS7 came around. Previously, I created a class derived from UIViewController with a XIB, added a label, and programatically added it to the rootWindow:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
... // boilerplate code
MyViewController* myRoot = [[MyViewController alloc]init];
self.window.rootViewController = myRoot;
To use a navigation bar, I changed the code slightly:
MyViewController* myRoot = [[MyViewController alloc]init];
UINavigationController* navigationController = [[UINavigationController alloc]init];
[navigationController pushViewController:myRoot animated:YES];
self.window.rootViewController = navigationController;
This seemed to work fine. However, on iOS 7 the controls at the top of my custom view controller appear to be behind the navigation bar. Some googling resulted in this link which describes changes in the status bar.
It also seems to indicate that,
A) UINavigationController should handle the changes automatically
B) "auto layout" should handle the changes automatically,
and that I shouldn't need to worry. However, my sample app above doesn't appear to handle anything automatically.
I also found some other sample code which uses the controller differently: adding the navigation controller's view as a subView to an existing view. This sort of makes sense for adding a navigation controller later on in an app's lifetime, but I am trying to set one up on launch.
Am I using the UINavigationController correctly?
What do I need to consider for iOS7 vs. earlier versions?
How do I configure "Auto Layout" (I don't see this in interface builder anywhere)?
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.viewController = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
self.navigationController = [[UINavigationController alloc] initWithRootViewController:self.viewController];
self.window.rootViewController = self.navigationController;
[self.window makeKeyAndVisible];
return YES;
}

View is only displaying for a fraction of a second

I have a LoginViewController class that extends UIViewController that is only displaying for a fraction of a second. In my AppDelegate.m class I try to allocate a UINavigationController that will have LoginViewController as a child view controller. I see loginViewController's view for a split second before the view goes black (I see the view I designed in Interface Builder). I'm not really sure why this is occurring.
Aside from the view flashing quickly, I was running the app on a 2nd gen iPod touch and noticed I could swipe my thumb to the right and see apps from the home screen. One more swipe and I could see search, but icons at the bottom such as music, mail, safari, and videos were not visible. Pressing stop button killed the app and returned to a normal home screen. This makes me think I'm setting the root view controller incorrectly? I took the if respondsToSelector code from another question here on Stack Overflow. The if stopped my code from crashing which I think has something to do with devices older than iOS 4? When it ran without crashing is when I started receiving login view flashing just once and the home screen apps are just a thumb swipe away.
Any help is appreciated. If more of my code would help explain my question just let me know and I'll post more.
Edit: Forgot to post Xcode Version 4.4 and Deployment iOS targeted 4.0
AppDelegate.m:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
mainViewController = [[[MainViewController alloc] initWithNibName:#"MainViewController" bundle:nil] autorelease];
loginViewController = [[[LoginViewController alloc] initWithNibName:#"LoginViewController" bundle:nil] autorelease];
navigationController = [[UINavigationController alloc] init];
[navigationController pushViewController:loginViewController animated:NO];
if([window respondsToSelector:#selector(setRootViewController:)])
{
[window setRootViewController:navigationController];
}
else
{
[window addSubview:[navigationController view]];
}
initWithRootViewController:viewController] autorelease];
initWithNibName:#"ContainerViewController" bundle:nil] autorelease];
[window makeKeyAndVisible];
return YES;
}
Take out the autorelease in your window-creating line. You want the window to live as long as the application is running.

ios 6 iPhone window.rootViewController crashes

My app works fine with iOS 5.1 on both iPad and iPhone. In iOS 6 app works fine on iPad but crashes in iPhone while setting the window.rootViewController = navigationViewController. I have set Exception break point which stops execution at this piece of code in application didFinishLaunchingWithOptions. I even tried adding it as [window addSubView:navigationViewController.view] but no luck. Has any one faced similar issue.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
rootViewController = [[SpringboardViewController alloc] initWithNibName:#"SpringboardViewController" bundle:nil];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:rootViewController];
self.window.rootViewController = nav;
self.window makeKeyAndVisible];
return YES;
}
could you write an exception ?
take a look at these questions
applications expected to have a root view controller console
Applications are expected to have a root view controller at the end of application launch
Thanks for your replies guys i figured out the problem ,problem was with the application supportedInterfaceOrientationsForWindow where i was returning UIInterfaceOrientationPortrait instead of UIInterfaceOrientationMaskPortrait since app debugger was struck at self.window.rootViewController = nav, i thought problem was with this statement.
Thanks Ravindharan and Ogres.

Converting a simple app to have a tab bar

I have a question that is similar to this SO question, but slightly different (or my skills don't allow me to follow the directions with confidence). I have an existing game app that has one view controller and one nib and works fine. I want to convert it to have a tab bar controller. I want the original, existing view controller to be on the first tab, and I wrote a new view controller and a new nib for the second tab, which will be dedicated to game settings. At this stage, the app builds and runs fine with the new nib and view controllers in the project (but with no further edits -- no attempt to add the tab bar controller etc). The modified app should simply have two views each accessible from one of the two tabs.
Sorry for the long bkgnd. I'm following the accepted answer to the above-referenced question. The first 4 steps I have done or can do. The 5th step is to Delete the old version of your Main View Controller from the NIB file and also remove the IBOutlet property from the Application Delegate. I don't think I have such an IBOutlet in my app (which is different from the OP's app). Should I delete the object view controller shown in this list? Or am I on the wrong track here?
Additional Info
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Set up view controller & load a clean view
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.viewController = [[P3ViewController alloc] initWithNibName:#"P3ViewController" bundle:nil];
self.window.rootViewController = self.viewController;
NSLog(#"P3ViewController now active");
[self.window makeKeyAndVisible];
return YES;
}
This should get you in the right direction...
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UITabBarController *tbc = [[UITabBarController alloc] init];
YourNewViewController *ynvc = [[YourNewViewController alloc] initWithNibName:#"YourNewViewController" bundle:nil];
YourCurrentViewController *ycvc = [[YourCurrentViewController alloc] initWithNibName:#"YourCurrentViewController" bundle:nil];
[tbc setViewControllers:[NSArray arrayWithObjects:ynvc, ycvc, nil]];
self.window.rootViewController = tbc;
[self.window makeKeyAndVisible];
return YES;
}

UIWindow's root view controller does not rotate to landscape at app launch

I am developing a xib-based landscape-only app. The app launches in landscape correctly. However, the view in my main ViewController is presented in portrait. That is, it is rotated 90 degrees so that the image appears cropped and does not take up the entire screen. If I use my interface to present a modal view controller then return to the main ViewController, the issue corrects itself (the view is presented in landscape). This problem did not occur under Xcode 4.2. It occurred after upgrading to Xcode 4.3, and the only code changes that were made were automatically implemented by Xcode when I upgraded the project settings.
Based on advice in other posts, I verified my Info.plist settings for Supported Interface Orientations and Initial Interface Orientation. I overrode the shouldAutorotateToInterfaceOrientation method for each of my view controllers to return YES only for landscape orientations. Also, I turned off auto resizing for the view, as I never want the size/orientation of the view to change.
Based on the ideas in this link [1], I suspected the problem is that the view is not receiving the call to change orientation at launch, possibly due to the removal of the MainWindow.xib concept, which appears to be replaced by the following Xcode-inserted code in AppDelegate.m:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.viewController = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
I modified this method to generate a generic root view controller from which my ViewController class is presented, as shown in the code below:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
ViewController* myViewController = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
self.viewController = [[UIViewController alloc] init];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
[self.viewController presentModalViewController:myViewController animated:NO];
return YES;
}
Voila! This solved my problem. However, to me it feels like a change at a fundamental level, which I don't want to make at this point in my development. What I intended to be my root view controller is now a modal view. Does anyone have another solution to this issue?
Thanks in advance!
I had this same issue: an app which was meant to be in Landscape that assumed the ViewController was always in Portrait. I made tons of changes to every aspect of the project and info.plist, including giving the main UIWindow a root-view controller which was landscape. It still didn't work. I eventually undid all the changes and just added the two lines noted below to my app delegate:
- (void)applicationDidFinishLaunching:(UIApplication *)application
{
[_window addSubview:[_viewController view]];
glView = _viewController.glView;
// THIS FIXED ORIENTATION FOR ME IN IOS 6
_window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
// END
...my other setup code here...
}
Nothing else was necessary.
It appears that, for some reason, in iOS 6 the UIWindow root-view-controller setting is sometimes ignored in Interface Builder. I am sure my reasoning is wrong somewhere, but I thought this might well help prod someone in the direction of a fuller, more exhaustive answer.
In iOS 8, settings windows frame to the UIScreen is also necessary, as it won't get automatically updated.
self.window.frame = [UIScreen mainScreen].bounds;

Resources