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.
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)
Why is my IBoutlet not showing when I try to link a text field to the swift controller view it says insert action instead of IBOutlet. I'm not trying to add an action what is wrong with this new update Is anyone else having this issue.
In connection, currently, you have Action selected. Tap on it to open a dropdown and select Outlet.
Image Source : Here
UPDATE
in Type, you need to select your TextField Class, currently its set to Any. Once you change it, the outlet option will start showing.
Just copy this code and paste above viewDidLoad method.
#IBOutlet weak var passwordTextField: UITextField!
And click the small circle before this line and drag to your text field.
Note: Make sure you've set the view controller class name in storyboard
When you attach #IBActions (or #IBOutlets), you do not want to attach them to Exit Object on the View Controller.
What i get from your image is your class is not set
The problem might be that you are supposing you can form an action connection from a UITextfield in the scene of one view controller to a different view controller. You can't. You can form outlets and actions only between a view controller and the interface inside the same scene of your storyboard.
Make sure your View Controller's class is linked to your file, then go into the Assistant Editor making sure you selected Automatic.
If you have questions, let me know!
Verify that you are using Main.storyboard and not Launchscreen.storyboard
enter image description here
I can't change connection and object when drag and drop from Main.storyboard to ViewController.swift
I made a Google signIn system. So, I made a view for SignIn and set its custom class to .
And I drag and drop from Main.storyboard to ViewController.swift, But I can't change Connection and object.
Connection is fixed by 'Action' and Object is fixed by 'Exit'. I can't change.
Not only this time(this signin Button) but also every button or view.
how can i solve this problem?
I saw this reference and follow.
https://firebase.google.com/docs/auth/ios/google-signin
You added a View and not a Button to the ViewController. Fix that in the storyboard and try it again. (Search for 'Button' in the bottom right)
Oh, and what I just saw: You obviously didn't set the class of the whole View to GIDSignInButton, but only of the additional view you added. Select the white bar above the View you want to add the button to and then on the right go to the Identity Inspector (Or via Cmd + Alt + 3) and add GIDSignInButton there again.
Then you can start dragging from the button.
Or you write the #IBOutlet var buttonOutlet: UIButton! and
#IBAction func buttonPressed(_ sender: AnyObject) {
print("Button was pressed")
}
manually and connect if afterwards...
First of all your have to make sure you are dragging to the same view controller class as in your storyboard. To do that you have to choose correct view controller class in storyboard in utility section. In utility section identity inspector choose correct view controller class
Than you'll be able to drag to your view controller with no problem.
Let me know if it worked.
I have an existing view controller derived from UIViewController that I'm trying to drag a view into but the storyboard editor won't let me. If I create a new viewcontroller in storyboard it will let me and I notice it has some autolayout controls... something the original viewcontroller doesn't have.
I don't want to delete the original view controller and recreate since it already has a lot of the push segues and what not hooked up to it. How can I get it to allow me to drop views/tableviews onto the storyboard surface of the viewcontroller?
Have you try to :
Copy your controller into a new storyboard and test to add a view ?
Add other element ? Likee UIButton, UISwitch etc... Can you do it or not ?
Have you try to add it like that (drag & drop on view hierarchy) ?
Autolayout is a way to manage how views are placing comparing to others views.
You can activate or desactivate by clicking on your viewcontroller and by clicking this checkbox, it will affect to whole storyboard :
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.