NSBrowserDelegate not asking for preview view controller - appkit

I'm implementing an NSBrowser in Swift and want to display a preview view controller when certain leaf items are selected in the browser.
I've implemented the func browser(browser: NSBrowser, previewViewControllerForLeafItem item: AnyObject) method, but it never gets called.
I'm also implementing the following delegate methods:
func browser(sender: NSBrowser, numberOfRowsInColumn column: Int)
func browser(sender: NSBrowser, willDisplayCell cell: AnyObject, atRow row: Int, column: Int)

previewViewControllerForLeafItem needs an item. The documentation of NSBrowser could be better but it says about previewViewControllerForLeafItem:
This method is called only if the delegate implements the item data source methods.
The item data source methods are rootItemForBrowser, numberOfChildrenOfItem, child ofItem, isLeafItem and objectValueForItem.

Related

Global variable and optional binding in Swift

I have some quite simple doubt regarding optional binding,global variable & wrapping and unwrapping . Since I am new to SWIFT, its very important to understand the tits & bits of their concepts.
1) In Swift if I declare a global variable, I have 2 options either to make it optional or non optional, so let I am having 2-4 or more optional variables . So is it advisable to optional bind all those variables in
viewDidLoad() method// so that I could use them without any problem of unwrapping and fatal error in my program.
2) Let me make myself more clear by the following example- I have 2 VC in my project VC1 & VC2 . VC2 has a text field in which user enters some value and displays it in a tabelview in VC1.
In Vc1
var namevc1 = NSMutableArray?//holds the input of textfield to be passed from VC2.
As you can see, my VC1 is the first view controller that loads when my project runs and I am using an optional variable to populate my tabke vuew that is
'arr'
So when the app runs for the first time its empty . So it might cause a fatal error while using its value in the code. So what is its solution whether to unbind it in the
viewDidLoad()
method or in all total declare an empty NSMutable array type in place of optional type .
Thanks in advance.
I'll start by repeating the my comment from above.
Possibly you've misunderstanding the concept of global variables in Swift.
If you have a global variable, you won't have to "pass" it between any views/methods/classes etc, because the variable is defined at global scope (accessible everywhere).
Generally global variables is not a good idea, and something that you want to avoid.
Regarding the matter of global variables and swift, you really should include singletons into the discussion. See e.g. the following existing SO thread(s):
Any reason not use use a singleton "variable" in Swift?
(How to create a global variable?)
(Declaring Global Variables in Swift)
Communication between TableViewController and ViewController by means of segues (prepare for & unwind segues)
(This answer ended up being very and probably a bit too thorough, as I didn't know in detail what your current tableview/viewcontroller program state looks like. Sorry for the lengthy answer and any inconvenience it might bring to readers of it).
Now, lets leave global variables and discuss one (among other) viable options for the communication between the two controllers in your example. From your question, I'll summarize your example as follows
VC1: storyboard entry point, a UITableViewController consisting of UITableViewCells, where, in these cells, you display some text, say, via instances of UILabel.
VC2: a UIViewController, accessible from the cells of VC1, containing an UITextField instance. When user enters text into this text field, your want the text to be displayed in the associated cell in VC2 (associated in the sense that it was the cell in VC1 that was used to access VC2).
We'll associate VC1 and VC2 with (cocoa touch) classes TableViewController (TableViewController.swift) and ViewController (ViewController.swift), respectively. The cells in the table view controller will be associated with (cocoa touch) class TableViewCell (TableViewCell.swift). Details for these classes follow below.
For this simple example, note that we will not embed VC1 into a navigation controller (which is otherwise appropriate for table view -> view navigation).
We'll start in the storyboard, adding objects (drag-and-drop from object library) for a Table View Controller and a View Controller. The table view container will also, automatically, contain, in its Table View, a TableViewCell. Continuing in the storyboard:
Add a UILabel object to the TableViewCell container in the Table View Controller (align it as you wish)
In the View Controller, add a Text Field object and a Button object (align them as you wish).
Set the entry point to the Table View Controller.
Thereafter Ctrl-drag a 'Show' segue from the TableViewCell to the View Controller.
Select the Show segue and, from the Attributes inspector, enter an identifier for it, say, ShowDetail.
Finally, with the TableViewCell selected, (as above; from the attribute inspector), enter an identifier for the cell. Here, we'll use simply use identifier TableViewCell.
We now leave the storyboard for now and implement three classes, associated with the Table View Controller, the View Controller and the formers' TableViewCell.
We start with the Table View Controller, and implement our UITableViewController sub-class. Note that here, instead of using an NSMutableArray to hold the texts of the UITextLabel in each cell, we'll simply use a String array.
// TableViewController.swift
Import UIKit
class TableViewController: UITableViewController {
// Properties
var userTextLabels = [String]()
var numberOfCells: Int?
override func viewDidLoad() {
super.viewDidLoad()
numberOfCells = loadSampleTextLabels() // Load sample labels.
}
func loadSampleTextLabels() -> Int {
userTextLabels += ["Label #1", "Label #2", "Label #3"]
return userTextLabels.count
}
// func numberOfSectionsInTableView(tableView: UITableView) ...
// func tableView(tableView: UITableView, numberOfRowsInSection section: Int) ...
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cellIdentifier = ("TableViewCell")
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! TableViewCell
// Text for current cell
let cellText = userTextLabels[indexPath.row]
cell.userSuppliedTextLabel.text = cellText
return cell
}
// ... communication?
}
Where the two commented out methods are standard methods used in any UITableViewController, for number of sections (e.g. return 1) and cells (e.g. return (numberOfCells ?? 0)) in the table, respectively. I'll leave fixing these to you.
Now, we associate the TableViewCell object(s) in the table view with instances of a subclass to UITableViewCell. Here, we'll use a very simple class for our cells; each cell just containing a single UILabel instance (created via storyboard Ctrl-drag as an #IBOutlet from the UILabel in the table view cells).
// TableViewCell.swift
import UIKit
class TableViewCell: UITableViewCell {
// Properties
#IBOutlet weak var userSuppliedTextLabel: UILabel!
// Ctrl-drag from UILabel (in TableViewCell) in storyboard
override func awakeFromNib() {
super.awakeFromNib()
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
}
Finally, for the view controller that is accessed from the table view cells: use a single #IBOutlet to the UITextField used for user text input, and handle events in this text field using the pre-existing UITextFieldDelegate. E.g.:
// ViewController.swift
import UIKit
class ViewController: UIViewController, UITextFieldDelegate {
// Properties
#IBOutlet weak var userSuppliedText: UITextField!
// Ctrl-drag from storyboard...
var cellText: String?
override func viewDidLoad() {
super.viewDidLoad()
userSuppliedText.text = cellText ?? "..."
// Handle the user input in the text field through delegate callbacks
userSuppliedText.delegate = self
}
// UITextFieldDelegate
func textFieldShouldReturn(textField: UITextField) -> Bool {
// User finished typing (hit return): hide the keyboard.
textField.resignFirstResponder()
return true
}
func textFieldDidEndEditing(textField: UITextField) {
cellText = textField.text
}
}
We've also declared a string property (cellText) here, that will as act as container for communication between VC1 and VC2.
We return to the storyboard and---from the Identity inspector---associate the three storyboard objects (Table View Controller, View Controller, TableViewCell) with their associated classes that we've just written above.
We're now almost at our goal; it only remains to specify how to communicate between the two controllers.
We'll begin with communication from VC1 to VC2. In your comment above, you were on the right track (for this specific solution, anyway) by looking at the prepareForSegue(...) method. In the class for the Table View Controller, we add the following method:
// ... add to TableViewController.swift
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
if segue.identifier == "ShowDetail" {
let viewController = segue.destinationViewController as! ViewController
if let selectedCell = sender as? TableViewCell {
let indexPath = tableView.indexPathForCell(selectedCell)!
let currentTextInCell = userTextLabels[indexPath.row]
viewController.cellText = currentTextInCell // <-- note this
}
}
}
Hence, for VC1->VC2 communication, we can (in this example) bring whatever existing text that is currently occupying the UILabel in the sender cell (as is specified by the String array userTextLabels). Look at the viewDidLoad(...) method in the ViewController.swift to see how this value is passed from VC1 and set as default text in the UITextField in VC2.
Now, for communication VC2->VC1, which was the specific communication direction you were asking about, add another method (programmatically), again to TableViewController.swift:
// ... add to TableViewController.swift
#IBAction func unwindToTableView(sender: UIStoryboardSegue) {
if let sourceViewController = sender.sourceViewController as? ViewController,
text = sourceViewController.cellText {
// ^ note 2nd clause of if let statement above
if let selectedIndexPath = tableView.indexPathForSelectedRow {
// Update cell text
userTextLabels[selectedIndexPath.row] = text
tableView.reloadRowsAtIndexPaths([selectedIndexPath], withRowAnimation: .None)
}
}
}
Here, we define an unwind action that, when triggered, retrieves the cellText property of the view controller that was the source of the segue, i.e., in our case, the instance of ViewController. But how do we trigger this action?
Return to the storyboard and the View Controller. Note the three little icons in the top of the View Controller object, more specifically, the right-most of these, named Exit. Ctrl-drag an action from your Button to the Exit icon, and select the unwindToTableView Action Segue. When you click your button the view controller, the view unwind (exit) and land at the unwindToTableView method in the TableViewController.
The resulting app should look something like this:
This was way longer than I had expected, but once you get started writing... Anyway, the method above uses, naturally, no global variables, but make use of references to future (prepareForSegue) or historic (unwindToTableView) views to get (generally from current or historic view) or set (generally in current of future view) values by using these references (to future/historic view).
Apple has their own very thorough tutorial on an example app in the tableviewcontroller/viewcontroller context that I would recommend going over. I found it very valuable myself when I started coding Swift.
Start Developing iOS Apps (Swift)

