How to add/connect NIB to programmatically created UINavigationController? - ios

First of all, I'm a beginner, so I might have my concepts wrong.
This is my code in AppDelegate.m:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UIViewController *viewController = [[UIViewController alloc] initWithNibName:#"FirstView" bundle:nil];
self.navigationController = [[UINavigationController alloc] initWithRootViewController: viewController];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
self.window.rootViewController = self.navigationController;
[self.window makeKeyAndVisible];
return YES;
}
The idea is to programmatically add UINavigationController, but then use a .xib (the "FirstView" paremeter) for the views. I have no problem with programmatically creating the views as well, but for simpler interfaces, I assume using IB can be done as well.
However, when I run the code, I get this error:
2013-07-11 21:20:42.644 new-book-proto-01[64308:11303] *** Terminating app due
to uncaught exception 'NSInternalInconsistencyException', reason:
'-[UIViewController _loadViewFromNibNamed:bundle:] loaded the "FirstView" nib
but the view outlet was not set.'
What should I do? Is there anything missing?
Additionally, is something like this common in iOS development? Is it bad practice? Is there a better way to do this?

This is the reason as told: "reason:
'-[UIViewController _loadViewFromNibNamed:bundle:] loaded the "FirstView" nib
but the view outlet was not set.' "
It means that you try to access or get value for some views (UI elements) that has not IBOutlet in your controller class or you have none view at all
You should create IBOutlets (properties) for each view (each UI element is view) that you would like to "see" in your controller

Related

Error: Application windows are expected to have a root view controller at the end of application launch

I'm trying to update an iOS app written 6 years ago using OpenGL ES and Objective C.
When running the app as it is, I get this error:
4DRoom_v3[2360:42863] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Application windows are expected to have a root view controller at the end of application launch'
From what I have read here I need to set the root view controller.
I add the subview here:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[MeshViewAppDelegate globalVarInit];
//sleep(1);
glView.multipleTouchEnabled = TRUE;
load4Dice();//(str, NEW);
[window addSubview:glView];
//[window makeKeyAndVisible];
glView.animationFrameInterval = 1.0/40;
printf("finish luanching\n");
//[glView startAnimation];
return YES;
}
glView is a subclass of UIView. But I don't see how to implement the solution given since in this case, the view is not a property of a UIViewController.
It isn't clear to me what UIViewController I could set as the root view controller.
Any help would be greatly appreciated.
Create a view controller and add your glview as subview:
UIViewController* vc = [[UIViewController alloc]initWithNibName:nil bundle:nil];
[vc.view addSubview:glview];
window.rootViewController = vc;

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.

Change initial View Controller of Master-Detail-Application

I created a new iPhone project on a Master-Detail-Application. As I need a login before, I added at the storyboard a new ViewController, checkmarked "is initial View Controller" and assigned the new created class LoginViewController to this ViewController.
Furthermore but not relevant (in my opinion), i created a segue and connected it from the ViewController to the NavigationController (of the Master-Detail-Template).
The AppDelegate.m was changed to launch the new controller at startup:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
/* // Old Master-Detail-View-Controller
UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController;
MasterViewController *controller = (MasterViewController *)navigationController.topViewController;
controller.managedObjectContext = self.managedObjectContext;
*/
(LoginViewController *)self.window.rootViewController;
return YES;
}
This will end up in the error:
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException',
reason: '-[UICollectionViewController loadView] loaded the "Qn5-Rj-iPA-view-IRE-eP
-ILZ" nib but didn't get a UICollectionView.'
I have as well tried this:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
LoginViewController *loginViewController = [[LoginViewController alloc] init];
self.window.rootViewController = loginViewController; //Set the view controller
[self.window makeKeyAndVisible];
return YES;
}
Resulting in a similar error:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException',
reason: 'UICollectionView must be initialized with a non-nil layout parameter'
Where is my mistake?
What I do not get is, where the hell the UICollectionView in the error message comes from. I did never create one or add one??
My LoginViewController inherits by mistake form UICollectionViewController instead of UIViewController. I fixed that and it works.
This explained the strange errormessage which i did not understood in the first place. As always the truth is in the sourcecode. Rechecking helps.
You don't have to do anything to load view controllers in the AppDelegate. As long as your stroyboard is the one that is loaded in project settings, it should load automatically.

iOS:How to open Another UINavigationController inside Existing UINavigationController?

