I created a second view controller in the storyboard. I also created a new Swift-file with File - New - Cocoa..... Now I want to associate that Swift-file with the second view controller. So I highlight the second view controller, and click Show Identity Inspector. In Custom Class in the Class field I normally would add the Swift-file in order to create i.e. outlets. However, I cannot find the newly created Swift-file in there.
Thats the code from the new Swift-file
import UIKit
class EditImageViewController: NSObject {
}
Why is that?
In your newly created Swift file. You need to declare a class which inherits from UIViewController.
Like:
class SomeViewController: UIViewController {
}
Then you will be able to connect IBOutlets and IBActions to this class.
If you are still having issues after your class inherits from UIViewController check the file inspector and ensure that the {your app name} target is checked (so that the file belongs to the correct module)
Related
This is a very beginner question as I am new to the swift language and working on my first app. I have been working in the default View Controller, which allows me to create outlets in the ViewController.swift file. but when I create a new view controller I am unable to attach outlets to the ViewController.swift file. so do I have to add a new .swift file for each view controller I add to the project?
You have to assign to your new controller in the storyboard a class (inside a .swift file), but you can have multiple controllers with the same class, just add a class to your controller here :
Example :
If you have a Test.swift like this :
//Test.swift
class viewController1: UIViewController {
}
class viewController2: UIViewController {
}
You could assign viewController1 or viewController2 inside your storyboard to your ViewController, however you should always have a single subclass of UIViewController inside your .swift file.
Definitely. Best practice is a separate file for each View Controller and other major classes.
Refer to this awesome free course from Stanford for a very nice introduction on MVC (Model-View-Controller).
I have following view controllers in my storyboard
I have created two custom class, which are RegisterViewController.swift and SignInViewController.swift, when I select my RegisterViewController in storyboard, I can see RegisterViewController.swift being assigned to my registerviewController, but when I create new custom class called SignInViewController.swift and when I try to assign it to the SignInview, it is not getting assigned however it is appearing in my custom class dropdown, just to make sure, if I select assistant editor, I see the RegisterViewController.swift code but it should be showing SignInViewController.swift code. What I am missing here?
This how my identity inspector looks when I select SignInViewController
This is my signinviewcontroller.swift
import UIKit
class SignInViewController: UIViewController {
//code
}
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.
I can't use the control drag function to hook up a button to action in .h when using storyboard with more than one view controller
Please help me...
Are you sure you've assigned the view controller a custom class? and dragging to that specific class? Each view controller should have its own Obj-c class. To create one:
press cmd + n and create a new Objective-C class and make it a subclass of UITableViewController (or the view controller you used in your Storyboard). after doing so, go to you Storyboard and select the view controller, on the right menu choose "Identity Inspector" and set the custom class name to the name of the class you just created.
Set the class file name for the UIViewController of the storyboard file at the identity inspector .
I want to change a TableViewController to a new UIViewController (PlayersViewController) from a UIViewController subclass, so I added two files (.h, .m), and then went back to the Identity Inspector/Custom Class.
Expected to find the PlayersViewController in the pulldown menu in Custom Class but could not find it, so I typed it in and hit "Return", but that didn't work either.
Am I doing something wrong?
Your new class should inherit from UITableViewController, not UIViewController. Only subclasses of the base class are available to select in the custom class field.