performSegueWithIdentifier causes fatal error: unexpectedly found nil while unwrapping an Optional value

What I need was to detect which cell was selected on my tableView, and using the indexPath.row to get the index of my object array which is personList, and pass that data to another View Controller and print on the label.
However, I received an error fatal error: unexpectedly found nil while unwrapping an Optional value
Below are my codes.
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath){
selectedCell = indexPath.row
performSegueWithIdentifier("DetailScreen", sender: nil)
}
override func performSegueWithIdentifier(identifier: String, sender: AnyObject?) {
//detailedView.personName = personList[selectedCell].GetPersonName()
detailedView.LastNameLabel.text = personList[selectedCell].GetLastName()
}
The problem is that you initialize the detailedView in the wrong way. Do it like this:
// IMPORTANT: Override prepareForSegue for your purpose, not performSegueWithIdentifier
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "DetailScreen" {
// Do the initialization here. You don't need a global variable.
if let detailedView = segue.destinationViewController as? PersonDetailScreen {
//detailedView.personName = personList[selectedCell].GetPersonName()
detailedView.LastNameLabel.text = personList[selectedCell].GetLastName()
}
}
}
PS: You are overriding the wrong function.
EDIT
As mentioned in your comment didSelectRowAtIndexPath and prepareForSegue doesn't coexist because the method you used for creating the segue is wrong. And with that, your code is doing more wrong than doing the right thing. Right now you might have created the segue by control dragging starting from the UITableView prototype cell to the destination view controller. Now when you do this what happens is, you are telling that the segue must be performed on the clicking on that particular cell itself, which means you are setting its action right in the storyboard. So even if you don't implement didSelectRowAtIndexPath, your code will still navigate to the second ViewController when the cell is clicked. But your requirement is that you must do some custom operations when the cell is clicked and then navigate to the second viewcontroller. For doing that, delete the current segue and then create a new segue with the same identifier like the way you create a normal segue - by control dragging from the source ViewContoller(not the tableview cell prototype) to the destination view controller. Then replace the override func performSegueWithIdentifier(identifier: String, sender: AnyObject?) function with the function I have provided in my original answer and then your code will work smooth.
PS: Make sure your Identifier is named correctly.
'NSInvalidArgumentException', reason: 'Receiver (<PersonDisplay.FirstViewController: 0x7feabaec2870>) has no segue with identifier 'DetailScreen''
This happens since you haven't set the name for the segue correctly in the storyboard. So rectify that as well. Specify the identifier for the segue in the storyboard.
You should pass valuable to detailedView and assign to label in viewDidLoad:
detailedView.lastName = personList[selectedCell].GetLastName()
Go viewDidload of detailedView:
detailedView.LastNameLabel.text = detailedView.lastName

