How add IBOutlet to class in embedded TableView - swift

Please give me a hint how to add IBOUTLET variables to the ViewControllerSettings class. TableViewController is embedded on Container View.

You need to control drag and drop it inside the ViewControllerSettings class's definition.
class ViewControllerSettings: UITableViewControntroller {
#IBOutlet weak var tableView: UITableView!
//...
}
Note: Make sure the class for the UITableViewController is set to ViewControllerSettings in your storyboard.

#IBOutlet weak var tableView: UITableView!
control Drag the table view to the above #IBOutlet

From your screenshot, it seems that you have not assigned the ViewControllerSettings for UITableViewController inside storyboard.
Inside storyboard, select added UITableViewController, got to Identity Inspector and set Class as ViewControllerSettings.
Also do connect outlets inside your class in .swift file.

Related

attach several UILabels from storyboard to one IBOutlet

I've 15 Labels in my Storyboard they are just texts, also set from storyboard, What I want to do is to style them, but programitically, Therefore I need to create 15 IBOutlets in my ViewController, I wonder if there is any other way of doing that, without 15 IBOutlets,if it's possible to create 1 IBOutlet and attach all of them to that one? because creating 15 of them is kinda stressing...
You can do this with Outlet Collections instead of an IBOutlet for all the labels you want to group together:
One way to do it is to ctrl+drag from your storyboard to your editor and select outlet collection
This will create #IBOutlet weak var labelCollection: UILabel! in your code
This works fine but then you need to add an additional check for the type when looping:
#IBOutlet weak var labelCollection: UILabel!
func setCustomLayout()
{
for label in labelCollection2.subviews
{
if let label = label as? UILabel
{
// do your custom set up here
}
}
}
What I like to do is to create the specific outlet collection in code first if I way to track the same type like so:
#IBOutlet var labelCollection: [UILabel]!
The I drag from the editor to the storyboard
Then I can work with it as follows
#IBOutlet var labelCollection: [UILabel]!
func setCustomLayout()
{
for label in labelCollection
{
// do your customization here
}
}
Then you can loop through the UIViews inside the IBOutletCollection and do the needful

how to create one iboutlet for multiple buttons in swift

So far I have only been able to create one iboutlet per button. What i want to achieve is to create one iboutlet for many buttons thus allowing me to write the code for creating the same border width, colour and radius around these buttons only once. Is this possible?
import UIKit
class GameScreenViewController: UIViewController {
#IBOutlet weak var answerTextLabel: UILabel!
#IBOutlet weak var scoreLabel: UILabel!
#IBOutlet weak var questionLabel: UILabel!
#IBOutlet weak var numberBtn: UIButton! //this iboutlet is what i would like to be able to connect to multiple number buttons in my app.
}
you can use IBoutlet Collection !
You can connect several same types of objects after declaring as IBoutlet Collection.
And if you want to run the same code, you can use the for statement.
for item in myButton {
item.layer.cornerRadius = 3
}
The above solution recommended is indeed correct. There is an option for connecting collection of IBOutlets. But you need to make sure all the IBOutlet it connected to the collection are of the same type. For example collection of UIButtons only.
You connect using New Referencing Outlet Collection instead of New Referencing Outlet

view dependency / reference in storyboard auto layout

Can you reference a view in AutoLayout in Storyboard?
In the "constant" field, I want to enter something like myCustomView.width + 14.
Is there a way to get this dependency?
You need to create an #IBOutlet on your MyCustomView class definition like this
#IBOutlet weak var platzHalterLogoTrailingConstraint: NSLayoutConstraint!
then tie that constraint, using the interface builder.
You will define an initial value on your storyboard, and then programatically change it when you need to.
override func viewDidAppear() {
super.viewDidAppear()
platzHalterLogoTrailingConstraint.constant = calculateTrailingConstant()
}

how to add textfield delegate in swift

I need to add delegate to textfields; my understanding is it can be done two ways:
we go to file and viewController.swift and under class, after UIviewcontroller we add comma and then type UITextFieldDelegate .
and then u under viewDidLoad we add method and function
when on storyboard we click and drag textfield to small icon on top of view that says view controller and pick delegate
Do they both work the same? or there is difference if we do one way or the other?
and what you do if you have more than one textfields, I have 10 textfields and have 7 functions and buttons, I need to do this so I can disable button if my textfield is empty for that button.
There is no difference, as far as I know. Choose what you like more.
In first case you also need to make #IBOutlet for your UITextField(click and drag UITextField from UIStoryboard to your UIViewController code) and after that make UIViewController delegate of UITextField
For example:
import UIKit
class MyViewController: UIViewController, UITextFieldDelegate
{
#IBOutlet weak var myTextField: UITextField!
override func viewDidLoad()
{
super.viewDidLoad()
myTextField.delegate = self
}
}

Unable to connect a Custom NSView to its view controller outlet (KPCTabsControl)

I am trying to assign a KPCTabsControl class to a custom NSView. The window view class is MainViewController.
The MainViewController code looks like this:
class MainViewController : NSViewController {
#IBOutlet var myTabsController: TabsController!
#IBOutlet var myButton: NSButton!
//...
}
The TabsController code looks like this:
class TabsController : NSViewController, KPCTabsControlDataSource, KPCTabsControlDelegate {
#IBOutlet var tabsBar: KPCTabsControl?
var titles: Array<String> = []
func tabsControlNumberOfTabs(tabControl: KPCTabsControl) -> UInt {
return UInt(titles.count)
}
//...
}
The form looks like this:
I tried to connect the "Main View Controller" to the "KPCTabsControl" custom view with control-drag. I get the outlet choices: sourceItemView and view. On the other hand, if I try to connect "Main View Controller" to the "+" button, I get the outlet choices: myButton, sourceItemView and view. I don't understand why the "myTabsController" never appears while the "myButton" outlet is available.
Right now, the custom class of the "KPCTabsControl" view is "KPCTabsControl", I tried to enter "TabsController" but it is not listed in the possible choices. And writing it does not work, the value seems rejected and returns automatically to "KPCTabsControl".
What am I missing?
(I'm using Xcode 7.3.1)