17

still cannot figure out what I am doing wrong. Just trying to get a modal view with a navigation controller inside.

Here is my project http://www.matthewpateman.com/New.zip

Can anybody tell me what I am doing wrong? I want "ShopModalView.xib" to pop up in the navigation controller, but its just diplaying a blank page…

CodeBender
  • 35,668
  • 12
  • 125
  • 132
Matthew Pateman
  • 1,009
  • 3
  • 11
  • 20
  • 1
    Please comment or update your previous question instead of simply posting another. – Justin Aug 13 '10 at 17:20
  • 7
    also, nobody wants to download a potentially malicious zip. use http://gist.github.com/ or friendpaste or something to display the pertinent code – Matt Williamson Aug 13 '10 at 17:29
  • 1
    possible duplicate of [Navigation View in Modal View](http://stackoverflow.com/questions/3477809/navigation-view-in-modal-view) – Brad Larson Aug 13 '10 at 18:57

3 Answers3

48

Present it as a modal view and wrap the controller in a navigation controller to provide a navigation bar in case you want to add edit, save, etc buttons

ModalViewController *modalController = [[ModalViewController alloc] initWithNibName:@"ModalViewController" bundle:nil];
modalController.delegate = self; // Set any delegate you may have

// This is where you wrap the view up nicely in a navigation controller
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:modalController];

// You can even set the style of stuff before you show it
navigationController.navigationBar.barStyle = UIBarStyleBlackTranslucent;

// And now you want to present the view in a modal fashion
[self presentModalViewController:navigationController animated:YES completion:nil];
iwasrobbed
  • 46,496
  • 21
  • 150
  • 195
  • wow! Thanks for your help! It was very helpful. Really appreciate you spending your time fixing it up. As you said! Works great - nice and simple. – Matthew Pateman Aug 13 '10 at 22:56
  • 1
    Not a prob, but make sure you take a few lessons learned: You were missing XIB files in XCode for some of your view controllers, you were missing a MainWindow.xib which is why you were getting an `NSInternalInconsistencyException` error (it had nothing to load in `applicationDidFinishLaunching` and no way to set up the nav controller initially). You were also hiding the nav controller bar from showing when your view appeared. Keep your XIB's named the same as the view controller class that handles them... it's one less thing to remember. – iwasrobbed Aug 14 '10 at 00:56
  • You may want to create a whole new example project and set it up yourself now that you have something to work off of. XCode actually has a navigation controller template to work with if you know from the beginning you'll be using one (XCode -> New project -> iPhone OS application -> Navigation based app -> uncheck "Use core data for storage") – iwasrobbed Aug 14 '10 at 00:59
  • Also one last thing, when you know you want to add a new view controller to the application do it by right clicking the model tree area, clicking Add -> New file -> Cocoa Touch class -> UIViewController subclass and make sure the "With XIB for user interface" box is checked so it creates the XIB for you. It'll make your life easier – iwasrobbed Aug 14 '10 at 01:02
  • by the way, you should probably accept the answers on the other 2 questions you posted related to this since those people were correct. In the future, just update your *first* post and if no one answers you, you can offer a bounty of reputation points which encourages more people to answer. here: http://stackoverflow.com/questions/3477809/navigation-view-in-modal-view and here: http://stackoverflow.com/questions/3476920/uinavigationview-in-modal-view – iwasrobbed Aug 14 '10 at 01:07
  • Also, I updated the example app above so use this newer version to go off of. Best of luck – iwasrobbed Aug 14 '10 at 01:39
  • Okay how do I arrange that UINavigationController has a back button that go back to the original controller if pressed. – Septiadi Agus Jul 29 '13 at 09:33
6

For those wanting to do this in Swift 3.x:

// Obtain your viewcontroller
let viewController = ...

// Initialize a navigation controller, with your view controller as its root
let navigationController = UINavigationController(rootViewController: viewController)
navigationController.navigationBar.barStyle = .blackTranslucent

// Present it modally from the current controller
present(navigationController, animated: true, completion: nil)
CodeBender
  • 35,668
  • 12
  • 125
  • 132
5
ShopModalViewController *shopMVC = [[ShopModalViewController alloc] initWithNibName:@"ShopModalViewController" bundle:nil];
//set properties
UINavigationContrller *navCon = [[UINavigationController alloc] initWithRootViewController:shopMVC];
[self presentModalViewController:navCon animated:YES];
[shopMVC release];
[navCon release];
DVG
  • 17,392
  • 7
  • 61
  • 88
  • I used your code but it gives me this error message: 2010-08-13 19:55:54.718 Nav[95599:207] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not load NIB in bundle: – Matthew Pateman Aug 13 '10 at 18:57