Not all optional NSTextViewDelegate methods being fired (SWIFT)

This is an OSX SWIFT project
The NSTextViewDelegate protocol defines a bunch of optional methods. I know I've correctly wired-up the delegate because I receive this notification.
func textViewDidChangeSelection(notification: NSNotification)
However, other methods I need are never being fired. Specifically, I'm trying to implement custom tooltip behaviour in a textview, but the following are never fired, even though a tooltip has been set and is being displayed.
func textView(textView: NSTextView, willDisplayToolTip tooltip: String, forCharacterAtIndex characterIndex: Int) -> String?
func textView(textView: NSTextView, doCommandBySelector commandSelector: Selector) -> Bool
There are other optional methods of interest as well, so the list above is just illustrative.
Any help would be appreciated.

How to add tableview datasource and delegate to a separate class/object?

I'm trying to learn how to make a native MacOS X app with SWIFT. I've no background in using Objective-C and even Xcode for the moment.
I achieved to do a simple app which answer to some button clicks and modify some labels. Now, I want to put some stuff in a table. I've put a NSTableView on my UI and figured out that I will have to implement a class which offer implementation for the NSTableViewDelegate and NSTableViewDataSource protocols.
I think that it would be cleaner, for code organization and readability, to put all the stuff to control my TableView in a separate module of the main AppDelegate. Anyway, any tutorial I've seen so far is just focused on how to implement the protocols and put them all in the AppDelegate class.
I've created a new SWIFT file and put the temporary following code inside it:
import Foundation
import Cocoa
class LogTableController: NSObject, NSTableViewDelegate, NSTableViewDataSource {
#IBOutlet var tableView: NSTableView!
var logTableData:[NSDictionary] = []
func numberOfRowsInTableView (tableView: NSTableView!) -> Int {
return 0
}
func tableView(tableView: NSTableView, objectValueForTableColumn tableColumn: NSTableColumn!, row rowIndex: Int) -> AnyObject! {
return "Hello"
}
}
But I'm unable to find how to proceed to make outlet beetween my TableView and my custom class or an instance of that class. Nothing allow me to do that in the InterfaceBuilder.
I think I'm missing something, any answer on how to organize my code to not put all the stuff in the AppDelegate class would be appreciated.
Thanks !
In Interface Builder you would need to add an object from the object library to the document outline and then set its class to your custom class. Then you can connect the delegate and datasource to that object.

