iOS7 Storyboards: App crashes after calling addSubview - objective-c

I am working with the iOS book "Hello iOS Development". I coded along the book and just finished chapter 4. After implementing everything like in the book my app crashes.
I guess it's because I used Storyboards (Xcode 5 and iOS7), the author uses XIB files. Here's an image of the error:
What does the error mean? What's wrong with it? Does the addSubview refer to the XIB files I don't use due to Storyboards?
I'd really appreciate your help with this!

You cannot simply alloc] init a view controller whose view is defined in a storyboard or XIB file, as you have. Doing so will not yield what you expect.
If you are using a storyboard, you should give your card view controller an identifier in the storyboard, and then [self.storyboard instantiateViewControllerWithIdentifier:#"_CardViewControllerIdentifier"];
This will create a view controller with a view as set in the storyboard.
If you do not use a storyboard, but a xib file, you should use initWithNibName:bundle: to instantiate your view controller.

Related

Storyboard doesn't contain a controller with identifier 'MainWindow'

I am trying to instantiate an NSWindowController from a storyboard in my OS X app:
NSWindowController *mainWindowController = [[NSStoryboard storyboardWithName:#"Main" bundle:nil] instantiateControllerWithIdentifier:#"MainWindow"];
But this is what I'm getting:
Storyboard (<NSStoryboard: 0x618000008500>) doesn't contain a controller with identifier 'MainWindow'.
But in the storyboard:
It is set. No, there are no other storyboards. Yes, I've tried cleaning the project and the build folder, but nothing changes.
Why would I get this error?
UPDATE: I am building my app, coming back to the storyboard, and I see that Storyboard ID is unset:
How can Storyboard ID get unset by itself?
It was definitely a bug with Xcode. Restarting Xcode solved the problem.

Adding a UIViewController to a UIView

I have ViewcontrollerA , which is a view made in storyboard.
In this view, i have a UIView that i have added in storyboard and create an outlet to it called containerView.
I wants to add some other viewControllerB (also made in storyboard) to the container.
Tried that :
//add to container a new view from storyboard,with id called serviceView
UIViewController *sv = [self.storyboard instantiateViewControllerWithIdentifier:#"ServiceView"];
[self.containerView addSubview:sv.view];
and got a crash.
How can i do that ?
Thanks .
It most likely crashed because sv is nil, though you should have posted what reason it gave for the crash in your question. If this is the reason, it's probably because your storyboard doesn't contain a view controller with that storyboard identifier. But even if you fixed this, this isn't the proper way of doing view controller containment.
As for adding a view controller to another view controller, you need to use the view controller containment API added in iOS 5. In fact you can even use the storyboard to do view controller containment without writing code. From the Object Library, you can drag out a "Container View". It will let you attach another view controller from your storyboard graphically.

How to connect a storyboard to a .h and .m

I have in an app a viewController without a .xib, because I want to use with that ViewController a storyboard instead. The problem is that I don't know how to tell Xcode that ViewController2.storyboard is the view of ViewController2.h and ViewController2.m. Anyone knows it?
When you use storyboards, you generally instantiate the view controller inside the storyboard. The big advantage that storyboards have over .xibs is that they model not just the view hierarchy but also the flow from one view controller to the next. So, you'll probably want to either instantiate your view controller in the storyboard where you plan to use it, or else just go back to loading the view from a .xib (which isn't all that different anyway).

viewWillAppear and viewWillDisappear callbacks when a ViewController's view is hidden by another view & UIViewController Containment in iOS5

The UIViewController docs mentions about -viewWillDisappear:
"This method is called in response to a view being removed from its
window or covered by another view. This method is called before
the view is actually removed or covered and before any animations are
configured."
In iOS 4.3 and lower we are supposed to present a viewController and not add a viewController's view to the view hierarchy explicitly, so the calls -viewWillDisappear or -viewDidDisappear would be triggered when a new view controller is being presented over the existing view, in which case 'covered by another view' is true! But what if a viewController's view is hidden since another view obstructs the viewController's view? Do we get these callbacks?
Well, in iOS 5 there is a UIViewController containment concept where views can be directly added as subviews in view hierarchy by setting the parent-child relationship between viewControllers. So, unlike <= 4.3 OS, -viewWillDisappear and -viewDidDisappear calls should ideally be triggered when a viewController's view is obstructed or covered by some other view, which I have verified by a sample project that it is not happening in SDK 5.0.
Has anyone found this problem related to these callbacks?
Or, is my understanding correct?
Thanks,
Raj
Someone has the same kind of problem here :
iOS 5 : -viewWillAppear is not called after dismissing the modal in iPad
You should read the answers, I found them very interesting.

Stick UISplitViewController in its own XIB?

my iPad app starts with a normal UIView showing a login. After the user logged in the screen is supposed to switch to a split view.
XCode's SplitViewTemplate (and all examples on the web I found) however, place UISplitViewController in the main window's xib and define an outlet in the app delegate.
I find that unlogical in my case because I don't need the controller at startup and would like to (following Apple's guidelines) place the split view controller in its own XIB.
Has anybody a working example or a small step by step instruction? I always end up with the XIB not being loaded.
Or is it just NOT possible? But why would it not?
René
You can put a UISplitViewController into a different XIB. You cannot have it be the owner, but you can have your app's delegate be the owner and load it when it removes the login view.
Add UISplitViewController IBOutlet to app delegate
Create a new, empty XIB for iPad
Set the File's Owner to your app delegate class
Add a UISplitViewController, connect to outlet in delegate
Add views to split vew controller
Then, you just have to handle your login in the app delegate, load the new XIB, and display it.
maybe not the answer to your question but I have in my App also a login window. What I do is to put the login view above - in my case - TabBar.
I found this post. Best way to switch between UISplitViewController and other view controllers?
It seems it really is not supposed to work. You have to put it in the main xib and then apply tricks...