How do you connect the "delegate" of the Picker Wheel to the interface? This seemed so simple in the tutorial, but in Xcode there is no "pickerview delegate" button I can use to drag to interface.
Welcome to stack overflow!
You need to connect the delegate to the pickerView, even though there is no spesific button for it. Just right-click the orange square box beside your interface builder, and press down CTRL while you drag 'New referencing outlet' to the picker. When you drop the line on the picker, there will be an option to add pickerView delegate and datasource! :)
You will need to implement the delegates in your header file aswell, like this:
#interface MainViewController : UIViewController <UIPickerViewDelegate,UIPickerViewDataSource>
Add your ViewController as a UIPickerViewDelegate and UIPickerViewDataSource like this:
#interface ViewController : UIViewController <UIPickerViewDelegate, UIPickerViewDataSource>
Those options should be there now.
Open your storyboard/xib file.
Open the Inspector window (Right most button in the view sections here
Open the connections inspector (Right most button in this area at the top of the inspector window)
Drag from the circle next to the "delegate" entry to what you want to be the delegate
Select picker, right click on it. Then you see a menu — list with circles on the left side.
Click and drag from circle opposite to delegate to the controller yellow icon.
Related
I am trying to add an outlet for a button, but when I control drag the button into the viewController class, it only allows me to create an action. In the screenshot you can see that the connection type is grayed out and stuck on action. Why isnt it letting me add an outlet? this same issue happens for every object like labels and text fields.
It is probably because you need to connect the storyboard vc to the file.
Click on the storyboard vc -> Click tab on right -> Enter name of view controller file into Class (Where it is highlighted in the image)
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)
I am unable to set a referencing outlet for a text field I have created. I am trying to do this via Ctrl-Drag. Can anyone advise? I have set the delegate as the View Controller. In general, I only have one view which is populated with a few text fields and I have written the following code for the ViewController.h which is what I'm trying to set up as a Referencing Outlet:
#property (weak, nonatomic) IBOutlet UITextField *userIDText;
First, make sure the control you're dragging from is the same class as the outlet (in this case, UITextField) or it could be a subclass of that class.
Second, make sure control's parent view is a subclass of the file you're dragging to.
Third, make sure the file you're dragging to is saved. This is the most common problem I run into. Go to the source file you're dragging to and push Cmd+S to save.
Fourth, when in doubt, Cmd+Shift+K to clean, Cmd+B to build, then try again. If that still doesn't work, restart Xcode. And if it's still not working, go back through the checklist.
In my case, to be able to create a referencing outlet,
I had to open the Storyboard.
Click on the top View Controller element (left panel in the Storyboard)
In the Identity Inspector (right panel), I had to select from the list my custom view controller class before I could drag and create the referencing outlet in it.
In my cace,(swift3)
1.right click on the storyboard.
2.Ctrl-Drag from black window and ReferencingOutlet to View on the storyboard.
enter image description here
I am almost completely new to Swift and Xcode so excuse my lack of knowledge in this field.
At the moment I have a view controller with a label in it. I want to add a view before that, with a text field in which a user enters a name, the app then goes to the second view with the label in which the name entered by the user is placed on the label.
I know this is very basic, but I cannot find any good solutions since I'm not quite sure what to search for. If anyone has a piece of code or a link to a good source, I'd love to see it.
Thanks!
Edit: I really know little about this. Do I need a navigation controller when using two views? Do I need two ViewController.swift files?
Here's how you do it in 12 easy steps:
1) Yes. I recommend using a Navigation Controller. Start a new project with a Single View Application.
2) Select the ViewController in the Storyboard and from the Menu bar above select Editor->Embed In->Navigation Controller.
3) Drag out a UIButton and a UITextField and put them in your first ViewController.
4) You'll want an IBOutlet to your text field so that you can read the text from it. Show the Assistant editor by clicking on the Tuxedo icon in the upper right corner of Xcode. Click on the ViewController in the Storyboard. The right editor pane will show the code for ViewController. Hold down the Control key and drag from the text field to the code in the right pane. Let go on the mouse button when you are in the space just below class ViewController : UIViewController {. In the popup, set Connection to Outlet, set the name to textField and press Connect. This will add the line #IBOutlet weak var textField: UITextField! to your ViewController.
5) Now it is time to add a second view controller. From the menu select File->New->File.... In the dialog, make sure Source under iOS is selected on the left, and choose Cocoa Touch Class and press Next. Name the class SecondViewController and make it a subclass of UIViewController. Press Next and then Create.
6) In the Storyboard, drag out a UIViewController and drop it to the right of the first ViewController. Select the new ViewController and open the Identity Inspector by clicking on the 3rd icon from the left in the row of icons below the Tuxedo icon. (Hint: if you hover of the icons it will tell you what they do). Change the class to SecondViewController.
7) In the Storyboard, control-drag (hold down control and drag) from the button in the first ViewController to the new SecondViewController. In the pop-up select Show as the Action Segue.
8) Drag a Label out and put it into the SecondViewController. Add an IBOutlet to it like you did in step 4 and call it mylabel.
9) In the code for SecondViewController add a new instance variable like so: var firstVCtext = ""
10) In the first ViewController add this method:
override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
let secondVC = segue.destinationViewController as SecondViewController
secondVC.firstVCtext = textField.text
}
11) In SecondViewController, add this line of code to the viewDidLoad method: mylabel.text = firstVCtext
12) Run the program!
Another good tutorial here with code and images:
http://www.jamesrambles.com/ios-swift-passing-data-between-viewcontrollers/
To move data from one view to another, use of a segue (pronounced seg-way) is the easiest. One example is here:
http://makeapppie.com/2014/07/01/swift-swift-using-segues-and-delegates-in-navigation-controllers-part-1-the-template/
If you Google for Swift and Segue you will likely find more examples.
I named the button "Reminders"
Apparently it was supposed to link itself to the files but nothing showed up in mine.
I have created a button in the storyboard but I don't know how to link it to a .m or .h file.
I'm really new to this and still learning. Am I supposed to change the settings of the button and what code do I write in the files to make it work?
Thanks a heap!
Here is how you add an IBOutlet to a header or a .m file:
Open your storyboard editor on one window. Position it in such a way that the button is visible
Open the header or the .m file in another window. Position the text in such a way so that the piece of code where you want the outlet added is visible
Highlight the button in the storyboard, and hold the Control key down.
Drag the button into the header or the .m file. You will see a blue line as you drag, and a blue marker where the outlet will be added.
When the marker is at the place where you want it, release the mouse button. The outlet will be inserted and connected to the storyboard.
Here is a post that illustrates this process with a video.