I am using Xcode 9.1 and trying to build a program. I have designed one textbook , one label and one button. Now I am trying to place the textbook in view controller to code it, But Unable to do this.
In your view controller, you want to setup an outlet. Add a property like
#IBOutlet var textField: UITextField?
In the interface you have screenshot, a small circle should then appear next to it, which you want to drag over to connect to your text field. This creates a binding between the property in your controller and the view element.
You can then reference this property in your controller to access the text field.
Related
I know the second button here shows the Assistant editor:
And I know how to make an Outlet and Action by Control dragging from an object in the Interface Builder to the View Controller code. Ideally it should be as easy as the documentation makes it look:
But this is usually what Xcode actually looks like for me when I press the "Assistant" editor:
A mess. I have to minimize lots of things, try to get the storyboard object in view, and then go find the right View Controller. All this before I can do the Control-drag.
Is there a way to make the connection without using the Assistant editor? (And preferably without having to type a lot of code in myself.)
Don't press the assistant editor button. Sometimes it opens a random file instead of the one you want.
When you are in Storyboard, Option click on the .h file that you want to open in the Project Navigator. This will open the proper .h file to add the outlets or actions.
When you're done, close the Assistant editor right pane (which is displaying the .h file) and you will be back in Storyboard.
Yes, you can do it without the Assistant editor and without writing lots of code. It requires learning to do two things:
Use code snippets
Use the Connections inspector
Create code snippets for the IBOutlet and IBAction
Normally when you create an IBOutlet and IBAction with the Assistant editor it automatically adds code like this to your View Controller:
#IBOutlet weak var myOutletName: UIButton!
#IBAction func myActionName(sender: AnyObject) {
}
You could type this all yourself and then add the connection in the Connection inspector, but why do all that typing? Just make a code snippet that will autocomplete. Drag the code to the Code Snippets library in the Utility panel.
Give it a title and most importantly, a Completion Shortcut. I called mine "ibaction" for the #IBAction code.
Now all I have to do is start typing "ibaction" and I can autocomplete the code snippet. I only have to edit the name of the action. It is a similar process for the Outlet.
Read more about creating code snippets:
Xcode Snippets
Creating a Custom Code Snippet
Now all you have to do is connect the IB object to the code.
Make the connection with the Connections inspector
First, click the object in the storyboard that you want to connect. For me, I am using a Button.
Then select the Connections inspector. It is on the far right.
Alternatively, you can right click or control click the object to get a menu.
Then click the New Referencing Outlet to connect it to your Outlet or the Touch Up Inside under Sent Events to connect it to your Action.
For whatever reason I find that sometimes I need to drag just a little bit after clicking the "+" button to get the menu of available connections to show up.
I have created a custom view (Quantity View) with nib file in Swift. I have created some IBOutlets & IBActions (for buttons, labels etc.) in my custom view.
I tried to use this custom view (Quantity View) by assigning class name to a UIView in my storyboard.
It's showing me all the IBOutlets & IBActions in the Connections Inspector, as shown in this screenshot: .
I just want to show only delegate for the Custom view.
Possible Answer:
I thought I can use the -viewWithTag to get the views instead of Outlets.
But, I want to know if it's possible with having Outlets also or if there is much better way to do this?
What are the other possible ways (optimum) to handle this situation?
You can also consider the following solution:
You can take the subviews of your QuantityViews(custom view) and you can identify the specific views by its frame origin.
Note : you should know the customview subviews frame
Its not possible to hide IBOutlets from storyboard if you declare the class members as IBs (IBOutlets or IBActions).
The IBOutlets or the IBActions are just indicators to the interface builder so that it can show the names on it when you try to bind them it actually calls the setValue: forKey: method to set the view's reference to the IBOutlet property.
Now if you try to access an subview from the file's owner class without any IBoutlets you need to have a pointer to point it, so for that either you can get the reference using ObjectID which is assigned to the subview by the interface builder or you can get it using the viewWithTag: method.
The ObjectID you need to find all time when you add or replace a subview from the view, so better and convenient approach is to use tag property of UIView class.
So my conclusion to this problem is to access the views using the viewWithTag method you mentioned earlier.
I think your way is correct. But sometimes Xcode doesn't work correctly.
The following makes the IBOutlets and IBActions reappear and work properly:
Clean project your project in Xcode.
Quit Xcode completely.
Delete all contents of ~/Library/Developer/Xcode/DerivedData/.
Restart MacOS just in case.
I hope you will resolve that :)
I'm targeting IOS 8+.
I have a form that is used in more than one place. So I decided to create a custom view where I define the various "form" text fields.
I have built my XIB, and the UIView subclass contains the outlets for each textField.
The view is composed of a background image and a scroll with the form fields over it.
Now, my first obstacle was: I need to have this custom view in a container that may or may not have a navigation bar. This made me create a constraint outlet so I could update its value to push down the scroller view. This way I'd have the whole image in the frame, the top being behind the navbar and the scroller bellow the nav bar).
Here's a manual drawing to help understanding the problem.
It's very possible that I'm making a lot of mess and confusion on my way to solve this. :)
The problem is:
After awakeFromNib runs I have no access to the constraint property. I then noticed the same thing happens for the TextFields outlets.
So, how can I access the custom view's properties when I instantiate them programatically?
Something like:
Controller:
let customView = SignupView(frame: f)
view.addSubview(customView)
customView.pushScrollerDownBy(50.0)
Custom view:
func pushScrollerDownBy(yOffset: CGFloat) {
//topScrollerConstraint is the outlet for the textField.
topScrollerConstraint.constant = yOffset //right now topScrollerConstraint is nil.
}
You should check if you have connected your topScrollerConstraint to the file's owner since it will not get instantiated and therefore, error. Here is a recent SO question regarding difference between these two:
What is File’s owner in XIB in this case?
I'm trying to set up a basic PNChart PNCircleChart on my app. I created a UIView in the storyboard and set the class to PNCircleChart. The restoration ID is piechart. I created an outlet in to my view controller that looks like:
#IBOutlet weak var pie: PNCircleChart!
Now I'm trying to build a basic piechart in my viewdidload and I can't figure out how.
pie.backgroundColor = UIColor.greenColor()
pie.setValue(50, forKey: "A")
pie.setValue(50, forKey: "B")
pie.strokeChart()
It is failing with a NSUnkownKeyException error. Any idea how to do this in swift
The NSUnknownKeyException is almost always caused when one of your interface elements was hooked up to a property in your view controller that you then deleted. The property was deleted from the view controller, but the interface builder was never unhooked from that property.
The actual exception message should tell you what "key" it's having trouble finding, which should give you a hint as to which UI element is hooked up wrong.
Once you figure out which key is hooked up wrong, go through your UI elements and right click on each of them. One of them will be hooked up to a property of this name (and it won't exist in your class source code file). You need to either click the X for this connection to delete the connection, or you need to add the property back to your source code file.
I'm having trouble updating the UITextField for an iPhone app. I've set the layout with the Interface Builder, created an instance of the text field in the ViewController, and set the ViewController as the delegate for the text field.
The text field object in code doesn't seem to be responding when I enter in information and press Done.
Does anyone have any ideas why its not working?
If you are creating the text field in interface builder, you don't need to also alloc and init it in code. Link the text field to files owner in IB (I'm assuming files owner is your view controller) as the delegate. If you need to refer to it specifically, also create and outlet in your view controller and link that to your text field. This is covered in the most basic tutorial apps in the docs.
To respond when the done button is pressed, implement the textFieldShouldReturn method from the UITextFieldDelegate protocol. Resign first responder in that method and return YES.
Is your text field connected to the IBOutlet in your code? Maybe if you post some related code it would be helpful.