iOS:How to open Another UINavigationController inside Existing UINavigationController? - ios

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.

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;

Switch between different Views with a Navigation Controller

I'm totally new to iOS programming. I only programmed on Android so far and Objective-C is a total different and new language for me now.
What I want to do is to not use a design that I've created with the storyboard. I want to do all programmatically, since I think it will be more dynamic if I do it like this.
The problem I'm encountering is, that I want to have 3 different views. I googled a bit, and stumbled upon some stackoverflow questions. There, people suggested using a NavigationController. Okay. Now I'm trying to implement it. What I want to have is the following
A MainViewController that has 3 different views. The first view is a loginView. The second one is displaying data and the third is displaying detailed data dependent on the click of the second view.
Is a navigationcontroller corerct for this? The problem I'm having is where I tell the app that I want to start with the MainViewController and push the LoginView in it.
I have a MainViewController.h and MainViewController.m that are subclasses of UIViewController
Now, where exactly do I do this? I have the didFinishLaunchingWithOptions method right here with the following content
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
UIViewController *viewController = [[MainViewController alloc]init];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
[navigationController pushViewController:viewController animated:NO];
self.window.rootViewController = navigationController;
[self.window makeKeyAndVisible];
return YES;
}
But that just crashes the app. What am I doing wrong? How do I get those three views? Am I starting completely wrong? Please help. As I said I'm new to iOS development. It's easy for me to programm on one view. I did that already, but I want thre different views! Thanks!
A MainViewController that has 3 different views. The first view is a loginView. The second one is displaying data and the third is displaying detailed data dependent on the click of the second view.
That's wrong.
You need three different view controllers, each of those will manage its own view.
Then you push one after another in the navigation controller, depending on user interaction.
Yes, Gonzalo Aune is rite, You should not push the rootviewcontroller in NavicationController.
Also , I will Suggest you to keep your first view (Login View) out of Navigation controller.
You can start with your MainViewController and based on check and conditions you can present LoginView on MainViewController using
[self presentViewController:loginViewController animated:YES completion:NULL];
And after successful login you can dismiss LoginViewController.
Remove this:
[navigationController pushViewController:viewController animated:NO];
You shouldnt push the ViewController since you told the NavigationController already that the ViewController would be the root one:
UINavigationController *navigationController = [[UINavigationController alloc]
initWithRootViewController:viewController];
use this code and if application is universion then use same code else remove the condition of ([[UIDevice currentDevice] userInterfaceIdiom]
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UINavigationController *navController;
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
self.viewController = [[ViewController alloc] initWithNibName:#"ViewController_iPhone" bundle:nil];
{
if(result.length>0)
{
for(var i in result)
{
var ObjResult=result[i];
var content = "<div data-role='collapsible' id='set" + i + "'>";
content+="<h3>"+ObjResult.title+"<br>";
var intDate=parseInt(ObjResult['ordered date']);
content +=timestampToDate(intDate)+"</h3>"
if(isNaN(ObjResult.med_placeorderfor))
content+="<p><a>Medicle Place order for: </a>"+result[i].med_placeorderfor+"</p>";
if(isNaN(ObjResult.pres_placeorderfor)>0)
content+="<p><a>Medicle Place order for: </a>"+result[i].placeorderfor+"</p>";
if(ObjResult['order status'].length>0)
content+="<p><a>Order status: </a>"+ObjResult['order status']+"</p>";
if(ObjResult.comments.length>0)
content+="<p><a>Comments: </a>"+ObjResult.comments+"</p>";
content+="</div>";
}
$("#id_notification_list_dashboard").append( content ).collapsibleset('refresh');
$("#id_notification_list_dashboard").trigger('create');
}
else
{
$("#id_notification_list_dashboard").append("<div style=\"text-align:center\" data-role='list-divider'><h1>No data found</h1></div>").collapsibleset('refresh');
}
$('body').removeClass('ui-loading');
loadingWithMsg("hide");
}
} else {
self.viewController = [[ViewController alloc] initWithNibName:#"ViewController_iPad" bundle:nil];
}
navController=[[UINavigationController alloc]initWithRootViewController:self.viewController];
[navController.navigationBar setTranslucent:YES];
navController.navigationBar.tintColor = [UIColor colorWithRed:161.0f/255.0f green:18.0f/255.0f blue:6.0f/255.0f alpha:1];
self.window.rootViewController =navController ;
[self.window makeKeyAndVisible];
return YES;
}

How to add/connect NIB to programmatically created UINavigationController?

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

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.

Trying without success to add Navigation Controller to existing iOS app

I'm experimenting with OAuth 2.0. which brings up a window containing a UIWebView as a canvas for the authentication server to communicate through. Right now, it is being shown as a modal view with its own view controller, and doesn't have a back button or cancel button. So the user has no way to escape from the sign-in process.
I want to have the webView handled by a navigation controller so I can push the webView's view controller.
I am having problems with doing this. It seems to me I should be able to just create a UINavigationController object with the root view controller being the main view controller, like this
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:self];
However, this line crashes the program without any error message in the debugger.
I thought I could place a line to push the web view controller immediately after the line above, like this:
[navController pushViewController:windowController animated:YES];
But, without getting past the first line, I can't begin to work out the details on getting the web view to show and then configure the back button.
UPDATE
Here is some context. This is in a single view application. The following code is in the main view controller, which brings up the sign-in dialog. I would like to replace the last line, where the presentModalViewController is called, with a push of the windowController onto a navController stack. Note the commented code at the end, where the initialization of the navigation controller is located.
- (IBAction)signInClicked:(id)sender {
if (![self isSignedIn]) {
// Sign in
[self runSigninThenInvokeSelector:#selector(updateUI)];
}
[self updateUI];
}
- (void)runSigninThenInvokeSelector:(SEL)signInDoneSel {
NSString *clientID = mClientId;
NSString *clientSecret = mClientSecret;
// Show the OAuth 2 sign-in controller
NSString *scope = [GDataServiceGoogleBlogger authorizationScope];
GTMOAuth2ViewControllerTouch *windowController;
windowController = [[GTMOAuth2ViewControllerTouch controllerWithScope:(NSString *)scope
clientID:(NSString *)clientID
clientSecret:(NSString *)clientSecret
keychainItemName:(NSString *)kKeychainItemName
delegate:(id)self
finishedSelector:#selector(windowController:finishedWithAuth:error:)] retain];
//UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:self]; // This causes a crash when it is not commented out.
[self presentModalViewController:windowController animated:YES];
}
The application delegate didFinishLaunchingWithOptions is set up this way:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
self.viewController = [[[blogSpotViewController alloc] initWithNibName:#"myNibName" bundle:nil] autorelease];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
Well, you can present this as a modal controller. You just need to arrange to call dismissModalViewController: in response to a button or some other event on the 'windowController'.
But if you want to use a navigation controller, then you can set that up in application:didFinishLaunchingWithOptions: like so:
// create window here like now
blogSpotViewController *viewController = [[blogSpotViewController alloc] initWithNibName:#"myNibName" bundle:nil];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:viewController];
[viewController release];
// TODO: hold onto this navController in a property if you really need that.
self.window.rootViewController = navController;
[navController release];
// present window here as you are now
This embeds your main view controller in a navigation controller. Then back in your runSigninThenInvokeSelector: you can...
[self.navigationController pushViewController:windowController animated:YES];
Hope that points you in the right direction.

Resources