Nil inherited outlet - xcode

I've defined a base class which has an UITableView outlet.
class BaseController: UIViewController, UITableViewDataSource, UITableViewDelegate {
#IBOutlet weak var tableView: UITableView!
...
Then I've inherited the class as follows:
class SubViewController: BaseController {
override func viewDidLoad() {
tableView.rowHeight = screenHeigth / CGFloat(textArray.count)
But the tableView is nil, I've just recently started programming in swift, is it possible to inherit an outlet? If so, how should I do it?
I'm currently using Xcode 6.4

You can inherit an outlet. In your storyboard (assumption), the view controller class name should be set to the name of your subclass. You can then connect up the table view to the outlet in the storyboard.

Related

Access an IBOutlet from another class in Swift

I'm new to Swift and Mac App.
So I'm writing an Mac App today and I still confuse how to access an IBOutlet from another class after searching a lot.
I'm using StoryBoard and there are two NSTextField path mirror in my ViewController.
I have created a custom class Path.swift for the first NSTextField path to know when the text in path has changed.
class Path: NSTextField, NSTextFieldDelegate
{
override func drawRect(dirtyRect: NSRect)
{
super.drawRect(dirtyRect)
// Drawing code here.
self.delegate = self
}
var current = ""
override func controlTextDidChange(obj: NSNotification)
{
current = self.stringValue
println("Current is \(current)")
}
}
And there are two outlets defined in ViewController.swift
class ViewController: NSViewController
{
override func viewDidLoad()
{
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override var representedObject: AnyObject? {
didSet {
// Update the view, if already loaded.
}
}
#IBOutlet weak var path: NSTextField!
#IBOutlet weak var mirror: NSTextField!
}
And when user type something in the first NSTextField path, I want the second NSTextField mirror shows the same string as path.
I tried to use ViewController().mirror.stringValue = current, but I got fatal error: unexpectedly found nil while unwrapping an Optional value
After googling a lot, I knew that I have created a new instance of ViewController instead of accessing the current existing instance of ViewController.
So my question is how I can access the IBOutlet in ViewController.swift from Path.swift class (how to access the current existing instance of ViewController).
Any help will be greatly appreciated.
Thanks in advance.

swift error exc_bad_access loading image using iboutlet

I'm new in iOS-programming. I tried the following. I created a custom class for a button (source founded on internet) like.
class CustomButton : UIButton {
var myAlternateButton:Array<CustomButton>?
var downStateImage:String? = "radiobutton_down"{
didSet{
if downStateImage != nil {
self.setImage(UIImage(named: downStateImage!), forState: UIControlState.Selected)
}
}
}
Now when i try to use the class in my TableViewController I get the error mentioned above
class SettingsTableViewController: UITableViewController {
#IBOutlet var testButton: CustomButton?
#IBOutlet var excelButton: CustomButton?
override func viewDidLoad() {
super.viewDidLoad()
testButton?.downStateImage = "radiobutton_down" -->xc_bad_access
why I can not set the property. Do I have to initialize the class separately or where is my error? Perhaps anyone can help me! Thanks,
arnold
Have you linked your outlet correctly with your UIButton on your interface builder? Be sure to set your custom class name on your custom view too, like so:
Also, if you're setting views using the IBOutlet system I would recommend defining them like this instead if the views are always present:
#IBOutlet weak var testButton: CustomButton!
#IBOutlet weak var excelButton: CustomButton!

How to pass data from NSWindowController to its NSViewController?

I have a IBOutlet of a NSToolBar button in my NSWindowController class, which is my main window class:
class MainWindowController: NSWindowController {
#IBOutlet weak var myButton: NSButton!
// ...
}
I have a class MainViewController that is that content NSViewController of the main window.
How can I access this button in my content NSViewController? Is there a better way to organize the IBOutlets and the controllers to facilitate this access?
To access NSViewController from NSWindowController:
let viewController:MainViewController = self.window!.contentViewController as! MainViewController
To access NSWindowController from NSViewController:
let windowController:MainWindowController = self.view.window?.windowController as! MainWindowController
How about like this using delegate? This example will change your button's title.
#objc protocol SomeDelegate {
func changeTitle(title: String)
}
class ViewController: NSViewController {
weak var delegate: SomeDelegate?
#IBAction func myAction(sender: AnyObject) {
delegate?.changeTitle("NewTitle")
}
}
class MainWindowController: NSWindowController, SomeDelegate {
#IBOutlet weak var myButton: NSButton!
override func windowDidLoad() {
super.windowDidLoad()
// Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
let myVc = window!.contentViewController as! ViewController
myVc.delegate = self
}
func changeTitle(title: String) {
myButton.title = title
}
}

How do you use Interface builder with Swift?

When hooking Swift code up to a Storyboard, how do you add the IBAction and IBOutlet tags?
Add IBAction and IBOutlet attributes to variables and functions so they can be visible in Interface builder.
class ViewController: UIViewController {
#IBOutlet var label: UILabel?
#IBAction func doTap(x:UIButton) {
println("Tapped: \(x)")
}
}
Below code shows IBOutlet and IBAction format in Swift :
class MyViewController: UIViewController {
#IBOutlet weak var btnSomeButton: UIButton?
#IBOutlet weak var lblLabelItem: UILabel?
#IBAction func btnSomeButtonClicked(sender: UIButton) {
...
}
}
You can bind them same way as done in Objective-C.
Just use old ctrl + drag technique which was popular in Xcode5 and everything works fine.
I would agree more with Jayprakash than the upvoted first answer. The only thing I would correct is the marking of the IBOutlets as implicitly unwrapped with the ! The first answer used to be correct, but several changes were made in Swift and how it interacts with IB in the latest release. In Swift, IBOutlets no longer have any implicit behavior or magic--they are simply annotations for IB. As of the date of this response, the following code is correct:
// How to specify an the equivalent of IBOutletCollection in Swift
#IBOutlet var fields: [UITextField]!
// How to specify a standard IBOutlet
#IBOutlet weak var button: UIButton!
// How to specify an IBAction
#IBAction func buttonWasPressed(sender: UIButton) { ... }
While creating a project, you should have selected the storyboard, so that you can add your IBOutlet's directly in the story board.
The Below code gives you a idea of how to add IBOutlet to the UILabel
class ViewController: UIViewController {
#IBOutlet var label : UILabel
}

UIPickerView + TableViewCell + delegates and datasource

I have Table view Controller and separate class which handles for me tableviewcell. Inside the tableview cell I have pickerview.
How to implement delegate and datasource for pickerview which is in tableCell class but my delegate functions in tableview controller?
For Swift:
Create outlet for UIPickerView in custom table view class:
class MyTableViewCell: UITableViewCell, UIPickerViewDelegate, UIPickerViewDataSource {
#IBOutlet var myPickerView: UIPickerView!
}
Add delegate and datasource in "cellForRowAtIndexPath" in ViewController:
class myViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource, UITableViewDelegate, UITableViewDataSource {
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("myCell") as! MyTableViewCell
cell.myPickerView.dataSource = self
cell.myPickerView.delegate = self
return cell
}
}
You could have your tableView controller set a property on the tableview cells as they are created indicating that it is the delegate and datasource.
On the tableviewcell class you created just add a property that is an instance of you tableview controller. Like
#property (nonatomic, retain) MyTableViewController * pickerDelegate;
Then in your cellForRowAtIndexPath you can set that property to self
cell.pickerDelegate = self;
You might need to also set some sort extra property like a tag to distinguish between each cell. I would think another property on the tableviewcell like an NSIndexPath would do.

Resources