4

In my Xcode project, I have several view controllers in my storyboard. How can I see the code for a specific view controller?

Because each view controller will need to have different functions from the others. So, where can I write that code?

Thanks!

6 Answers6

6

By default, storyboard will use UIViewController for the view controllers you use.

However you can change the class used for the view controller by:

  1. selecting the view controller (in Interface Builder)
  2. show the identity inspector (View > Utilities > Show identity inspector)
  3. Change the value in Class to "MyOwnViewController"

Where "MyOwnViewController" is the name of the UIViewController subclass that you want to use for that view controller.

sch
  • 27,436
  • 3
  • 68
  • 83
  • I mean, where can I see the code? I'm looking in the .h and the .m. I don't see any code that has anything to do with my viewcontroller. I've added labels and other things using the interface builder. But the h/m files haven't changed. I should be able to edit what's in the view controller, not only via the interface builder, right? But I should be able to edit it's code directly. But where? –  Feb 25 '12 at 08:43
  • No, the design you do in interface builder doesn't impact the view controller. The view controller, as its name implies, is not the view, but it controls a view (it has a property view) and that view can be created programmatically, or in IB without code. However, you still can do both: have some view elements created in IB and others in the view controller. – sch Feb 25 '12 at 08:51
  • @Dingredient - What do you mean by: *" I should be able to edit what's in the view controller"*? you mean add UI or add functionality such as actions, reading data, ... – sch Feb 25 '12 at 09:03
  • Thanks for your reply. Sorry for the confusion. My situation is this: I downloaded a sample project "ScareCam" from apple's dev site. It uses the camera to detect faces in real time. Looking over the functions in the .m, I see variables like "faceRect" and "bounds." I wanted to find out which variable hold the actual coordinates of the detected face. So, using the interface builder, I created a label, which I'd like to layer on top of everything for the moment, to display the value of those variables... In the code, I can't find my label, so how can I set it to show a variable? –  Feb 25 '12 at 09:52
  • You should add an IBoutlet in the view controller and link it to the label in interface builder. – sch Feb 25 '12 at 10:10
3
  1. Select the View Controller you want to see the code
  2. Go to the Assistant editor

It's on the upper bar to the right.

Community
  • 1
  • 1
max lau
  • 31
  • 1
3

The "Show the Assistant editor" venn diagram icon on the top right did it for me. Xcode Version 6.4

pradope
  • 31
  • 3
2

Select the storyboard in the navigator, right click and select 'Open As'. You should see 2 options 'Interface Builder - Storyboard' or 'Source Code' - select the Source Code option should fix your issue.

Arnab
  • 4,216
  • 2
  • 28
  • 50
Bob
  • 21
  • 1
0

You subclass each UIViewController to add unique functionality. The implementation of UIViewController is private to Apple, the interface is described in it's documentation.

zaph
  • 111,848
  • 21
  • 189
  • 228
  • But where is the code? I commented on sch's answer too. I should be able to edit what's in the view controller, not only via the interface builder, right? I should be able to edit it's source code directly. But where? Its not in .h or .m... –  Feb 25 '12 at 08:45
-1

We start with multiple scenes in our storyboard.

see picture

  1. Create new view controller file Create new swift file For our custom view controller

see picture

  1. Setup file In our new file, Type in code To set up class structure

see picture

  1. link file to view controller in Storyboard In Storyboard, Click on our view controller

pic a

pic b

pic c

In the Identity Inspector, In the Class field, Type in the name of the new view controller file we created in step 1

  1. open Assistant view

see pic

  1. Done! We are ready to write code In our view controller That is connected to our view controller in the Storyboard

see pic