Access on buttons in NSTableCellView - nstableviewcell

In my macOS XCode-project I addd a button to my Table Cell View which I want to access programmatically.
When I connect the button to an IBOutlet or IBAction I get following error message when I try to build my project:
/Users/homefolder/Desktop/myapp/myapp/Base.lproj/Main.storyboard: The myButton outlet from the ViewController to the NSButton is invalid. Outlets cannot be connected to repeating content.
Same for IBAction which is self-explaining.
Can you tell me how to access the button for each Table Cell View?
PS: Edit:
There is almost no code to show:
I added an IBOutlet in ViewController.swift:
#IBOutlet weak var remove_BTN: NSButtonCell!
Then I get the error as described above.
Same for the IBAction:
#IBAction func remove_BTN(_ sender: Any) {}

Related

How connect a UIButton to the UIViewController Exit with code Swift

Lets say I have two UIViewControllers, we'll call them VC1 and VC2. VC1 performs a segue that then goes to VC2, and VC2 has a close button that will unwind that segue back to VC1.
So normally, if I'm wanting to make a custom UIStoryboardSegue I would make a segue from VC1 to VC2 and then select custom and use my custom UIStoryboardSegue I made that and I have it working just fine.
And for the close button I would put in code in the destination UIViewController (VC1) like this:
#IBAction func prepareForUnwind(unwindSegue: UIStoryboardSegue) {
//some code
}
Then on the UIViewController with the close button, I would ctr + drag up to the EXIT and select that unwind prepareForUnwind. Then I'd select the UnwindSegue on the left panel and change the class to my unwind class.
The problem is, in my case I am having to create that button with code, so I don't have the option of ctr + drag. So how do I connect it to the exit and then assign the class for the unwind segue with code only? I've tried googling it, but everything keeps coming back with the method when you are using the storyboard.
I can include more code if necessary, just let me know.
You can connect your button in this way:
First Step
In the Storyboard connect with drag and drop your source view controller name icon with the exit icon, there you should see in the pop-up your prepareForUnwind function.
Then you can set for your unwind segue an identifier.
Second step
Add an #IBAction function for your close button and perform the segue:
#IBAction func closeAction() {
performSegue(withIdentifier: "yourExitIdentifier", sender: self)
}

disabling UIButton throws an error

