instantiateViewControllerWithIdentifier crashes app - ios5

I am trying to instantiate a ViewController defined in MainStoryBoard.storyboard using the code snippet shown in the pic below. I also assigned an identifier to the controller but app crashed with the error shown in pic saying "Storyboard doesn't contain a controller with identifier masterViewController" although you can clearly see in pic that its there.
Any help will be appreciated.
Just so you know, following solution didn't work for me: Crash with instantiateViewControllerWithIdentifier
Below is the snapshot that confirms that the storyboard object is same that is retrieved from viewcontroller and used to instantiateViewControllerWithIdentifer:.
SITUATION DESCRIPTION: I am trying to develop a custom SplitViewController (subclassed from UIViewController). This UIViewController is purely programmatic i.e. not based on IBInterface layout. However for its children i.e MasterViewController and DetailViewController I have made layouts in IBInterface. Now I am retrieving a UIStoryboard object in SplitViewController (purely programatic one) and passing it to a utility class shown here in the first pic that uses it to instantiate MasterViewController based on a layout in the Storyboard.

Hm, looks like if should work.
Try this:
Clean project and rebuild
Check if storyboard contains an instance of the correct storyboard
Fix by shaffooo (from comments below)
I rebuilt my storyboard and it fixed the issue I was having.

try to write the same name of your ViewController class into 'StoryBoard ID' in IB. Then edit the argument of your instantiateViewControllerWithIdentifier method with same string:
yourVCName *startingViewController = (yourVCName *)[self.storyboard instantiateViewControllerWithIdentifier:#"yourVCName"];

Related

PushViewController shows Black Screen

i have a problem with my App.
I´m not an experienced programmer, so maybe it´s just a simple solution. I thought this problem exists because i´m just trying things and play with my app so i made a new App and there´s the same problem.
When i push to a ViewController with navigationController?.pushViewController(PinkViewController(), animated: true).
I´m getting just a black screen. If i have a code like
Label.text = "String in viewdidload of the PinkViewController(). I get the following error at this line of code:
fatal error: Unexpectedly found nil while implicitly unwrapping an
Optional
I searched the web and i didn´t find any solutions for this problem. I hope you can help me.
The reason why it crashes is that your label is probably defined as an #IBOutlet that is connected to a UILabel in your storyboard's PinkViewController. However, when you instantiate PinkViewController with an empty constructor, you're not "using the storyboard-way" and your label outlet (which is non-optional, because it's likely to have an exclamation mark there) could not have been connected to the storyboard instance of your view controller.
So what you should do is one of these options:
If you defined PinkViewController in the storyboard, you'd have to instantiate it in a nib kind of way, for example:
In your Storyboard, select the view controller and then on the right side in the Identity Inspector, provide a Storyboard ID. In code you can then call this to instantiate your view controller and then push it:
let pinkViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "yourIdentifier")
Even better, you create a Segue in the storyboard by control-dragging from the current VC to PinkViewController. There's lots of tutorials for creating Segues online. Don't forget to provide an Identifier for the Segue in the Storyboard, if you want to trigger it programmatically. This can be done by calling
self.performSegue(withIdentifier: "yourIdentifier", sender: nil)
If you want to trigger that navigation upon a button click, you can even drag the Segue from the button to PinkViewController, that way you wouldn't even need to call it in code.
If you defined PinkViewController programmatically only (by creating a class named like that which conforms to UIViewController), you might wanna instantiate it with PinkViewController(nibName: nil, bundle: nil) (notice the arguments instead of an empty constructor) and then push it with your provided code.
Hope that helps, if not, please provide further code / project insight.

NSUnkownException : this class is not key value coding-compliant for the key piewTextField

