swift 3 check the row where the button was pressed - macos

i have a outline view with a custom cell.
all works fine but i need to know, if a button was pressed, in which row number the that button is.
how can i get this row number ?

You could wire up your button's action to an IBAction in your view controller (or whatever knows about the outline view) and call row(for:) on the outline view:
#IBAction func buttonClicked(_ sender: NSButton) {
print("button row:", outlineView.row(for: sender))
}
Straight from the docs:
This method is typically called in the action method for an NSButton
(or NSControl) to find out what row (and column) the action should be
performed on.

Related

macOS NSTableView cell does not do anything

I have this line "func tableView(_ tableView: NSTableView, didClick tableColumn: NSTableColumn) {code here}"
This function does not do anything. I want to click a row in a table view and do some action when clicked it. But it does not do anything. The row is showed correctly, but action is missing. I added also "print("Clicked") in that function, but even that does not show on log information"
How else could I make this working?
From the documentation of tableView(_:didClick:): "Tells the delegate that the mouse button was clicked in the specified table column, but the column was not dragged.". This method isn't called when you click in a row. If you want to catch clicks in a tableview, subclass the table view, row view or cell view and override mouseDown(with:).
Another solution: let the table view handle the click and use the selected row. The delegate has several methods to detect changes in the selection.
Use NSTableView.selectionDidChangeNotification to detect selection
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(ViewController.didSelectRow(_:)), name: NSTableView.selectionDidChangeNotification, object: tableView)
}
#objc
func didSelectRow(_ noti: Notification){
guard let table = noti.object as? NSTableView else {
return
}
let row = table.selectedRow
print(row)
}
deinit {
NotificationCenter.default.removeObserver(self)
}
After test, i found it was triggered by click at column area. As its parameter says, "Did click table column", not table row.

UISplitViewController presenting over current context from detail View

I have a split view controller with master view being something like a menu allowing users to pick the scene for detail view (I have multiple detail views). On one of the detailView scene, I have a button that presents a view controller modally and "Over Current Context" as it has a translucent background and I wanted to create that fog effect. This particular detailView (lets call it TodayViewController) is also the initial detail view controller when the app loads, and only changes when user selects a new view controller from the master view (menu).
This is what I meant in code:
When app just starts:
splitViewController.viewControllers[1] // returns TodayViewController
When user selects from the menu:
splitViewController.viewControllers[1] // returns a different view controller
So the issue I am having is that when the app just starts (bullet 1), when I present a child view controller of TodayViewController modally and "over current context", the child VC presents itself over both the master view (menu) as well as the detail view (TodayViewController), causing the entire screen to have a foggy effect. This is the effect that I want
However when I select another view controller (from the menu) and then select back TodayViewController and try to present the child VC it only presents itself over the detail view now. Meaning that the foggy effect is only present on the detail view and the master view (the menu again) remains clear. How do I fix this?
I hope I'm clear enough with my explanation. Here are some of my code:
My GlobalSplitViewController.swift:
import UIKit
class GlobalSplitViewController: UISplitViewController, UISplitViewControllerDelegate {
func primaryViewControllerForCollapsingSplitViewController(splitViewController: UISplitViewController) -> UIViewController? {
let detailViewController = self.viewControllers[1] as! TodayViewController
return detailViewController
}
func splitViewController(splitViewController: UISplitViewController, collapseSecondaryViewController secondaryViewController: UIViewController, ontoPrimaryViewController primaryViewController: UIViewController) -> Bool {
return true
}
func splitViewController(svc: UISplitViewController, shouldHideViewController vc: UIViewController, inOrientation orientation: UIInterfaceOrientation) -> Bool {
return false
}
}
GlobalSplitViewController is structured so that TodayViewController is presented first on iPhones, but on iPad it shows both master and detail view, uncollapsed.
'Over current context' is supposed to present over only the master or the detail wherever it is called from. I'm not sure why it doesn't work properly at first (but I understand that's what you want) but then works when you select another option. Anyhow, to achieve what you want, stop using 'over current context'. That will present the fog vc over the whole screen.

Why doesn't button state remain?

I have the following code:
#IBAction func mybuttonclick(sender: UIButton) {
if(sender.titleLabel?.text == "Start"){
sender.titleLabel?.text = "Change"
}
else {
sender.titleLabel?.text = "Start"
}
}
When I click the button, I see "Change" flash and then it goes back to "Start". This is a simple new test app. The above is the only code I have in the app. Why does the button text change back to "Start" instead of remaining on "Change"?
Its very hard to answer this question without more information, but I think it's possible you have the action bound twice in Interface Builder. Where the action is being fired twice each time you hit the button. Another possibility is you have the button inside a table or collection view cell where the cell is being reused and replacing your previously edited button.