I am facing crash with following code. The scenario is
This is my app delegate method in which i load RTC_HomeVC using UINavigationController.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
RTC_HomeVC *obj_RTC_HomeVC=[[RTC_HomeVC alloc]init];
UINavigationController *nav=[[UINavigationController alloc]initWithRootViewController:obj_RTC_HomeVC];
// Override point for customization after application launch.
self.window.rootViewController=nav;
[obj_RTC_HomeVC release];
[nav release];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
Now I want to open UINavigationController inside a parent Navigation controller. So i use a following code. The method -(IBAction)call_SectionFlow is in RTC_HomeVC.
-(IBAction)call_SectionFlow{
RTC_1_StoreDetailsVC *obj_StoreDetailsVC=[[RTC_1_StoreDetailsVC alloc]initWithNibName:#"RTC_1_StoreDetailsVC" bundle:nil];
RTC_3_EnablingWorksVC *obj_EnablingWorksVC = [[RTC_3_EnablingWorksVC alloc]initWithNibName:#"RTC_3_EnablingWorksVC" bundle:nil];
UINavigationController *navController_Sections = [[UINavigationController alloc] init];
NSArray *array_ControllerArray=[[NSArray alloc]initWithObjects:obj_StoreDetailsVC,obj_EnablingWorksVC, nil];
[navController_Sections setViewControllers:array_ControllerArray animated:FALSE]
navController_Sections.view.frame=CGRectMake(14, 40, 996,636 );
[self.view addSubview:[[[navController_Sections viewControllers] objectAtIndex:0] view]];
}
When i called this method application is crashed. This is crash log.
Crash log:
* Terminating app due to uncaught exception 'UIViewControllerHierarchyInconsistency', reason: 'child view controller:< RTC_1_StoreDetailsVC: 0x71f53a0 > should have parent view controller:< RTC_HomeVC: 0x758b310 > but actual parent is:< UINavigationController: 0x71f55d0 >'
* First throw call stack:
(0x1c9c012 0x10d9e7e 0x1c9bdeb 0x6838a 0x68739 0x6f5a3 0x67eed 0x4fc3 0x10ed705 0x24920 0x248b8 0xe5671 0xe5bcf 0xe4d38 0x5433f 0x54552 0x323aa 0x23cf8 0x1bf7df9 0x1bf7ad0 0x1c11bf5 0x1c11962 0x1c42bb6 0x1c41f44 0x1c41e1b 0x1bf67e3 0x1bf6668 0x2165c 0x1f82 0x1c45)
libc++abi.dylib: terminate called throwing an exception
So any one can tell me
What is wrong with this code? And which approach i should follow for resolving this crash ?
How to open another UINavigationController in existing UINavigationController?
Thanks.
Do not add subviews to UIWindow manually. It is not supported (or at least it does not work OK).
Use this method:
[firstNavigationVC presentViewController:secondNavigationVC animated:YES completion:nil];
What is causing crash is, that you are adding obj_StoreDetailsVC to the new navigation controller and then its view to self.view. Once a VC is child of another VC, its view must be descendant of that VC's view. Maybe you can add secondNavigationVC's view to to the view of firstNavigationVC, but that isn't how UIKit is supposed to work. Use the above method.

Runtime error running non-storyboard app

I am writing an iOS app; no ARC and no storyboards.
I have a view controller inside Navigation controller. It supposed to have Table view and navigation button which pushes to second view.
This is the error I get:
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[UIViewController _loadViewFromNibNamed:bundle:] loaded the "ViewController" nib but the view outlet was not set.'
*** First throw call stack:
(0x1c8d012 0x10cae7e 0x1c8cdeb 0xf2f18 0xf3418 0xf3648 0xf3882 0x42a25 0x42dbf 0x42f55 0x4bf67 0x2a88 0xf7b7 0xfda7 0x10fab 0x22315 0x2324b 0x14cf8 0x1be8df9 0x1be8ad0 0x1c02bf5 0x1c02962 0x1c33bb6 0x1c32f44 0x1c32e1b 0x107da 0x1265c 0x2442 0x2375)
libc++abi.dylib: terminate called throwing an exception
(lldb)
The code in AppDelegate:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
self.viewCon = [[ViewController alloc]initWithNibName:#"ViewController" bundle:nil];
self.navCon = [[UINavigationController alloc]initWithRootViewController:self.viewCon];
self.navCon.navigationBar.tintColor= [UIColor greenColor];
self.viewCon.title= #"First View";
self.tblView = [[UITableView alloc] init];
NSMutableArray *viewArr=[[NSMutableArray alloc] init];
[viewArr addObject:self.navCon];
self.navBar = [[UINavigationBar alloc] init];
self.viewCon.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Second View" style:UIBarButtonSystemItemAdd target:self action:(nil)];
[self.window addSubview:self.navBar];
self.window.rootViewController = self.viewCon;
[self.window makeKeyAndVisible];
return YES;
}
At this stage I cannot run even the app. What might be the prob?
Best regards
loaded the "ViewController" nib but the view outlet was not set.
It seems you forgot to connect the view outlet of the view controller to your actual view in ViewController.xib file.
Your xib file has got a file's owner. This you have presumably set to be a ViewController (which I imagine is a subclass of UIViewController).
Now, if you go to the right-hand pane and show the bindings pane (the last one, with a small arrow), you will see that the view controller has got an outlet called view: drag from the small circle on its right on to the view which you have created in the xib.

Resources