I am developing my app for Deployment on ios5 with Xcode 4.6 .I have a singleview app i am creating simply a new objective-c class for a new View Controller
name 'PiewController' i have defined the PiewController.h file like this:
and my PiewController.m looks like this
and my PiewController.xib Looks like this
and the connections of File's Owner are:
and the Error it is giving when i have loaded PiewController Directly from AppDelegate
and the AppDelegate looks like
Hmm it seems you want to connect the PViewController's xib stuffs to the ViewController. The debug message, i think, can be interpreted as : there is no property like piewTextField in ViewController (which is true, since you defined it in pviewController). Are you sure you load the correct xib for pviewController and not for the default initialized ViewController in AppDelegate.m?
Turns Out i forgot to include PiewController in the AppDelegate
With the Following changes it works perfectly

iOS add rootViewController to window causes delegate not found error

I'm creating an application that first loads a settings screen which displays a series of text fields and labels asking the user for input. This is all working fine.
What I then want to do is once this data has been input, it comes up with the main application interface.
What is happening though is that when I'm telling the application delegate to load the main view, it says that the viewController isn't key value complaint for the key delegate.
The code I'm using to create the viewController is:
CustomViewController *viewController = [[CustomViewController alloc] initWithNibName:#"CustomViewController" bundle:nil];
self.window.rootViewController = viewController;
If anyone thinks that UIWindow doesn't have a rootViewController property, please check the documentation. That's what I did, and it does have one.
Any help with figuring this out would be greatly appreciated.
For those that like full debug info, this is what I get from xcode.
2011-06-18 15:03:15.474 Some App[15596:207] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<CustomViewController 0x53368b0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key delegate.'
Thanks,
Matt.
Most likely you try to use delegate somewhere in your xib file, but it doesn't exist in your CustomViewController class.
Check the connections in your nib file and remove the one that connects to the non existing delegate.
The rootViewController property was only recently introduced and might not be available on devices running an older version of iOS.
You want to have a UINavigationController as the root view controller of your application and subsequent pages you simply push onto it. If you don't want animation, then do animate:NO. If you don't need a navigation bar, then hide that as well.
It is generally preferable to use one of the existing container view controllers over swapping them out yourself.

Passing info to a delegate from a UIActionSheet

My understanding about passing data back to a delegate completely revolves around making a new view controller and having it conform to a protocol.
I am trying to get a time input back from a UIDatePicker set with the UIDatePickerModeCountDownTimer mode and I am running into issues.
Inside of Main1.m I create a UIActionSheet *action, and set everything up so that it presents itself with a UIDatePicker on a click. Inside of Main.m I also say:
main.delegate = self;
If this were not a UIActionSheet, I would make a protocol reference inside the new ViewController and then have the new vc pass data to a method that Main has, but I can't do that with a UIActionSheet!!
What am I missing? I assume there is something inherently different about Action Sheets, ut I can't figure it out.
Thanks for your help!!!
EDIT:
Sorry! I was giving a generic name to my view controller. It isn't actually Main.m, its FirstViewController.h/m
I didn't realize that my generic reference was getting mixed up with the Main.m file that is completely different than a vc.
I don't exactly understand why you're putting your delegate assignment in Main.m. I assume that you're setting up your UIActionSheet in a ViewController, and launching it from there. In this case, your ViewController is your delegate object. So you need to make sure that your ViewController implements the UIActionSheetDelegate. ie:
#interface SomeController : UIViewController <UIActionSheetDelegate>
Then you simply implement the required methods of that delegate in your view controller class, and that should do it. If I'm missing something about how you're implementing this, then you need to provide more code examples to examine.

MKMapView will not accept an annotation

This is hard to explain so bear with me ... I'm using an example that I know works. It's the MapMe chapter from the Apress More iPhone dev book.
I'm using the same code but I've created a MapViewController class to contain all the map code and am inserting the view from the nib as a subview of my MainViewController view. With me?
My problem seems to be this : now the view containing the MKMapView object is a subview of the MainViewController it doesn't want to add the annotation object into the view. I can change the mapType and can initiate the locationManager:didUpdateToLocation to pinpoint my location. Indeed it drops the default red pin for me. My custom annotation object is not passed though.
Remember, this code worked fine when run directly from the MapViewController. It only breaks when I insert the view as a subview of another ViewController.
Ok, I'm an idiot ... the delegate wasn't set in the NIB ... DOH!!! Schoolboy error. Lesson learned.