I have two UIButtons, I want one button when pressed, to disable the other button.
I have tried code from different posters but have been unable to get one UIButton to disable the other UIButton.
I am able to setTitleColor and disable using Sender but that won't work for the other button.
Here is some partial code of the IF/Else statement, but it should be enough for the experienced coder.
This code builds successfully but throws an exception at the last line shown when run. Fatal Error: unexpectedly found nil while unwrapping an Optional value. Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_I...
import UIKit
import CoreData
class ViewController: UIViewController {
#IBOutlet weak var buttonAdd: UIButton!
#IBOutlet weak var buttonEdit: UIButton!
#IBAction func buttonAdd(_ sender: AnyObject)
{
if (sender.currentTitle == "Add")
{
sender.setTitle("Save", for:UIControlState.normal)
sender.setTitleColor(UIColor.red, for:UIControlState.normal)
buttonEdit.isEnabled = false FAILS ON THIS LINE
This generally happens when the outlet from your storyboard to your code is messed up. Go to storyboard, right click on the yellow ViewController symbol and you will see a warning. You should delete the entry with the warning by clicking on the 'x' and recreate a new outlet form storyboard to your code.

(OSX/Cocoa) How to set controller for main window

I've created a new cocoa application using .xib files (not storyboarded, the app has to be backwards compatible with mavericks/mountain lion) and I would like to have a custom windowcontroller for the main window. Is this possible? I can't seem to find a way to connect the window to my desired custom controller. The window has a reference outlet in AppDelegate, however I need a custom NSWindowController for this window as it doesn't open on application launch. The application launches silently as a menu bar app and the main application is launched via button press in the drop down from the menu bar.
Is there a way to link a controller to the window in the interface builder? Or do I have to do something along the lines of :
wc = [[CustomWindowController alloc] initWithWindowNibName:#"Main"];
Thanks!
Yes, open up Utilities (the right panel) in Interface Builder, and at the bottom click on the Object Library (circle with square in it).
Search for Object (blue cube), and drag it into your Document Outline (the panel on the left inside of interface builder)
From there, select that object you just created, and change the Class in the Identity Inspector to be the window controller you want.
Finally you can go into the Connections Inspector and connect your window to the window outlet
I can't seem to find a way to connect the window to my desired custom
controller. The window has a reference outlet in AppDelegate, however
I need a custom NSWindowController for this window as it doesn't open
on application launch.
Another way:
1) Delete the window in MainMenu.xib. Delete the window property in AppDelegate.m--because you deleted the window, it is no longer relevant.
2) File>New>File>Cocoa Class. Enter a class name, e.g. MainWindowController; select "Subclass of: NSWindowController"; check "Also create .xib file for user interface".
3) Create an outlet in AppDelegate.m:
#import "AppDelegate.h"
#import "MainWindowController.h"
#interface AppDelegate ()
#property (strong, nonatomic) MainWindowController* windowController;
#end
4) Declare an action in AppDelegate.h:
#interface AppDelegate : NSObject <NSApplicationDelegate>
-(IBAction)launchWindow:(id)sender;
#end
and implement it in AppDelegate.m:
- (void)launchWindow:(id)sender {
[self setWindowController:[[MainWindowController alloc]
initWithWindowNibName:#"MainWindowController"]];
[[self windowController] showWindow:nil];
}
5) In MainMenu.xib, hook up the Menu Item to the launchWindow() action: control drag from the Menu Item to the AppDelegate object and select launchWindow.
Create the controller and make it extend from NSWindowController.
In your xib file select the File's Owner and set it to your custom class.
Select your NSWindow and connect it to the File's Owner.
To open the window:
In your .h:
#property (strong, nonatomic) YourWindowController *yourWinController;
In your .m:
self.yourWinController = [[YourWindowController alloc] initWithWindowNibName:#"YourWindowController"];
[self.yourWinController showWindow: nil];

Can't find Storyboard ID in Identity inspector

I'm trying to find the "Storyboard ID" which should be before Restoration ID in Identity inspector, but I can't find that for any view I've selected. I've tried to open a new project and I still can't find it either.
I'm using Xcode 5.0.2 developing for iOS-7.
Can you tell me what I'm missing here?
You are pointing to a UIView or some other object on the StoryBoard. Press the yellow indicator on top of the other objects which is your ViewController
The storyboard ID is a string field that you can use to create new UIViewController (not UIButton or other elements). An example of how to use it can be found here.
only View Controller has Storyboard ID
other view (eg: UIButton) no Storyboard ID
how get id of a normal view (in Storyboard) ?
two method:
use Referencing Outlet = normally is weak reference ~= like pointer in C
eg: add reference for UIButton
in Storyboard -> choose your UIButton -> Right Click UIButton -> Referencing Outlets->New Referencing Outlet -> drag it to ViewController.h -> got notice Insert Outlet -> popup window -> set Name to you want, eg deviceNameBtn -> Connect -> auto generated code into ViewController.h
#property (weak, nonatomic) IBOutlet UIButton *deviceNameBtn;
then in you (Objective-C) code, you can use deviceNameBtn as the ID of UIButton, do whatever you want
use (int type) tag
eg: your UILabel view's tag = 1
get your view:
UILabel *label = (UILabel *)[self.view viewWithTag:1];

How can get a reference to an NSWindow created in a xib file?

I use a xib file to show an NSWindow named mainWindow - now I want to get a reference to mainWindow via code (e.g. NSWindow *mainWindow). I can't find anything in the documentation, any pointers?
The xib file will have placeholder objects in it for the app delegate and / or the file's owner.
On the assumption that it has the app delegate in it, you can get a reference to the window or any object in the xib by
Declare a property in the app delegate of the right type and with IBOutlet as part of its type:
#property (weak) IBOutlet NSWindow* theWindow;
Locate the app delegate object in the xib. Click and drag it while the control key is pressed. You should get a line between the mouse pointer and the object.
Drag on to the window and release the mouse button.
You should see a list of the outlets in the app delegate. Select theWindow and your done.