how to set action for a button inside the NSCollectionViewItem - objective-c

I was using an NSCollectionView to show a list of pictures in the binded array controller, also, I want to add a delete button on the left-top corner of each picture so that when someone press the button, it will remove the corresponding object in the array controller.
However, when I tried to make the NSCollectionViewItem a subclass of my custom class, and set action of the delete button to a method written in my custom class, It reports an error like
'Could not connect the action XXX: to target of class NSCollectionViewItem'
Also, I failed when trying to add an array controller object in the NIB file.
What should I do to make everything OK?

It seems we can add a button in the default Protype of the CollectionView, and link it's target to the controller.

Related

Connection for label/image view outlet for collectionViewCell not populating. ( no errors)

I am trying to connect a label and image view outlet for a collection view cell with control drag, since each photo and label will be different. I have already done this before with a previous view controller. However this time when I try to get the outlets to connect to the cell class. It is not populating. Is there any reason why this is happening?
I also want to note that this view controller will be connected to another view controller I previously made that also uses a collection view. Could this be the reason why I am having this problem? I already created another view controller class and connected the collection view datasource and delegates. I also tried connecting the label and image view to my other collection view cell class that worked for the other collection view cell and it won't even populate there.
Worked fine with my first collection view
When I drag it doesn't populate outlet connection
Ahh, so I forgot to set the actual cells custom class property.
That fixed it.

View Controllers, Popup buttons, AppDelegates, Status Menus, and headaches

This is kinda long but I want to give as many details as possible.
I have created a status bar app. One of the things I've done is made it to where there is a "Settings" menu item that if you click on, an NSWindow pops up with 5 different buttons -- these buttons are tied to their own custom NSViewController so that if you click them, the View Controller changes, but the window remains the same, just like any other StatusBar app.
On one of the view controllers is a popup button with several user selectable options. If I add a button to the specific VC (view controller) and link that to an ibaction that pulls the selected index of the popup button, it works just fine (i.e. I select item 2, it logs item 2).
Now here's where things get. . . odd. I've created a submenu that would in theory allow the user to quickly change the selected item of the popup linked to my custom view controller. Think of this submenu as being labeled 1-4 and clicking one of those items would change the index of the popup button, however every time I try this and follow it up with a NSLog to ensure the button has changed, it ALWAYS reads zero, in other words, it NEVER changes.
Here's what I tried.
Just like with every other view controller I've ever used, I create a new object of my view controller (alloc, init), and reference the popup button object that i've created as a property. This compiles just fine, but returns only zero no matter what is selected.
I've tried calling the method that I know works (the button that is on the view controller containing the popup), but again, this returns only zero no matter what is selected.
Any advice would be greatly appreciated.
tl;dr: can't change a popupbutton on a view controller from the app delegate or nsstatusmenu item, it just always returns zero.
EDIT: I have found a work around since I can communicate to my app delegate just fine, I created an int property and set it to zero. From there, whenever my popupbutton on my VC changes, I change the value of the app delegate int property and just ensure that whenever the nib launches, it set's the selected item to the app delegate property. I feel there has to be a more straight forward way to just get the value of the popup and change it from the app delegate.
EDIT 2: Some code as requested. I realized I needed to point the the view controller by using initwithnibname from the app delegate and then load the view. Doing so allows me to pull the first object in the popup button, any subsequent changes are not recognized unless I do so via my test button on the view controller itself. Also, I'm adding values to my popup in my awakefromnib in the view controller.
searches_VC = [[Searches_ViewController alloc] initWithNibName:#"Searches_ViewController" bundle:nil];
[searches_VC loadView];
NSLog(#"Selected Title: %#",[[searches_VC profilePopupOutlet] titleOfSelectedItem]);

Custom NSPopUpButtonCell outlet / bindings

I'm having a problem with a custom NSPopUpButtonCell in a table that's instantiated when the table view is populated via bindings and a NSArrayController.
The pop up button cell is created but when attempting to access the outlet by overriding the pop up button cell's setMenuItem:item method it's nil.
Is this the expected behaviour..?
Should another method be used to replace the menu at creation time?
Basically I need the outlet to link back to my controller (NSWindowController) for that document window so I can customize the NSPopUpButtonCell menu accordingly from the custom popup button when it's populated.
A solution using bindings would be even better - but when overriding setObjectValue: I can see it's only never called with a nil parameter.. using a stock NSPopUpButtonCell results in a properly populated pop up menu, though.
(see also Why is NSPopUpButtonCell showing correctly when only setObjectValue:nil is called).
You don't need to override anything to populate an NSPopUpButtonCell in an NSTableView column. The thing to know is that you set the bindings on the NSTableColumn and not on the cell itself. Typically, you would have an NSArrayController in your xib that is bound to an NSArray containing all the options for the pop-up, and then you would select the column with the pop-up cell and go to it's bindings. Like in this screenshot (note the populated Content, Content Objects, and Selected Object bindings in the inspector on the right):
If you want a working example, you can check out this project I whipped up for another StackOverflow question. There's a bunch of unrelated stuff pertaining to making the NSPopUpButtonCell use NSAttributedStrings, but the bindings in the xib constitute a working example of how to bind an NSTableColumn with a pop-up whose options are populated by bindings.

iOS Development - How to receive button event from a custom view controller class?

So I've got this "save" button in my main view controller that I would like to move into a different view controller (WriterViewController). After doing this, my custom view controller class isn't able to respond to any of the button events. Not sure what im doing wrong here but here some screenshots:
Save button is first in my main view controller. I'll be moving it to the view controller to the right (WriterViewController).
As you can see, after moving the button into the intended view controller, the view controller has a custom class set to "WriterViewController".
I then ctrl + click and drag the save button so that i can link up it's "Touch Up Inside" event to the "saveEntry" method.
A screenshot of my WriterViewController.h file.
And the implementation.
Doing the same exact step for the main view controller did work and i was able to log my message. However, it wouldn't work for the custom view controller.
Any thoughts?
Thanks for reading!
Why not just make the save button from scratch for WriterViewController? It's not hard to drag out a UIButton and set it's text to "Save."
Regardless, have you clicked on the button in the scene and viewed the connection's inspector to make sure the connections are what you'd expect? It seems obvious, but sometimes the connections can get all screwed up if you forget to clean them up. (ie. you copy a button over, connections and all, and things don't act as you think they should.)

loading different text in same view controller

Hi I have a quiz app that I am working on, and on my RootViewController I have two buttons with action methods, now when I press on one of the buttons I get my questionViewController and text is loaded into specific fields, I do this by using the viewDidLoad method. When I go back to the RootViewController and click on a different button I want to show the same questionViewController but with different text, how can I go about doing this?
I just finished a project with similar requirement. One way to do this is to use static variable and class method in your questionViewController class.
You could implement viewDidAppear: on your questionViewController, and set the text there instead of viewDidLoad.
You could also implement a method on questionViewController, that updated the text fields, and call this method from the RootViewController.