Xcode new scenes and classes [duplicate] - ios

I'm just starting out in xCode 6, and I can't seem to figure out how to add a new swift-file to the new view controllers I add in the interface builder.
Any tips on how to proceed would be appreciated.

Add a new Cocoa Touch class. Choose File->New->File... from the menu. Give this new class a name such as "ViewController2" and set the Subclass of: pop down to UIViewController. The file "ViewController2.swift" will be created.
Click on your new View Controller in Interface Builder. In the Identity Inspector (on the right in Xcode) set the Custom Class for the View Controller to "ViewController2".

If your custom class isn't showing up in the Identity Inspector, your files may have been moved or saved in the wrong folder. I would delete them from your project and try again, this time letting the files be saved wherever they are automatically. Good luck!

Related

New Swift Application Creates Obj C Files

I am a bit new to iOS development. Am I going crazy here?
I am using xCode to create a paged application. I select new project with the language as swift.
When I go to add a new view to the application it adds a view with Objective C?? Why? I want to use Swift. Am I doing something wrong?
Adding more screenshots, how do I get the second view to show me the Swift code instead of the "UIKit" code?
Working Side...
Not working
3:
It looks like your Assistant Editor is automatically opening the underlying Objective-C header files rather than the Swift files associated with your View Controllers. Try clicking on Automatic at the top of the Assistant editor, then hovering for a moment to see if your Swift file comes up (it should appear above or below the UIViewController.h file).
If you haven't created a subclass for the View Controller you've created in the storyboard, you'll need to do that first, then assign that class to the View Controller in the storyboard. To do this, select File > New > File…, select Cocoa Touch Class, and make it a subclass of UIViewController (don't create a XIB file). Then, in your storyboard, click on your newly-created View Controller (you can select it from within the view hierarchy on the left), open the Identity inspector, and set its Class to the subclass of UIViewController you just created.
If, for some reason, you've created a View Controller class and correctly associated it with the storyboard object and it's still not appearing, you can use the Manual selection in the Assistant editor to drill down to the file you want to appear.

How to create a view controller file after creating a new view controller? (xcode 8.21)

I'm trying to develop a tabbed activity app in xcode 8.21, and it only starts you out with two tabs. I want to make a third tab. I followed an online tutorial by adding another view controller and linking it to the tab bar controller. However, it doesn't automatically create a ViewController.swift file. All the tutorials are out of date (already), so I tried manually creating a .swift file called ThirdViewController.swift. Then, in the custom class section of the view controller, I tried typing ThirdViewController but it didn't work. Please help, I don't know what I'm doing wrong. Every tutorial I've seen online is already outdated.
You chose the wrong path to generate your file. In Xcode select "Cocoa Touch Class" instead of "Swift File" you can then decide if you want a Swift or Objective-C file with a specific Class (UIViewController in this case)

Can't drag and drop item from storyboard to viewcontroller in xcode

