Why doesn't xcode let me create an IBOutlet - ios

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)

Related

Unable to Set Referencing Outlet

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

Use entered text from view in other view in Swift (Xcode 6)

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.

how can i get the object of a button created with storyboard

I created a button on a view controller by dragging the icon of button in storyboard. I thought this would generate an object. is there an approach that I can get the id, i.e. the object of that button? Is there a method that we designate a label or identifier for that button and we can refer to it with certain methods?
Thanks in advance for answering.
When you drag a button from the object library onto your storyboard's scene, when that scene is instantiated at runtime, the object associated for that button will be created. If you want to have a reference to that object so you can interact with it programmatically, you can open the assistant editor:
You will want to make sure you set the custom class for your view controller and you simplify your life a little if you tell your assistant editor to "automatically" select choose the right associated .h file:
You can then control-drag (or right-click-drag) from the button down to assistant editor window and create either an outlet (an IBOutlet) or an action (an IBAction):
By doing that, you then have a reference to the button object that is created when the scene is instantiated.
Open Assistan Editor (Split View, so that you see Interface Builder on the Left and Your View Controller on the right.)
Ctrl-Klick & Drag on the button and drag it to the Controller (you'll see a blue line).
An IBOutlet will be generated in your ViewController and you can access it.

How do I add a custom class to a storyboard?

I am currently trying to implement a custom radio button class that I have been messing with (located at https://github.com/t4ku/RadioButtonWithUIKit). This radio button solution draws everything programmatically.
I was wondering if there is a similar solution using buttons that were added via Storyboard. I already have my design exactly how I want it through Storyboard and would much rather somehow "link" the buttons with my radio button class.
Any newer ideas or suggestions would be greatly appreciated.
You can change the class of an object on Storyboard in the identity inspector (you can manually enter the class instead of relying on the drop down menu). You can then link the object however you like.
If Storyboard doesnt accept the RadioButton class for some reason you could also create a custom class of UIButton that would inherit from the RadioButton class and then use that in the same way.
That control is based on UIView, so in your storyboard just add a new View object, select it, and then set the class (at the top of the image) to RadioButton:
You won't be able to see anything but a blank view where you place it, but it will work properly when you launch the application. This will at least make it easier to resize, position, etc.
If I understand your question right, to refer to a button added to a storyboard programmatically, you have to link the variable name in your .h file to the actual button in your .xib file. If you add the following line to your .h file
#property (nonatomic) IBOutlet UIButton *theButton;
Then when you return to the .xib, there should a yellow cube (file's owner) to the left of the storyboard panel. If you right click on that cube, you should get a list of outlets. At the end of the line that has your button's name, there should a circle. If you Ctrl+Click and Hold and drag it to the button in the storyboard, it should like the variable in the .h file to the button in the storyboard.
So now when you refer to the variable name you created, you will be referring to the button in the storyboard.

IOS - control-drag of a button is not working

I have some buttons I am trying to create, and when I try to do a control-drag into the controller, it isn't showing me the options to create configure that button. Any idea what could be wrong? I am just following my first tutorial is its probably something for newbies.
My screen shot is attached.
It seems that you opened an incorrect .h file in your assistant editor, you will need to open the correct interface file for the correct view controller you have in your nib
from what i can see is that LearningCenter should not be the interface opened,
You will need to change the assistant view controller to the correct one
Check the screen shot
just right click on your button from that select last option (New Referencing outlet collections. from that map your .h file . At the time a popup will appear. just enter your button name.

Resources