I'm trying to make a NSWindow close when I click a button for a new NSWindow. I'm not trying to connect the button to make it close because the button is what makes it open. Or can it do both? (i am very new to xcode, please excuse me if this sounds dumb)
Anyway here's my code I tried (no errors popped up or warning):
-(IBAction)switchTo:view2{
[view1 performClose:(id)view1];
}
I have 3 iboutlets ,
IBOutlet NSWindow *view1;
IBOutlet NSWindow *view2;
IBOutlet NSWindow *view3;
I have a button in view1 that opens view2, and a button in view2 that opens view3.
My only problem is when I click the button in view1 to view2, I want view1 to close.
Use [view1 orderOut:nil]; instead of performClose.
You can also connect a button with two selectors. Both will be called.
Related
A NSStatusItem has a NSMenu attached, and one of the buttons of the NSMenu opens a NSWindow. Whenever one of these buttons is clicked, the window opens as expected and works properly, but another display of the NSStatusItem is opened.
The NSStatusItem is a clock, so I can see that it is updating correctly. However, the cloned NSStatusItem doesn't have its own menu. If I push the button that makes the window more times, more cloned versions of the NSStatusItem pop up.
Everything works fine except for this.
That's not a whole lot of information to go off of, but there's nothing else I can think of that could potentially help you. I would be happy to provide more information or try something.
EDIT: Every time the button is clicked, awakeFromNib is somehow called, which is why another half-working NSStatusItem happens.
EDIT: Temporary workaround is to put the awakeFromNib method in a dispatch_once.
EDIT: Added method that is triggered when button is clicked, as suggested by #zpasternack
- (IBAction)preferences:(id)sender {
self.windowController = [[NSWindowController alloc] initWithWindowNibName:#"PreferencesWindow"];
[[self windowController] showWindow:self];
}
Is the NSStatusItem contained in the PreferencesWindow nib? That might explain it, since you're loading the nib each time the button is clicked.
Also, is there a reason you need to recreate that window each time the button is clicked? Maybe you could only do it the first time?
- (IBAction)preferences:(id)sender {
if( self.windowController == nil ) {
self.windowController = [[NSWindowController alloc] initWithWindowNibName:#"PreferencesWindow"];
}
[[self windowController] showWindow:self];
}
I created a simple cocoa project, and added an NSButton in the window.
Then I added an NSScrollView to the window and hided the NSButton.
However, when I click the scroll view , it is strange that the NSButton action responds!
I guess there is something with the touch event chains, but I failed to find it.
For example, I try to use:
- [NSView becomeFirstResponder];
- [NSView setAcceptsTouchEvents:];
SO what I want is the only the front-most view to become the first responder, and the touch event will not be sent to its superview or so.
Thanks.
This is the view hierarchy:
the scroll view and button are both added to the window view, and the scrollview's frame includes the button's frame. In other words, the button is hidden by the scroll view but still receives click events.
You need to add mouseDown: event in NSScrollView or NSCrollView's View. like this:
-(void)mouseDown:(NSEvent *)theEvent {
NSLog(#"MouseDown in NSView");
}
I'd like to create a custom NSTableCellView instantiated by Interface Builder. I've set my Table Cell View class to MyTableCellView, and properly created MyTableCellView : NSTableCellView .m/.h files.
However, I just can't CTRL+Drag a simple button from inside this view to MyTableCellView.h in order to create an IBOutlet.
Here is a video to show what happens: http://youtu.be/sNNbuVT-SZs.
How the view is subclassed:
How I try to CTRL+Drag a button
Also, sometimes, Interface Builder just don't allow the cell view's class to be modified. What's happening ?
I finally found a solution, that is a little weird but works as expected. Instead of connecting the NSButton to MyTableCellView header directly, I used the reversed path:
Manually create an outlet:
#property(retain, nonatomic) IBOutlet NSButton* button;
Then click the empty circle on the left, and drag it to your XIB file's button:
I have no idea why it works this way, please let me know if you know the anwser.
Is there anyway to fire the "touch on UITextField" event? Because I would like to place a button that, if pressed, sets the focus on a specific UITextField and starts editing.
Thanks.
Call [textField becomeFirstResponder]; when user pressed on the button you want.
Once the code bellow is executed, the textfield's text doesn't change in the UI to "Fly" but the second NSLog does print "TextField: Fly" as it should.
#property (strong, nonatomic) IBOutlet UITextField *typeTextField;
....
UITableViewCell* cell = [self.theTableView dequeueReusableCellWithIdentifier:#"TypeCell"];
self.typeTextField = (UITextField*)[cell viewWithTag:1];
NSLog(#"TextField: %# ", self.typeTextField.text);
self.typeTextField.text = #"Fly";
NSLog(#"TextField: %# ", self.typeTextField.text);
Any help would be much appreciated.
You almost definitely forgot to connect the outlet for the UITextField in interface builder. Bring up the .xib file that that typeTextField is visible in, click on typeTextField, then show the Utility pane (the one on the far right in Xcode 4+). Click the Connections Inspector (the one that looks like a right arrow) and drag a New Referencing Outlet to your File's Owner.
When you don't connect the UITextField you drew in Interface Builder with the IBOutlet that you identified in your source file, both UITextFields get created as separate entities. You can make changes and work with the valid typeTextField with a broken IBOutlet, but it'll never appear on your view.
Consult How to connect an IBOutlet from an UITableViewController directly to custom cell? and http://www.youtube.com/watch?v=d_kO-J3DYvc on properly wiring your custom UITableViewCell objects.