swift error exc_bad_access loading image using iboutlet - xcode

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!

Related

Nil inherited outlet

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.

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.

Pass continuous colorwell data to second view controller

I have the following situation:
two ViewControllers each containing a box that is to be colored to a color picked from a color well in ViewController
The colorwell is set as continuous in order to see the changes reflected immediately
I am looking for a way to continuously pass the color well value on to the SecondViewController and on to a callback method that will color a box in the SecondViewController.
I found that the prepareForSegue method is commonly used to pass data between view controllers, this however only occurs once during the transition and not continuously.
Can someone point me out in the right direction? Googled for hours but I got really stuck with this.
Thanks.
import Cocoa
class ViewController: NSViewController {
#IBOutlet weak var box: NSBox!
#IBOutlet weak var well: NSColorWell!
#IBAction func well(sender: AnyObject) {
box.fillColor = well.color
}
override func prepareForSegue(segue: NSStoryboardSegue, sender: AnyObject?) {
let second = segue.destinationController as! SecondViewController
second.representedObject = well.color
}
}
import Cocoa
class SecondViewController: NSViewController {
#IBOutlet weak var box: NSBox!
override func viewWillAppear() {
// Note that box.fillColor requires box type to be custom
box.fillColor = self.representedObject as! NSColor
}
}
The prepareForSegue method is a chance to create links between two view controllers. It's pretty common for the source view controller to set itself up as the delegate of the destination view controller. It's also possible for the source view controller to save a reference to the destination view controller for future reference.
If you define a protocol with a method like
func colorValueHasChanged(newColor: NSColor)
Then you can use it in the IBAction for your color well to pass information about changes in the color well from one view controller to the other.

Cannot invoke <method> with and argument list of type '(String)'

Im trying to write an OSX app. A functionality of this app is that it displays the machine IP address.
The address is fetched when the program is opened (AppDelegate.swift):
#NSApplicationMain class AppDelegate: NSObject, NSApplicationDelegate {
var ipadd:String = ""
var path:String = ""
var status:String = ""
func applicationDidFinishLaunching(aNotification: NSNotification) {
ipadd = getIFAddress() //<-- ip stored in here as String
println(ipadd) //successfully prints out the ip
ViewController.setIpDisp(ipadd) //error on this line
}
...
}
And in ViewController.swift:
class ViewController: NSViewController {
#IBOutlet weak var ip: NSTextField!
...
func setIpDisp(ipin: String){
ip.stringValue = ipin
}
To be exact, the error is "Cannot invoke 'setIpDisp' with an argument list of type '(String)'
Thanks
The AppDelegate is trying to call a ViewController method that is updating an #IBOutlet in the view controller's view. It needs a valid ViewController instance to do that.
But this is backwards: The app delegate should not be trying to call view controller methods. The view controller can call methods/properties of the app delegate, but the app delegate really shouldn't be calling view controller methods.
If you need to update the IP number field in the view controller, then the view controller should be initiating this (e.g. in viewDidLoad):
class ViewController: NSViewController {
#IBOutlet weak var ip: NSTextField!
override func viewDidLoad() {
super.viewDidLoad()
updateIpDisp()
}
func updateIpDisp() {
let appDelegate = NSApplication.sharedApplication().delegate as! AppDelegate
ip.stringValue = appDelegate.getIFAddress()
}
}
Or, if you wanted, the AppDelegate set some ipadd string property in its init method (not applicationDidFinishLaunching), and then the updateIpDisp() method could retrieve the property's value from the app delegate, too. (Given that IP numbers are dynamic and can change, that doesn't seem right to me, but do it however you want.) Anyway, that might look like:
class AppDelegate: NSObject, NSApplicationDelegate {
var ipadd: String!
override init() {
super.init()
ipadd = getIFAddress()
}
}
and
class ViewController: NSViewController {
#IBOutlet weak var ip: NSTextField!
override func viewDidLoad() {
super.viewDidLoad()
updateIpDisp()
}
func updateIpDisp() {
let appDelegate = NSApplication.sharedApplication().delegate as! AppDelegate
ip.stringValue = appDelegate.ipadd
}
}
But the view controller should be requesting the IP number from the app delegate and updating its own view. But the app delegate has no business calling methods in the view controller(s).
Your function isn't static, so make sure to initialise an instance of it, like so
#NSApplicationMain class AppDelegate: NSObject, NSApplicationDelegate {
let viewController = ViewController()
var ipadd:String = ""
var path:String = ""
var status:String = ""
func applicationDidFinishLaunching(aNotification: NSNotification) {
ipadd = getIFAddress() //<-- ip stored in here as String
println(ipadd) //successfully prints out the ip
viewController.setIpDisp(ipadd) //error on this line
}
...
}

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
}

Resources