I have the following code written in SWIFT for OS X App, the code is working fine (NSComboBox are select able only, not editable)
I have these two IBOutlet projNewProjType and projNewRouter, when I change the the selection of either of the NSComboBox, I can see the correct selected Index value and String value but how to check that the returned Index value is from projNewProjType NOT projNewRouter in the comboBoxSelectionDidChange()
import Cocoa
class NewProjectSetup: NSViewController, NSComboBoxDelegate {
let comboxProjValue: [String] = [“No”,”Yes”]
let comboxRouterValue: [String] = ["No","Yes"]
#IBOutlet weak var projNewProjType: NSComboBox!
#IBOutlet weak var projNewRouter: NSComboBox!
#IBAction func btnAddNewProject(sender: AnyObject) {
print(“Add New Button Pressed!”)
}
#IBAction func btnCancel(sender: AnyObject) {
self.dismissViewController(self)
}
override func viewDidLoad() {
super.viewDidLoad()
addComboxValue(comboxProjValue,projNewProjType)
addComboxValue(comboxRouterValue,projNewRouter)
self.projNewProjType.selectItemAtIndex(0)
self.projNewRouter.selectItemAtIndex(0)
self.projNewProjType.delegate = self
self.projNewRouter.delegate = self
}
func comboBoxSelectionDidChange(notification: NSNotification) {
let comboBox: NSComboBox = (notification.object as? NSComboBox)!
print("comboBox comboBox: \(comboBox)")
/* This printed “<NSComboBox: 0x6000001e1a00>”*/
print("comboBox objectValueOfSelectedItem: \(comboBox.objectValueOfSelectedItem)")
/* This printed the correct selected String value */
print("comboBox indexOfSelectedItem: \(comboBox.indexOfSelectedItem)")
/* This printed the correct selected Index value */
}
/* Add value to Combo box */
func addComboxValue(myVal:[String],myObj:AnyObject){
let myValno: Int = myVal.count
for i in 0..<myValno{
myObj.addItemWithObjectValue(myVal[i])
}
}
}
You already know the addresses of your two NSComboBox outlets and you know the address of which NSComboBox caused that notification to trigger, so why not do something like:
func comboBoxSelectionDidChange(notification: NSNotification) {
let comboBox: NSComboBox = (notification.object as? NSComboBox)!
if comboBox == self.projNewProjType
{
print("selection changed via self.projNewProjType")
}
if comboBox == self.projNewRouter
{
print("selection changed via self.projNewRouter")
}
You can set identifiers to your NSComboBoxes in IB. Select your combo box and choose identity inspector and name identifier. Then you are able to do like this:
if comboBox.identifier == "someIdentifier" {
// Do something
}
Related
I keep getting this error and don't know how to solve it. Can anyone help me out? The thing is I want it to update the title accordingly.
Here it gives me an error at countElement, where it gives me an error
Use of unresolved identifier
for item in components {
if countElement(item.stringByTrimmingCharactersInSet(NSCharacterSet.whitespacesAndNewlines())) > 0
{
self.navigationItem.title = item
break
}
import UIKit
//the protocol (or delegate) pattern, so we can update the table view's selected item
protocol NoteViewDelegate {
//the name of the function that will be implemented
func didUpdateNoteWithTitle(newTitle : String, andBody newBody :
String)
}
class NotesViewController: UIViewController , UITextViewDelegate {
//a variable to hold the delegate (so we can update the table view)
var delegate : NoteViewDelegate?
//a variable to link the Done button
#IBOutlet weak var btnDoneEditing: UIBarButtonItem!
#IBOutlet weak var txtBody : UITextView!
//a string variable to hold the body text
var strBodyText : String!
override func viewDidLoad() {
super.viewDidLoad()
self.txtBody.becomeFirstResponder()
//allows UITextView methods to be called (so we know when they begin editing again)
self.txtBody.delegate = self
//set the body's text to the intermitent string
self.txtBody.text = self.strBodyText
//makes the keyboard appear immediately
self.txtBody.becomeFirstResponder()
}
#IBAction func doneEditingBody() {
//tell the main view controller that we're going to update the selected item
//but only if the delegate is NOT nil
if self.delegate != nil {
self.delegate!.didUpdateNoteWithTitle( newTitle: self.navigationItem.title!, andBody: self.txtBody.text)
}
//hides the keyboard
self.txtBody.resignFirstResponder()
//makes the button invisible (still allowed to be pressed, but that's okay for this app)
self.btnDoneEditing.tintColor = UIColor.clear
}
func textViewDidBeginEditing(_ textView: UITextView) {
//sets the color of the Done button to the default blue
//it's not a pre-defined value like clearColor, so we give it the exact RGB values
self.btnDoneEditing.tintColor = UIColor(red: 0, green:
122.0/255.0, blue: 1, alpha: 1)
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
//tell the main view controller that we're going to update the selected item
//but only if the delegate is NOT nil
if self.delegate != nil {
self.delegate!.didUpdateNoteWithTitle(newTitle: self.navigationItem.title!, andBody: self.txtBody.text)
}
}
func textViewDidChange(_ textView: UITextView) {
let components = self.txtBody.text.components(separatedBy: "\n")
self.navigationItem.title = ""
for item in components {
if countElement(item.stringByTrimmingCharactersInSet(NSCharacterSet.whitespacesAndNewlines())) > 0 {
self.navigationItem.title = item
break
}
}
}
}
countElement is a very old syntax and has been replaced with count for a long time.
The current (optimized) Swift 4 syntax is
func textViewDidChange(_ textView: UITextView) {
let components = self.txtBody.text.components(separatedBy: "\n")
self.navigationItem.title = components.first{ !$0.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty } ?? ""
}
Never check for an empty string or an empty collection type with .count == 0. There is isEmpty.
And never use a horrible syntax like
if self.delegate != nil {
self.delegate!.didUpdateNoteWithTitle...
This is Swift, there is Optional Chaining
self.delegate?.didUpdateNoteWithTitle...
I'm working on an OSX app with Swift which makes use of an NSSplitView which holds two view controllers: "TableViewController" and "EntryViewController". I'm using delegates in order to transmit a custom NSObject ("Entry") on click from TableViewController up to the SplitViewController, then back down to the EntryViewController.
My problem is this: When the Entry object is received in the EntryViewController, any attempt to assign its properties to a text field value result in an unexpectedly found nil type error, never mind that the IBOutlets are properly linked, and that it can both print the Entry.property and the textfield string value (provided it is in a different, unrelated function).
I have tried many arrangements to solve this problem, which is why the current configuration might be a bit over-complicated. A delegate relation straight from Table VC to Entry VC caused the same issues.
Is there some way that the IBOutlets are not connecting, even though the view has loaded before the delegate is called? I've read many many articles on delegation—mostly for iOS—and yet can't seem to find the root of my problems. I'll be the first to admit that my grasp of Swift is a little bit piecemeal, so I am open to the possibility that what I am trying to do is simply bad/hacky coding and that I should try something completely different.
Thanks for your help!
TableViewController:
protocol SplitViewSelectionDelegate: class {
func sendSelection(_ entrySelection: NSObject)
}
class TableViewController: NSViewController {
#IBOutlet weak var searchField: NSSearchField!
#IBOutlet var tableArrayController: NSArrayController!
#IBOutlet weak var tableView: NSTableView!
var sendDelegate: SplitViewSelectionDelegate?
dynamic var dataArray = [Entry]()
// load array from .plist array of dictionaries
func getItems(){
let home = FileManager.default.homeDirectoryForCurrentUser
let path = "Documents/resources.plist"
let urlUse = home.appendingPathComponent(path)
let referenceArray = NSArray(contentsOf: urlUse)
dataArray = [Entry]()
for item in referenceArray! {
let headwordValue = (item as AnyObject).value(forKey: "headword") as! String
let defValue = (item as AnyObject).value(forKey: "definition") as! String
let notesValue = (item as AnyObject).value(forKey: "notes") as! String
dataArray.append(Entry(headword: headwordValue, definition: defValue, notes: notesValue))
}
}
override func viewDidLoad() {
super.viewDidLoad()
self.sendDelegate = SplitViewController()
getItems()
print("TVC loaded")
// Do any additional setup after loading the view.
}
// send selection forward to entryviewcontroller
#IBAction func tableViewSelection(_ sender: Any) {
let index = tableArrayController.selectionIndex
let array = tableArrayController.arrangedObjects as! Array<Any>
let obj: Entry
let arraySize = array.count
if index <= arraySize {
obj = array[index] as! Entry
print(index)
print(obj)
sendDelegate?.sendSelection(obj)
}
else {
print("index unassigned")
}
}
}
SplitViewController:
protocol EntryViewSelectionDelegate: class {
func sendSecondSelection(_ entrySelection: NSObject)
}
class SplitViewController: NSSplitViewController, SplitViewSelectionDelegate {
var delegate: EntryViewSelectionDelegate?
#IBOutlet weak var mySplitView: NSSplitView!
var leftPane: NSViewController?
var contentView: NSViewController?
var entrySelectionObject: NSObject!
override func viewDidLoad() {
super.viewDidLoad()
// assign tableview and entryview as child view controllers
let story = self.storyboard
leftPane = story?.instantiateController(withIdentifier: "TableViewController") as! TableViewController?
contentView = story?.instantiateController(withIdentifier: "EntryViewController") as! EntryViewController?
self.addChildViewController(leftPane!)
self.addChildViewController(contentView!)
print("SVC loaded")
}
func sendSelection(_ entrySelection: NSObject) {
self.delegate = EntryViewController() //if this goes in viewDidLoad, then delegate is never called/assigned
entrySelectionObject = entrySelection
print("SVC:", entrySelectionObject!)
let obj = entrySelectionObject!
delegate?.sendSecondSelection(obj)
}
}
And Finally, EntryViewController:
class EntryViewController: NSViewController, EntryViewSelectionDelegate {
#IBOutlet weak var definitionField: NSTextField!
#IBOutlet weak var notesField: NSTextField!
#IBOutlet weak var entryField: NSTextField!
var entryObject: Entry!
override func viewDidLoad() {
super.viewDidLoad()
print("EVC loaded")
}
func sendSecondSelection(_ entrySelection: NSObject) {
self.entryObject = entrySelection as! Entry
print("EVC:", entryObject)
print(entryObject.headword)
// The Error gets thrown here:
entryField.stringValue = entryObject.headword
}
}
You don't need a delegate / protocol since there is a reference to EntryViewController (contentView) – by the way the instance created with EntryViewController() is not the instantiated instance in viewDidLoad.
Just use the contentView reference:
func sendSelection(_ entrySelection: NSObject) {
contentView?.sendSecondSelection(entrySelection)
}
I have 2 combo boxes that use the same dictionary as the data source. in the first combo box the keys will be displayed and in the second one the values will be displayed.
First I need a bit of help with implementing the data source delegate methods; please see code listing below. the relevant boxes are floorsBox and roomsBox. Ignore the other 2 as they will be populated from a local variable or from the interface.
Also, lets say that for the first floor [key = 0] i have 3 rooms [value = 3], how do I take the value of 3 and make it into 3 items inside the roomsBox; So that every time the floor changes the box for the rooms has the corresponding number of items (if that makes sense). I really do appreciate your help and I thank you.
class RoomAndAlarmTypesVC: NSViewController, NSComboBoxDelegate, NSComboBoxDataSource {
//MARK: - Properties
private var floorsAndRooms = [String: String]()
#IBOutlet private weak var floorsBox: NSComboBox!
#IBOutlet private weak var roomsBox: NSComboBox!
#IBOutlet private weak var roomType: NSComboBox!
#IBOutlet private weak var alarmType: NSComboBox!
//MARK: - Actions
override func viewDidLoad() {
super.viewDidLoad()
floorsAndRooms = representedObject as! [String : String]
floorsBox.usesDataSource = true
roomsBox.usesDataSource = true
roomType.usesDataSource = true
alarmType.usesDataSource = true
floorsBox.delegate = self
roomsBox.delegate = self
roomType.delegate = self
alarmType.delegate = self
floorsBox.dataSource = self
roomsBox.dataSource = self
roomType.dataSource = self
alarmType.dataSource = self
}
#IBAction private func setTheSystem(_ sender: NSButton) {
}
func numberOfItems(in comboBox: NSComboBox) -> Int {
what do i do here?
}
func comboBox(_ comboBox: NSComboBox, objectValueForItemAt index: Int) -> Any? {
same here?
}
}
Use a switch statement based on the sender (your comboBox parameter)
for example:
switch comboBox
{
case floorsBox : return // the data for floors
case roomsBox : return // the data for rooms
...
}
I am really struggling to pass the contents of one array from a view controller to another to set up the contents of a nscombobox. I have tried everything I can think of, prepare for segue, init; but nothing seems to work.
the program flow is as follows: the user enter a number into a text field and based on it an array with the size of the number is created. Once the user presses a button the next VC appears that has a combo box and inside that combo box those numbers need to appear. All my attempts result in an empty array being passed. Could someone please take a bit of time and help me out. Im sure I'm doing a silly mistake but cannot figure out what.
Code listing below:
Class that take the user input. At this stage I'm trying to pass the contents of the array in the next class as I gave up on prepare for segue because that one crashes because of nil error. Please note that prepare for segue is uncommented in the code listing just for formatting purposes here. Im my program it is commented out as I am using perform segue at the moment.
Any solution would be nice please. Thank you.
import Cocoa
class SetNumberOfFloorsVC: NSViewController {
//MARK: - Properties
#IBOutlet internal weak var declaredNumber: NSTextField!
internal var declaredFloorsArray = [String]()
private var floorValue: Int {
get {
return Int(declaredNumber.stringValue)!
}
}
//MARK: - Actions
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction private func setNumberOfFloors(_ sender: NSButton) {
if declaredNumber.stringValue.isEmpty {
let screenAlert = NSAlert.init()
screenAlert.messageText = "Please specify the number of floors!"
screenAlert.addButton(withTitle: "Got it!")
screenAlert.runModal()
} else if floorValue == 0 || floorValue < 0 {
let screenAlert = NSAlert.init()
screenAlert.messageText = "Please input a correct number of floors!"
screenAlert.addButton(withTitle: "Got it!")
screenAlert.runModal()
} else {
for i in 0...floorValue - 1 {
declaredFloorsArray.append(String(i))
}
print("\(declaredFloorsArray)")
let declareNumberOfRoomsVC = SetNumberOfRoomsForFloorVC(boxData: declaredFloorsArray)
declareNumberOfRoomsVC.boxData = declaredFloorsArray
performSegue(withIdentifier: "set number of rooms", sender: self)
}
}
override func prepare(for segue: NSStoryboardSegue, sender: Any?) {
if segue.identifier == "set number of rooms" {
if let addRoomsVC = segue.destinationController as? SetNumberOfRoomsForFloorVC {
addRoomsVC.floorBox.addItems(withObjectValues: declaredFloorsArray)
}
}
}
}
this is the class for the next VC with the combo box:
import Cocoa
class SetNumberOfRoomsForFloorVC: NSViewController, NSComboBoxDelegate, NSComboBoxDataSource {
//MARK: - Properties
#IBOutlet internal weak var floorBox: NSComboBox!
#IBOutlet private weak var numberOfRoomsTxtField: NSTextField!
internal var boxData = [String]()
//MARK: - Init
convenience init(boxData: [String]) {
self.init()
self.boxData = boxData
}
//MARK: - Actions
override func viewDidLoad() {
super.viewDidLoad()
floorBox.usesDataSource = true
floorBox.dataSource = self
floorBox.delegate = self
print("\(boxData)")
}
#IBAction private func setRoomsForFloor(_ sender: NSButton) {
}
//MARK: - Delegates
func numberOfItems(in comboBox: NSComboBox) -> Int {
return boxData.count
}
func comboBox(_ comboBox: NSComboBox, objectValueForItemAt index: Int) -> Any? {
return boxData[index]
}
}
First you should remove the following code.
let declareNumberOfRoomsVC = SetNumberOfRoomsForFloorVC(boxData: declaredFloorsArray)
declareNumberOfRoomsVC.boxData = declaredFloorsArray
I assume you think that the viewController you created here is passed to prepareForSegue. However the storyboard instantiates a new viewController for you.
After that you need to set your declaredFloorsArray as the the boxData of the new viewController in prepareForSegue and you should be good to go.
I have a swift class that reads lines from a text document and prints out the first line. After, every time a button is clicked a new line is read out.
What I want is to have a random line printed out the first time, and then a random line printed out after every button click.
Here's what I have so far:
import Foundation
import UIKit
class InfoController: UIViewController {
// MARK: Properties
#IBOutlet weak var difficultylevel: UILabel!
var i:Int = 0
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func readFile(){
if let path = NSBundle.mainBundle().pathForResource("easymath", ofType: "txt"){
var data = String(contentsOfFile:path, encoding: NSUTF8StringEncoding, error: nil)
if let content = data {
let myStrings = content.componentsSeparatedByCharactersInSet(NSCharacterSet.newlineCharacterSet())
let randomIndex = Int(arc4random_uniform(UInt32(myStrings.count)))
difficultylevel.text = myStrings[randomIndex]
}
}
}
#IBAction func difficultybutton(sender: UIButton) {
difficultylevel.text = // TODO insert random index of "myStrings" array here
}
}
However, I cannot access the myStrings array at the TODO portion inside the button click. Any help on how to set this up?
Variable scope in Swift is limited to the brackets of the function. So to make myStrings available outside of your readFile() function, you need to declare it as a property for the class:
#IBOutlet var difficultyLevel: UILabel? // BTW your IBOutlet should not be weak
var i: Int = 0
var myStrings: [String]?
Since you are going to use the random functionality over and over, we can abstract the function like this
func randomString() -> String? {
if let strings = myStrings {
let randomIndex = Int(arc4random_uniform(UInt32(myStrings.count)))
return strings[randomIndex]
}
return nil
}
then your instantiation will be like this
if let content = data {
myStrings = content.componentsSeparatedByCharactersInSet(NSCharacterSet.newlineCharacterSet())
difficultyLevel.text = randomString()
}
Then, your difficultybutton function will be (with an abstracted random string function)
// Changed the name for better readibility
#IBAction func difficultyButtonTapped(sender: UIButton) {
difficultyLevel.text = randomString()
}
Finally, there isn't any code that calls the readFile function, so you should add it to probably the viewDidLoad function as #CharlesCaldwell points out
override func viewDidLoad() {
super.viewDidLoad()
readFile()
}