Cannot paste text inside menu item's popup panel - objective-c

I have a menu application and I'm launching a NSPanel from a menu item: when the user clicks on the menu item I lazily instantiate a custom NSWindowController (just the first time), and then I show it calling showWindow:
The custom NSViewController is linked to a xib file:
This is how I create it:
// #property (nonatomic,strong) AddFeedController* addFeedController;
- (AddFeedController*) addFeedController
{
if (!_addFeedController)
{
_addFeedController = [[AddFeedController alloc]initWithWindowNibName:#"AddFeedController"];
}
return _addFeedController;
}
The problem is that if I try to paste some text inside of one of those two text field, it doesn't paste anything and it beeps.
EDIT
I managed to restore the original menu (I had to copy it from another project's xib, because only the original is recognized), and I modified the addFeedController accessor this way:
- (AddFeedController*) addFeedController
{
if (!_addFeedController)
{
_addFeedController = [[AddFeedController alloc]initWithWindowNibName:#"AddFeedController"];
[_addFeedController.window setLevel: NSPopUpMenuWindowLevel];
}
return _addFeedController;
}
I also modified the method that displays the window:
- (IBAction) launchFeedController : (id) sender
{
[self.addFeedController showWindow: self];
// I added these lines:
[NSApp activateIgnoringOtherApps:YES];
[self.addFeedController.window makeKeyAndOrderFront:self];
}
The first time the panel appears immediately, with no problem. But when I close the panel and try to launch it a second time it doesn't appear.

Related

NSWindow not displayed when closeWindow command is present

I need progress feedback in my app. Using an NSWindowcontroller (subclass IndicatorWindowController) that controls a Window containing an NSProgressIndicator (myBar). Everything works fine if I don't finish the procedure with [myIWC.myWindow close]. If I have this command, the window won't even show.
- (IBAction)runIndicatorWindow:(id)sender;
{
IndicatorWindowController* myIWC = [[IndicatorWindowController alloc] initWithWindowNibName:#"ProgressWindow"];
[myIWC showWindow:sender];
for (double barLoop=0; barLoop<=100; barLoop++) {
myIWC.myBar.doubleValue = barLoop;
[myIWC.myBar display];
}
[myIWC.myWindow close];//<-When this is not commented out, nothing shows
}

Access Property from Another Class, separate XIB

This might be an easy question, but bear with me I am very new and just experimenting. MacOS, Not iOS, let's say I have two separate XIB files (MainMenu.xib and AnotherWindow.xib). I am using the File Menu in MainMenu.xib to open AnotherWindow.xib, and disable the file menu when it opens with:
- (IBAction)OpenAnotherWindow:(id)sender {
if (!anotherWindow) {
anotherWindow = [[AnotherWindow alloc] initWithWindowNibName:#"AnotherWindow"];
}
[anotherWindow showWindow:self];
[self.MenuItem setEnabled:NO];
In the AnotherWindow.xib, I want to re-enable the file menu when it closes using:
- (void)windowWillClose:(NSNotification *)aNotification {
[self.MenuItem setEnabled:YES];
}
The problem I have is I am not able to access the MenuItem from the second class because it is part of MainMenu.xib - so I just get error: Property not found on object of type with the [self.MenuItem setEnabled:YES]; in the AnotherWindow.xib
So I guess my question is: How can I access a property like
#property (weak) IBOutlet NSMenuItem *MenuItem;
That is in my MainMenu.xib from AnotherWindow.xib.
Instead of manually enabling and disabling the menu item, override the function validateUserInterfaceItem in the class that contains the OpenAnotherWindow IBAction.
The validateUserInterfaceItem function takes an item of type NSValidatedUserInterfaceItem as an argument. Check if the item's action is OpenAnotherWindow. If it is, check if anotherWindow is open. If it's open, return false, which will disable the menu item. If the window isn't open, return true, which will enable the menu item. My Objective-C is rusty so I don't have a code listing for you.

Mac: NSWindowController, view does not open after it's closed

This is probably something simple that I am missing but this is what is happening:
I am making a Menu item app (application is agent = YES)
So I have a MainViewAppDelegate that kind of runs the entire thing. It gets a detection when a button on the menu item gets clicked.
Then I have an upload NSWindowController:
#import <Cocoa/Cocoa.h>
#interface UploadView : NSWindowController
- (IBAction)upload:(id)sender;
#end
.m:
-(id)init {
if (! (self = [super initWithWindowNibName:#"UploadView"])) {
// Initialization code here.
return nil;
}
return self;
}
In MainViewAppDelegate, when that button is pressed I do:
if (!uploadView) {
uploadView = [[UploadView alloc] init];
}
[uploadView showWindow:self];
Now, this works as long as I don't click out of xcode. Once I do that (say I make chrome the active tab) then it does not work anymore even if I go back to xcode.
Any thoughts?
I dont see any issue in the UploadView code. But can you try connecting the UploadView Panel in xib to File's Owner window outlet and try. I created a similar demo now and tried and i dont see any issue.
All I had to add was:
[NSApp activateIgnoringOtherApps:YES];
before trying to open the window.

Cocoa/OSX - NSTextField not responding to click for text editing to begin when titlebar is hidden

I have a NSTextField in my window (added with IB) with a hidden title bar but when I click on it when the app is running, it does not respond or place a cursor in its field. Is there anything I am doing wrong? it is setup in the most standard way possible, an editable textfield on a window.
Thanks.
Create a custom class that inherits from NSWindow, and set it as the NSWindow Object in InterfaceBuilder. Then override the following methods:
//
// canBecomeKeyWindow
//
// Overrides the default to allow a borderless window to be the key window.
//
- (BOOL)canBecomeKeyWindow
{
return YES;
}
//
// canBecomeMainWindow
//
// Overrides the default to allow a borderless window to be the main window.
//
- (BOOL)canBecomeMainWindow
{
return YES;
}

How to take text value of NSButton?

how should i take NSButton text value , e.g if i use 2 buttons with text Click and Cancel, i want to check which button is clicked and then show a message with NSRunAlertPanel(...) which button i have clicked..what code should i write for it when the button is clicked.
In you action method you get an argument, usually named 'sender', which is the button. So you could do something like:
- (IBAction)buttonClicked:(id)sender
{
if ([[sender title] isEqualToString:#"Click"]) {
NSLog(#"Click clicked.");
} else if ([[sender title] isEqualToString:#"Cancel"]) {
NSLog(#"Cancel clicked.");
}
}
It's better not to use the title for checking the button, since the title could change in different localizations. You could specify the tag instead, which is simply an int and which can be used to identify different senders.
The way this is typically implemented is that each button would call a different action, thus there would be no need to check the text of the button. See The Target-Action Mechanism.
In general it is almost always a bad idea to use the user visible text to control program logic because that makes localization harder.
You might also want to describe your situation further. Are you using Interface Builder to create your interface? Are these buttons in a modal dialog or a document window?
You could give the button a name in the class info tab of the inspector window in Interface Builder, then declare it as an IBOutlet in your app delegate.
AppDelegate.h:
IBOutlet NSButton *ClickButton;
IBOutlet NSButton *CancelButton;
Then hook up the outlet in Interface Builder, and just check to see which button is the sender in your method:
- (IBAction)buttonClicked:(id)sender
{
if (sender == ClickButton) {
NSLog(#"Click clicked.");
}
else {
NSLog(#"Cancel clicked.");
}
}