I am have multiple view controller in my storyboard with same class called "viewcontroller.swift " and they crash but if I assign operate classes they don't crash how do i fix this?
Simple answer for that is you can not use single ViewController.swift for multiple view controllers in your storyboard. Just create a new class for every new view controller in storyboard.
I tried same ViewController sub class in Obj-C for multiple storyboard view controllers and mentioned storyboard id for each ViewController and it worked fine, no crashes occur. You can try this in Swift as well.
Related
I'm new to iOS development and this could be a stupid question for some experienced guys...
When I create a new iOS project in Xcode, I get a ViewController class and a storyboard which sets its Custom Class to this ViewController. It looks like there is a "Storyboard" class holding an instance of ViewController, however, I cannot find where this "Storyboard" class is defined.
Even though I know how to create multiple subclasses of UIViewController to handle different views interaction following some tutorials, I still find it uncomfortable to associate these subclasses to the storyboard by selecting them in the storyboard panel. I would rather see something like a "Storyboard" class holding an array of UIViewController.
So my question is, how these UIViewController interact with the Storyboard under the hood?
Thanks
Roughly, it happens as follows:
App launches.
App loads storryboard.
Depending on app's navigation structure, app instantiates each view controller inside the storyboard as needed.
The storyboard contains detailed information on:
Which custom subclass of UIViewController, UINavigtionController, etc. to use for instantiating each view controller in the storyboard.
How to map (connect) each if its view controllers' subviews to the corresponding custom classes' outlets and actions.
But seriously, read Apple's docs. It's all there.
UIViewController has a property named storyboard which refers to the storyboard file associated with the viewcontroller subclass.
Also the view controller code are interecting with the storyboard with connections symboled with #IBOutlet and #IBAction.
I have been going through this tutorial, "Option 2: Prototype Cells"
I implimented all the steps, but, the following code gives me an error:
#interface MTViewController : UITableViewController
#end
the error message is:
instantiated view controller with identifier "UIViewController-BYZ-38-t0r" from storyboard "Main", but didn't get a UITableView.'
Here is what I tried - I changed UITableViewController to UIViewController and the error went away. But, the tableview remains blank. Can you tell me why this is ?
In your storyboard, you need to make the root view of your UITableViewController a UITableView.
Try changing your super class to UIViewController or change the controller in your storyboard to TableView Controller
#interface MTViewController : UIViewController, UITableViewDataSource, UITableViewDelegate
#end
Drag a new instance of Table View Controller into your storyboard, copy the cells you created in your previous controller into the new one.
This error occurred probably because you used the default UIViewController created by XCode which isn't a UITableViewController.
I met exactly the same issue as you.
You're probably using a UIViewController instead of a UITableViewController in the storyboard.
NOTE:
Only subclass a custom UITableViewController in the storyboard is not enough. You'll see the difference:
difference between UIViewController and UITableViewController
So you have to drag out a UITableViewController from the library, then move all things(cells) to it. Don't forget to check bindings and auto layouts if broken.
And of course you cannot move the tableview to the root of UIViewController, it just doesn't work like that. =)
Actually you are using UIViewController not UITableViewController. Delete your default view controller and drag new TableViewController in your storyboard. Because your ViewController not having TableView, but it's trying to load cells.
Simple solution is : Delete ViewController and add TableViewController
Then you have to change its class name to MTViewController or Your Class Name
I've had this problem too. I created originally plain UIViewController then i decided to rather use UITableViewController. Problem is that i forgot to set storyboard ID on new controller, and change it to something else on the old controller, otherwise you will instantiate the old controller from storyboard and this problem will occur.
In the storyboard just change the class of your ViewController to your new class name which here is MTViewController - image of steps
make sure the dataSource is set to self.
In my project I have two view controllers, and I am having trouble connecting objects such as an UIImageView to the view controller. When I try to create the IBOutlet, it tells me that "Could not insert new outlet collection: could not find any information for the class named UIViewController". I believe this problem stems from the fact that my original declaration of my class is as follows:
class UIViewController: UIViewController {
when in fact the view controller is named mainScene instead. However, when I change the first UIViewController to what I think it should be (mainScene), it doesn't even show me the option of connecting an IBOutlet...
class mainScene: UIViewController {
So, I have two questions.
Do I need to have a whole separate class for the second UIViewController and would that solve my issues?
Is there a better way to link objects to the UIViewController or am I just doing something horribly wrong (the likely scenario)?
Thanks so much
Short answer: 1. Yes, and yes. 2. There's no better way, and you're not doing something horribly wrong. (You probably just missed a step.)
You have two view controllers. Assuming they are different, you would subclass each one from UIViewController with a different name. E.g., mainSceneViewController and otherSceneViewController.
Your mainSceneViewController and otherSceneViewController would each have their own properties and IBOutlets.
Where you're probably stuck, is needing to change the class of your viewController within Interface Builder to match the class name in its .swift file, so IB knows what outlets it can connect for that view controller.
Each scene in your storyboard corresponds to a view controller. When the segue is performed, iOS instantiates your view controller from the storyboard.
While it is possible to only have one view controller subclass, and use the same subclass for different views, it doesn't happen too often.
Update:
Subclassing lets you add properties and methods to a class, and override their superclass.
In your comment, UIViewController is the class, and mainSceneViewController is subclassed from UIViewController. For your second view controller, it would likely be class otherSceneViewController: UIViewController {, as your other scene would likely require a different properties and methods from your main scene view controller.
You can read more about Inheritance in the Swift Programming Language guide.
I have a storyboard with a navigation controller that leads to an UIVIewController that I want to reuse. That UIVIewController has a ParentUIViewController that has all the basic functionalities for all the UIVIewControllers that I am reusing.
Currently I am copying and pasting (meh) and then I change the class of the UIViewController to the ChildUIVIewController that I want to use (ChildUIViewController extends ParentUIViewController).
But this sounds like a bad solution. Everytime I want to change the ParentViewController visually I need to update, manually, all other ChildViewControllers.
I have tried to create a xib for the ParentViewController but the xib isn't loaded because I need a xib with the name of the ChildViewController. I have created it and then said the class is the ParentViewController but it crashes in the segue.
EDIT
I have created an example of the status of my problem
https://github.com/tiagoalmeida/storyboardexample
Note that the ParentViewController has a set of logic way more complicated that is not illustrated there. Also note that I am also using a TableView. I hope that this can illustrate the problem.
Keep the logic on the parentViewController and the UI Part on the child UIViewControllers. If you need to create a new UIViewController, you will create a child that will have a corresponding XIB (or get rid of XIBs and create the interface by hand).
Have you considered looping back into the same UIViewController via a "phantom button"?
Have a look at this: UIStoryboard Power Drill, Batteries included
Essentially you can drag a Bar Button Item into the little black bar under the View Controller in Storyboard (the 1 with View Controller, First Responder, and Exit icons; sorry, I don't recall what this is called exactly), then you can control+drag from that button back into the UIViewController for a Push segue. This should create a loop segue in your Storyboard. All you need to do next is give that segue an identifier, programmatically call it from your code using [self performSegueWithIdentifier:], then implement -(void)prepareForSegue: and use [segue destinationViewController] to conditionally set the title and perhaps some flags so you can identify when to use different kinds of fetches (or other code variations) in the same Class code.
Im using story boards and in the Custom Class panel of the specific view controller if i give the name of the class that contains code for that view controller i get a blank screen. What could be the problem? Ive tried everything.
SO basically Class is usually UIViewController in CustomClass. I changed it to MenuScreenViewController(a ViewController objective C class that has code associated with this) and im calling it modally. Why do i get a blank screen. Urgent help please.
My guess, once again:
remove the implementation of - (void)loadView from your UIViewController subclass. You only need this method when you create your viewControllers view in code. When you create the view from a nib file or storyboard you must not have this method.
But it's not your fault, the implementation is there because of a bug in the Xcode 4.3 viewController templates.
EDIT: This bug is fixed in Xcode 4.3.2. The template does no longer contain - (void)loadView