Swift: How to keep ViewControllers in exact same condition?

I have two view controllers and in ViewController 1 I have a date picker and a text field. I have another ViewController that displays what you have entered in that date picker and text field. I have a ¨change¨ button that returns you to that first view controller and a ¨doen¨button that show view controller 2. As of now the text in the textfield and the date that the user has selected, is gone every time you close the app or click the ¨change button(Go back to View controller 1).
How can i keep the text in the text field, and the selected date in the date picker.
I want the user to be able to change the information that has already been added.
Please add code.
http://imgur.com/PDzJrNJ (Picture of the main storyboard)
The way you are showing the ViewControllers is not optimal. In the best case, when you show something new (ViewController2), then dismiss it, it should lead you back to the previous screen (ViewController1). It should not instantiate a new instance of ViewController1, as the segues in you case are doing.
Please note that depending on how you present the second view the solution would be different.
In case your segue from the first controller to the second is a "Show" segue, you can just add the following to your "Change" button:
#IBAction func changeButtonPressed(sender: AnyObject) {
self.dismissViewControllerAnimated(true, completion: nil)
}
In case you have a navigation controller (I do not see such on the screenshot), then you can use
#IBAction func changeButtonPressed(sender: AnyObject) {
self.navigationController.popViewControllerAnimated(true)
}
What you could try doing is saving the data before exiting the view controller.
In the viewWillDisappear method, you could add
NSUserDefaults.standardUserDefaults().setObject(textField.text, forKey: "userText")
NSUserDefaults.standardUserDefaults().setObject(datePicker.date, forKey: "userDate")
Later, when you reopen the same view controller, in the viewDidLoad method, you could add
if NSUserDefaults.standardUserDefaults().objectForKey("userText") != nil {
textField.text = NSUserDefaults.standardUserDefaults().objectForKey("userText") as! String
}
if NSUserDefaults.standardUserDefaults().objectForKey("userDate") != nil {
datePicker.setDate(NSUserDefaults.standardUserDefaults().objectForKey("userDate") as! NSDate, animated: true)
}
I do not use date pickers very frequently, but I believe this is the correct code. Otherwise, it would be very similar. Hope this helps.

What is the statement to go from one ViewController to another ViewController AFTER I have selected an item from my UIPickerView?

I have an initial view controller. It goes to the next view controller which has the “picker wheel” in it. From that screen, if the user selects one of the 4 items in the picker wheel and stops on it. Then press ONE action button to initiate several C code statements.
I need specific help with coding the statement (s) that cause the transition from one view controller to another view controller.
I’m thinking out loud, it should look something like this (but I really don’t know):
‘If row = 0 then go to View controller B.’
‘If row = 1 then go to View controller C’
etc.
Or use some other ‘VERY SIMPLE’ method for transitioning from a ‘selection from a picker wheel to another view controller’.
Basic documentation.
I have 4 viewcontrollers :
First view controller is named - VCa
Second view controller is named - VC1
Third View controller is named - VCb
Forth view controller is named - Vcc
There is nothing but the default 'Storyboard" coding in this VCa (view controller).
The first view controller only has a label named VCa , so we can see which VC this is.
And it has a button named Button A, so I can do a modal segue to the next view controller named VC1.
The second view controller (VC1) only has some basic things.
The picker wheel named pickerView1. The array named Companies with data.
A button to go back to the first view controller VCa.
The button is named B1Back.
A button to go forward to the next view controller named VCb.
The pupose for this button is only for demo. purposes to transition to VCb.
It has nothing to do with coding a solution to allow the picker wheel selection to . transition somehow to the next view controller.
The button is named B1forwardtob.
The same is true for the 3rd button. It just allows this VC (VCa) to transition to . . another view controller named VCc. The button is named B1forwardtoc.
An ACTION button that does nothing, yet. I’m hoping the transition from view controller to view controller code goes here (after the action button). But again I don’t know what I don’t know.
There is nothing but the default 'Storyboard" coding in this VCb (view controller).
The third viewcontroller (VCb) is very simple too. It is there just to go to and then go back to VC1.
There is nothing but the default 'Storyboard" coding in this VCc (view controller).
Same for the forth viewcontroller (VCc). It is there just to go to and then go backto VC1.
It's sort of hard to tell what you're asking, but are you looking for something like this?
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row
inComponent:(NSInteger)component
{
UIViewController *viewControllerToDisplay;
if (row == 0) {
viewControllerToDisplay = [[ViewControllerB alloc] init];
} else if (row == 1) {
viewControllerToDisplay = [[ViewControllerC alloc] init];
}
if (viewControllerToDisplay) {
[self presentViewController:viewControllerToDisplay animated:YES completion:nil];
}
}

Resources