NSTableView & NSNotificationCenter strange behavior

I got very strange behavior when reloading NSTableView data inside notification observer.
class MainWindowController: NSWindowController, NSTableViewDataSource, NSTableViewDelegate
{
var data: String[] = []
#IBOutlet var filesTableView: NSTableView!
override func awakeFromNib()
{
super.awakeFromNib()
NSNotificationCenter.defaultCenter().addObserver(self, selector: "droppedFiles:", name: DroppedFilesNotification.notificationName, object: nil)
}
func droppedFiles(notification: NSNotification!)
{
data += ["123"]
println(data.count)
filesTableView.reloadData()
}
func numberOfRowsInTableView(tableView: NSTableView!) -> Int
{
return data.count
}
#IBAction func crazyTest(AnyObject)
{
NSNotificationCenter.defaultCenter().postNotificationName(DroppedFilesNotification.notificationName, object: self, userInfo: [DroppedFilesNotification.fileNamesParameterName: ["123"]])
}
}
First call of crazyTest function displays:
1
Seconds call of crazyTest function displays:
2
3
4
Third call of crazyTest function displays numbers 5-13.
If we would remove filesTableView.reloadData() from droppedFiles function then all works fine except table view isn't updated. Any idea why this happens and how to reload table view there?
EDIT:
Also, there is no issue in case calling droppedFiles function directly instead of using NSNotificationCenter. But I'd prefer to use notification center in my application.
Thanks ahead.
If this is a view-based table view which (perhaps implicitly) loads its views from NIBs, then its awakeFromNib method will get called each time the NIB is loaded. From here:
Note: Calling makeViewWithIdentifier:owner: causes awakeFromNib to be
called multiple times in your app. This is because
makeViewWithIdentifier:owner: loads a NIB with the passed-in owner,
and the owner also receives an awakeFromNib call, even though it’s
already awake.
In your case, you're registering for the notification each time. So, you're registering for it many times over and you receive the notification once for each time you registered.
You are not removing the observer. My guess is that you are going into that view controller multiple times and therefore it is registered for the same notification multiple times. Therefore, when the notifications is triggered, the callback gets called multiple times.

Resources