I started working with xcode a few days ago, and today I tried to connect a textview from the main storyboard to the viewcontroller, but I figured out that its not working.
I am able to ctrl+click to drag the item, but unable to place it in both viewcontroller.h and .m. I double checked that its viewcontroller and not UIViewcontroller, so this is not the case.
I'm using a Yosemite 10.10 mac.
Any kind of help would be appreciated.
Select the ViewController in storyboard and change the name of the ViewController to the name in the .h or .m file and then try ctrl+click and drag
For beginners -- > make sure you have kept the "Ctrl" key pressed down while dragging and dropping onto the code. This can be easily missed.
For me, the solution was to click on the "View" item in the dropdown menu that shows all the subviews for the controller.
Simply clicking on the controller image on the storyboard does not appear to properly "select" the controller for drag and drop abilities, the base view must be selected.
Make sure you're in Assistant Editor Mode (When code and layout editor are side-by-side).
From Apple:
With Interface Builder open in the standard editor pane, select the
control you want to configure, and click the Assistant Editor button
() in the workspace toolbar.
The assistant editor opens your object’s implementation file.
Control-drag from the control in Interface Builder to the
implementation file. (In the screenshot, the assistant editor displays
the implementation file of the view controller for the Warrior
button.) Xcode indicates where you can insert an action method in your
code.
For anyone who is new to XCode this picture might help more:
Select the item in Main.storyboard from the right pane and then set the class for the view in the right pane. Now you can ctrl + drag into your class
After trying all of the other answers, what finally fixed this issue for me is simply restarting Xcode and I was able to see the blue line that indicates where I can place my action method...
Note that dragging to any point in your code doesn't necessarily work. If you've been unable to drag to create the connection in one spot of your code, try dragging to another spot. For me, I was trying to drag to add a connection in the middle of a function which didn't work. Dragging to the beginning line of a function or in between functions did work.
Note: I'm on Xcode 9.1
Try selecting your view in the storyboard and clicking on the identity inspector on the right side in class. Enter your view controller name and then drag and drop the item from the storyboard to the view controller.
Hope it helps.
I had to both rename the class (from ViewController to ViewController2) of the ViewController.swift file and change the Custom Class (from ViewController to ViewController2) of the Main Storyboard's View Controller to make Ctrl-Drag-and-drop work again.
Renaming back to ViewController from ViewController 2 in ViewController.swift and Main Storyboard did not thwart the Ctrl-Drag-and-drop, it remained working afterward as well.
Hey I think you are not connected your storyboard viewController with your viewController class. check screen shot which "suhit" uploaded in his answer.
I would like to tell you something more than it.
When ever you need to connect any storyboard object with class.
first go to utility panel (right side) and then set class there.
after that you can connect your controls with class
I hope it will help you.
Check that the names on Viewcontroller identity inspector and the .h, .m files are the same. That worked for me
I'm using Xcode 9.2 I had my ViewControlled refactored into sections using the "extension" keyword. It appears you can't control drag into an extension of a ViewController. If you go up to the top part that is in the class definition, control dragging works fine (assuming you did the other things in this thread about making sure the right class is associated with the view controller.
I had same problem, I realised I selected wrong story board. If you made app with default settings you will get two story boards (Main and Launch Screen). Labels on Launch screen story board cannot drag drop.
Make sure that the item in question is connected to the file in which you want to control drag it to (specify the class in the identity inspector)

Having trouble associating additional view controllers to custom cocoa touch classes in Xcode 6

I'm a new user to SO and iOS programming.
When I'm in the Xcode 6 IDE and I create a new view controller and a new cocoa touch class, I'm having trouble associating the new view controller and the new custom class. As such, I cannot control-drag objects from the any view controller to the new .h or .m files.
I know that in Xcode 5 you were able to associate a view controller and a custom class through the identity inspector which I also see here, but I'm not able to add a new class, restart Xcode, and see new classes I make in this custom class dropdown as I saw people suggest for earlier versions of Xcode.
Also I should note that when I create new cocoa classes I intend to associate to view controllers, I'm inheriting from UIViewController as I saw suggested on another post about this topic but I am still not having success.
New class .h file:
#import <UIKit/UIKit.h>
#interface ViewController2 : UIViewController
#end
Thanks!
The easiest way to do this is to create your new class as a subclass of UIViewController, as you have already done. Make sure you are creating a Cocoa Touch class here.
In Storyboard editor, select your view controller (the first of the three icons), switch to the Identity Inspector and use the dropdown (or just type the name of your new class if it won't autocomplete) into the Class field.
To create outlets and actions in your subclass, switch to assistant mode, and keep storyboard editor on the left, and open your new viewcontroller's .m file on the right. Control drag from the UI element you want to create an action or outlet for, into the #implementation section, and release.

How can I rewire a MainView-iPad.xib to the viewController?

I am porting my Phone app to iPad.
I changed my target's "Devices" to Universal under "Summary".
I put the conditional (UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPad) to determine whether the app is running on iPad.
I created a new XIB dedicated for iPad and placed a view in there.
How can I rewire the IBOutlets back into the iPad version of the XIB?
Thanks
You probably need to define the class of the File's Owner:
Since you create a new XIB, it should be set to UIViewController, just select from the list your UIViewController's subclass.
The new image:
Just a small detail:
Avoid using this kind of stuff: (UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPad). It completely destroys your application architecture when creating a version for iPad and iPhone of the same application, which share the same code. Just sub-class the class you want. Keep the common code on the parent and the specifics in each sub-class.
select "Identity inspector" window on new Xib.
change class name to your viewController
then select file owner and your outlet automatically show up
there is the Controller class is same for both Xib
You don't need to rewrite any IBOutlets. But just assign the proper class to the xib's file owner and all the IBOutlets of that class is available to you and as you have used them in iPhone xib use them for iPad's xib.
Happy Coding :)

Resources