2

WHAT I AM DOING:

Hi guys, I am currently learning iOS development. I have encountered a problem, which I have solved. However, I don't actually know what happened, that it worked. I am doing a DrinkMixer app from a book called:”Head first Iphone & Ipad development”(2nd edition). The book is written for Xcode 4 and iOS4 Sdk, while I work on Xcode 5 with iOS 7.

GOAL:

I am trying to create a new view which I want to be pushed as a Modal view in order to make the user fill in new drink informations.

CODE BREAKING POINT:

@autoreleasepool 
{
    return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
}

BREAKING CODE:

-(IBAction)addButtonPressed:(UIBarButtonItem *)sender
{
    DummyAddDrink *someDrink = [[DummyAddDrink alloc] initWithNibName:@"DetailViewController" bundle:nil];

    [self presentModalViewController:someDrink animated:YES];
}

WORKING CODE:

-(IBAction)addButtonPressed:(UIBarButtonItem *)sender
{

  DummyAddDrink *someDrink = [self.storyboard instantiateViewControllerWithIdentifier:@"DetailViewController"];[/u][/b]


[self presentModalViewController:someDrink animated:YES];
}

CODE EXPLANATION: addButtonPressed is connected to a "Bar Button Item" in a "Navigation Bar". When the BBI is clicked it calls aBP. aBP creates a new view by subclassing from a existing controller called "DetailViewController". The DVC has his own .h, .m, VC in IB.

ERROR OUTPUT:

2013-12-18 09:57:02.735 DrinkMixer_Take_2[848:70b] NSBundle </Users/.../Library/Application Support/iPhone Simulator/7.0/Applications/408E0817-824B-40A6-   B1B2-34DDC8043712/DrinkMixer_Take_2.app> (loaded)
2013-12-18 09:57:37.105 DrinkMixer_Take_2[848:70b] *** Terminating app due to uncaught   exception 'NSInternalInconsistencyException', reason: 'Could not load NIB in bundle: 'NSBundle   </Users/.../Library/Application Support/iPhone Simulator/7.0/Applications/408E0817-824B-40A6-   B1B2-34DDC8043712/DrinkMixer_Take_2.app> (loaded)' with name 'DetailViewController''

QUESTION:

Why does my program break with :

DummyAddDrink *someDrink = [[DummyAddDrink alloc] initWithNibName:@"DetailViewController" bundle:nil];
thorb65
  • 2,696
  • 2
  • 27
  • 37
MB_iOSDeveloper
  • 4,178
  • 4
  • 24
  • 36

1 Answers1

2

initWithNibName is used when your viewController is created with a .xib, and when you handle your views that way. Even then, you should probably avoid using this method unless absolutely necessary.

On the other hand instantiateViewControllerWithIdentifier is the preferred way of creating a new viewController when using storyboards, and the previous way will not work in that case.

Adis
  • 4,512
  • 2
  • 33
  • 40
  • So if I don't check the "With XIB for user interface" (when creating the corresponding ViewController .h & .m) I will not be able to use initWithNibName method? I ask this because I have seen similar methods, while not having any corresponding XIB files. – MB_iOSDeveloper Dec 18 '13 at 12:05
  • 1
    Not if your controller is referenced in a storyboard, I think. There might be a way around it, but I strongly discourage it. Consistency is the key, and mixing things around and using nonstandard code unless you have to ends up with a spaghetti code. – Adis Dec 18 '13 at 14:34
  • I'm puzzled as to why you would suggest that developers avoid calling the designated initializer for the 'UIViewController' class "unless absolutely necessary," as well as when you think that would be the case. – jlehr Dec 18 '13 at 23:49
  • 1
    This question and answer sums it up nicely: http://stackoverflow.com/questions/2224077/when-should-i-initialize-a-view-controller-using-initwithnibname – Adis Dec 18 '13 at 23:57
  • I agree with Rob & Adris. The explanation seems logical and correct. Beside, initWithNibName doesn't work like I used it. – MB_iOSDeveloper Dec 19 '13 at 12:19