Unique Issue displaying first storyboard scene in Xcode - ios

I have tried for 2 days now to get my storyboard to display with this code:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
UIViewController *LoginViewController = [storyboard instantiateInitialViewController];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.rootViewController = LoginViewController;
[self.window makeKeyAndVisible];
return YES;
}
However, I keep getting this error and a sigbart in my main: 'NSInvalidArgumentException', reason: 'Could not find a storyboard named 'MainStoryboard' in bundle NSBundle (loaded)'
Nothing seems to appear in google about these errors? Does anyone have any idea how to fix it?
Update: When I change it to:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard_iPhone" bundle:nil];
UIViewController *LoginViewController = [storyboard instantiateInitialViewController];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.rootViewController = LoginViewController;
[self.window makeKeyAndVisible];
return YES;
}
it gives me the errors:
[3208:c07] The app delegate must implement the window property if it wants to use a main storyboard file.
{AppDelegate setWindow:]: unrecognized selector sent to instance 0x962bf30
[3208:c07] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[AppDelegate setWindow:]: unrecognized selector sent to instance 0x962bf30'

Finally Found It!!!!!! Turns out that I forgot to put this line of code in the app delegate header file:
#property (strong, nonatomic) UIWindow *window;

Related

NSInternalInconsistencyException throwing by AppDelegate

I'm trying to open a storyboard from my AppDelegate but it throws me the exception:
2016-10-25 10:26:16.776 momnt[22865:1300106] *** Terminating app due to
uncaught exception 'NSInternalInconsistencyException',
reason: 'Application windows are expected to have a root view
controller at the end of application launch'
Here is what i'm trying to do:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UIStoryboard *loginStoryboard = [UIStoryboard storyboardWithName:#"Login" bundle:nil];
UIViewController *mainViewController = [loginStoryboard instantiateInitialViewController];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.rootViewController = mainViewController;
[self.window makeKeyAndVisible];
return YES;
}
I've read that newer versions of XCode requires that all the Windows must have a rootViewController but i've did that.
Debug the application and check if mainViewController is not nil.
Problem seems that you don't have any viewController set as initial viewcontroller in your storyBoard.
Set any viewcontroller you want to initial viewcontroller and your code work fine.
OR
Use instantiateViewControllerWithIdentifier and pass your viewController identifier to which you want as initial viewController.
UIStoryboard *storybord = [UIStoryboard storyboardWithName:#"Login" bundle:nil];
UIViewController *mainViewController = [storybord instantiateViewControllerWithIdentifier:#"InitialViewControllerID"];
self.window.rootViewController = mainViewController;
[self.window makeKeyAndVisible];

What is the correct way to proceed with this bool?

I have this bool in my AppDelegate.m:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
UIViewController *viewController = // determine the initial view controller here and instantiate it with [storyboard instantiateViewControllerWithIdentifier:<storyboard id>];
self.window.rootViewController = viewController;
[self.window makeKeyAndVisible];
return YES;
}
I'm getting an error on the *storyboard but what I am mainly trying to do is initialize the application in a different view controller than the main one. I have all the view controllers with IDs (such as the one I want to launch is named "Home"). How would I correctly write this bool if say my ViewController ID was home?
My main.m:
#import <UIKit/UIKit.h>
#import "AppDelegate.h"
int main(int argc, char * argv[]) {
#autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
}
}
The nil I get is whenever I try to go back to the original Home page from my Contact list page so it crashes and highlights the nil line. Both this and the above delegate bool are giving me issues. Any clarification would be very much appreciated, thanks.
Use this in your AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];
HomeController *viewController = // initialise it
self.window.rootViewController = viewController;
[self.window makeKeyAndVisible];
return YES;
}
or in the storyBoard just put the pointing arror to the view controller which you want to make as a root view controller.
The stack trace explains what's wrong:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Could not find a storyboard
named 'MainStoryboard' in bundle NSBundle (loaded)' *
Your storyboard isn't called "MainStoryboard".

Navigating from Appdelegate when push message received

Currently this is how my method loooks
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
NSString *screenNo =[userInfo objectForKey:#"screen"];
}
Based on the screenNo I would like to navigate to different view controllers. But I couldn't do as most of the answers given below.
Reason is that my root view is not navigation control, so I couldn't segue. It crashes the app.
when push message arrives didReceiveRemoteNotification is called and I could see the content of the message too. But it doesn't get navigated using the methods shown here.
[self.window makeKeyAndVisible];
UIStoryboard *mainStoryBoard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UIViewController *vc = [mainStoryBoard instantiateViewControllerWithIdentifier:#"galleryViewController2"];
[(UINavigationController *)self.window.rootViewController pushViewController:vc animated:YES];
this is the exception
2014-07-21 18:06:53.709 Proitzen Rest[993:60b] -[RESTSecondViewController pushViewController:animated:]: unrecognized selector sent to instance 0x14e26270
2014-07-21 18:06:53.712 Proitzen Rest[993:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[RESTSecondViewController pushViewController:animated:]: unrecognized selector sent to instance 0x14e26270'
*** First throw call stack:
(0x2f480fd3 0x3a021ccf 0x2f484967 0x2f483253 0x2f3d27b8 0xff93b 0x31eb3b29 0x31eb37fb 0x31dbb05f 0x31e6d377 0x31d1c6f5 0x31c9555b 0x2f44c2a5 0x2f449c49 0x2f449f8b 0x2f3b4f0f 0x2f3b4cf3 0x342da663 0x31d0016d 0x157e69 0x3a52eab7)
libc++abi.dylib: terminating with uncaught exception of type NSException
Thanks for your time in advance.
Did you try something like this?
UIStoryboard *mainStoryBoard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UIViewController *vc = [mainStoryBoard instantiateViewControllerWithIdentifier:#"galleryViewController2"];
self.window.rootViewController = vc;
Instead of pushing your new controller (it crashes because to push you need a navigation controller) you can replace current controller with the new one.
Please, take in account that you can not pop to the original controller (if you need to get back, you need a navigation)
You're trying to push a UIViewController with a UIViewController. This is not possible. You must have a UINavigationController in your app hierarchy.
You can also just set the rootViewController:
[self.window setRootViewController: newViewController];
make sure you call this method before trying to present any view controller.
[self.window makeKeyAndVisible];
You can't navigate using push and Pop from APPDelegate if you need to navigate from appDelegate to a file, Segues won't help either then You would need to load it first in your window and then make it Visible such as..
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
NSString *screenNo =[userInfo objectForKey:#"screen"];
UINavigationController *navController = (UINavigationController *)self.window.rootViewController;
firstViewController *menu = [navController.storyboard instantiateViewControllerWithIdentifier:#"firstVC"];
// First item in array is bottom of stack, last item is top.
navController.viewControllers = [NSArray arrayWithObjects:menu, nil];
[self.window makeKeyAndVisible];
}
This is what finally saved me. placed it inside didReceiveRemoteNotification method.
NSLog(#"User wanna navigate");
[self.window makeKeyAndVisible];
UIStoryboard *mainStoryBoard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UIViewController *vc = [mainStoryBoard instantiateViewControllerWithIdentifier:#"moreTableViewController"];
self.window.rootViewController = vc;
UINavigationController *navController = (UINavigationController *)self.window.rootViewController;
UIViewController *evc = [mainStoryBoard instantiateViewControllerWithIdentifier:#"eventsViewController"];
[navController.visibleViewController.navigationController pushViewController:evc animated:YES];

Exception while trying using UITabBarController with UITableViewController in iOS

I'm newbie. I'm trying to use UITabBarController with UITableViewController (without UINavigationController), but I've faced with exception after modifying std tabbar project
Terminating app due to uncaught exception
'NSInternalInconsistencyException', reason: '-[UITableViewController
loadView] loaded the "IHHideView" nib but didn't get a UITableView.'
My didFinishLaunching
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
UIViewController *hideViewController = [[IHHideViewController alloc] init];
UIViewController *unhideViewController = [[IHUnhideViewController alloc] init];
UIViewController *filesVIewController = [[IHFilesViewController alloc] init];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = #[hideViewController,unhideViewController,filesVIewController];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
return YES;
}
IHHideViewController just simplesubclass of UITableViewController
#interface IHHideViewController : UITableViewController
#end
As I know UITableViewController create own UITableView object with the correct dimensions and autoresize mask if not to specify nib file. Why such exception occurs?
It is because you are sub-classing a TableViewController. Instead change:
#interface IHHideViewController : UITableViewController
to:
#interface IHHideViewController : UIViewController
Hmmm recreating project using empty template solve problem.

Pushing ViewController gives NSInvalidArgumentException

I'm a newbie iOS developer, recently developed several Android apps, but I'm not familiar with iOS jargon. Let me explain my problem.
I want to use two different UIViewController. I've created .h and .m files for both controller. My plan is to push the secont view controller on top of the first view controller five seconds after the first view controller appears on the screen. I mean the first view controller is something like splash screen or similar.
Here is my contribution. In the first view controller, I defined (one of them implemented of course) these two method:
-(void) pushSecondController {
SecondViewController *secondController = [[SecondViewController alloc]
initWithNibName: nil
bundle: NULL];
[self.navigationController pushViewController: secondController animated: YES];
}
-(void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self performSelector: #selector(pushViewController:animated:)
withObject: nil
afterDelay: 5.0f];
}
And the second view controller looks like this:
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
self.title = #"Second Controller";
}
I've changed only viewDidLoad method. When I ran the simulator, first view controller worked well and waiting 5 seconds and crashed. Output looks like:
2012-08-24 10:46:34.104 NavApplication[20355:f803] -[ViewController pushViewController:]: unrecognized selector sent to instance 0x6e7f780
2012-08-24 10:46:34.107 NavApplication[20355:f803] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[ViewController pushViewController:]: unrecognized selector sent to instance 0x6e7f780'
Let me ask one more question: I know there are differences between methodName and methodName:. Can anyone explain what's difference?
Any help would be appreciated.
UPDATE:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
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.navigationController = [[UINavigationController alloc] initWithRootViewController:self.viewController];
[self.window makeKeyAndVisible];
[self.window addSubview: self.navigationController.view];
return YES;
}
Change #selector(pushViewController:animated:) to #selector(pushSecondController).
As mentioned above, you probably want to change your performSelector command to:
[self performSelector: #selector(pushSecondController)
withObject: nil
afterDelay: 5.0f];
because you want it to call your pushSecondController method, and not pushViewController:animated:.
Regarding your second question: the difference between methodName and methodName: is that the : at the end of methodName: signifies that this method takes a parameter. So, you could have the following methods:
- (void)listItems
{
// ...
}
- (void)insertItem:(NSDictionary *)item
{
// ...
}
When passing a reference to them into #selector, for the first method you'd just do #selector(listItems), because it takes no parameters, and for the latter you'd do #selector(insertItem:) because it takes a parameter.
UPDATE
Just saw your applicationDidLaunch code. You probably want to rearrange things so that you add your ViewController to your UINavigationController, and then set the UINavigationController as the rootViewController of your window. Like so:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
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;
}
please change your didFinishLaunchingWithOptions method to:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
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;
}

Resources