highlight NSSearchField - objective-c

I have a NSWindow which consist a NSView which appear only on some specific occasion this NSView consist a NSSearchField I want if there is data on clipboard it will appear in searchField otherwise it would be empty.I am able to do this what I want is if there is Data in SearchField it should be in focus I tried it this way:
In NSViewController class there is a function which returns NSSearchField which is a class variable
-(NSSearchField*)getSearchField
{
return searchField;
}
In NSWindowController class I am making it first responder where pSearchContact is instance variable of NSViewController class
[[self window] makefirstResponder:[pSearchContact getSearchField]];
It is running smoothly but I don't know why searchField is not getting focus
Is their something like searchField will become first responder only if it is a part of NSWindow because in my case searchField is in NSView which is in NSWindow.
Thanks in Advance

Your question is a little confusing but I think what you're asking to boils down to "why can't I ever make my search field the focus?".
That one line of code:
[[self window] makefirstResponder:[pSearchContact getSearchField]];
has a little too much going on with it for my comfort (i.e. I wouldn't embed so much functionality - any pieces of which could go wrong or haywire - into one line of code).
How about doing something like this:
NSWindow * myWindow = [self window];
if(myWindow)
{
if(pSearchContact)
{
NSResponder * ourSearchField = [pSearchContact getSearchField];
if(ourSearchField)
{
[myWindow makeFirstResponder: ourSearchField];
} else {
NSLog( #"ourSearchfield is nil; why?" );
}
} else {
NSLog( #"pSearchContact is nil; why?" );
}
} else {
NSLog( #"myWindow is nil; why?" );
}
This might also allow you to narrow down on why the focus setting isn't working for you.

A late response, but I was having a similar issue which was fixed by unchecking refusesFirstResponder in Xcode/Interface Builder.

Related

textShouldEndEditing in NSOutlineTableView is getting called twice

I just implemented following method that suppose to take some action after the value of a NSTextField is changed in my NSOutlineView
-(BOOL)control:(NSControl *)control textShouldEndEditing:(NSText *)fieldEditor
{
NSLog(#"end editing");
NSTextField* tf = (NSTextField*)control;
if(selectedItem && [selectedItem isKindOfClass:[HSCategoryClass class]])
{
HSCategoryClass* c = selectedItem;
c.name = tf.stringValue;
// request the update from DB
[[NSNotificationCenter defaultCenter] postNotificationName:#"updatingCategoryName"
object:c
userInfo:#{#"sender":self}];
}
return YES;
}
However, when I'm done editing and hit enter key or navigate anywhere outside of the text field this method is getting called twice instead of just once.
Does anyone know why is this?!
Any kind of help is highly appreciated!
That routine does not signify that editing has ended. Instead, it's called to find out if it should end (hence the name of the method). It can be called by the framework any number of times, and you shouldn't be relying on it for this purpose.
Instead override the NSOutlineView's textDidEndEditing: method.
Be sure to call super.
So you'd subclass the NSOutlineView and in your subclass:
- (void)textDidEndEditing:(NSNotification *)aNotification
{
// do your stuff
[super textDidEndEditing:aNotification];
}

Objective-C, Binding IBAction to NSTextField

I have an NSTextField that repopulates itself based on the outcome of a result; I would like to automatically perform an IBAction (which is normally controlled by clicking a NSButton in the app). How could I perform the click, or IBAction automatically based upon the result?
appDelegate.h
#interface main : NSView <NSApplicationDelegate>
{
NSView *main;
IBOutlet NSTextField *textValue;
IBOutlet NSWindow* window1;
IBOutlet NSWindow* window2;
}
- (void)textUpdated;
- (IBAction)switchWindow:(id)sender;
Which I would like to trigger or bind to the (IBAction)switchWindow:
appDelegate.m
- (void)textUpdated
{
[textValue setStringValue:[NSString stringWithFormat:#"%#, Switching Window", stringValue]];
[self switchWindow:sender];
}
- (IBAction)switchWindow:(id)sender {
[self performSelector:[NSApp keyWindow]==window?#selector(openWindow):#selector(closeWindow) withObject:nil afterDelay:0.0];
}
- (void)openWindow {
[window1 showWindow:window2 open:YES];
}
- (void)closeWindow {
[window2 showWindow:window1 open:NO];
}
Switching to real code improves the question, but it's still hard to understand exactly what you're asking. As Josh Caswell pointed out, an action is just a method and you're free to call it yourself. You seem to be doing that already in -textUpdated, but your assertion that "it still doesn't do anything" doesn't give us much to go on. So:
Does -textUpdated get called? If no, there's your problem.
Does -switchWindow: get called? Stick a breakpoint in that method and see if you stop there. (Hint: If you answered 'yes' to the first question, you'll stop there.)
What do you expect to happen that isn't happening?
Are the values of the various variables (ivars and local variables) what you expect?
Do -openWindow and -closeWindow get called appropriately?
What does [NSApp keyWindow] return? Does it ever return a pointer equivalent to window? (And by the way, it wouldn't hurt to tell us what window means here.)
Did you perhaps intend to write something like (note the '1' after 'window'):
[self performSelector:[NSApp keyWindow] == window1 ? #selector(openWindow) : #selector(closeWindow) withObject:nil afterDelay:0.0];
Your code might be easier to understand if you just do one thing at a time. Yes, it's more lines, but it's a lot easier to debug:
NSWindow *keyWindow = [NSApp keyWindow];
if (keyWindow == window1) {
[self openWindow];
}
else if (keyWindow == window2) {
[self closeWindow];
}

UITextField editingChange Control Event not works

I have some textfields and I want to do when I change textfield1 text set text to other textfields. My code below. But it not works. How can I solve this?
- (IBAction)TCKimlikTextChange:(id)sender {
[TCKimlikText addTarget:self action:#selector(yourMethod: ) forControlEvents:UIControlEventEditingChanged];
}
-(void)yourMethod: (UITextField*)tf_{
if (tf_) {
if (TCKimlikText.text == #"1") {
AdinizText.text = #"Hacer";
}
}
}
Your code is very abstract. yourMethod, tf_ TCKimlikTextChange are all expressions that are not very human readable. You should work on your variable names.
I suppose your first method is a button handler. It just assigned a target and action to the text field, but does not call any method. You do not need that action if you use the delegate protocol.
To solve your problem: implement the UITextField delegate methods. Make sure you set the delegate (probably self) for your text fields. Your view controller must mention the <UITextFieldDelegate> protocol in its .h file. Thus, in textField:shouldChangeCharactersInRange:replacementString::
if ([textField.text isEqualToString:#"1"]) {
displayLabel.text = #"Hacer";
}
Notice that you need isEqualToString: to compare strings, a simple == won't do.
If u are want to change on the click of the return button use the delegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
if(textField == field1)
[field2 setText:field1.text];
[field1 resignFirstResponder];
return YES;
}
or u can use other delegates too like:
– textFieldShouldBeginEditing:
– textFieldDidBeginEditing:

Compare self.parentViewController to a given UIViewController

Is it possible? That is, can I do something like this, which doesn't work...
if (self.parentViewController == CustomViewController) {
// do something
} else {
// do something else
}
...to make, for example, a Settings panel look more like either of its possible parent controllers? (Example again, if the menu is soft and light but the gameplay is kinda dark, it'd be convenient to say "if your parent is A look like this, but if your parent is B, look like this")
If it definitely can't be done, or if comparing the parentViewController to something else is dangerous/messy, I'll just set a flag fromView and code according to that.
You could go with if([self.parentViewController isKindOfClass:[CustomViewController class]])
self.parentViewController returns NavigationController so it doesn't work, so I found other way, this worked for me:
unsigned long currentVCIndex = [self.navigationController.viewControllers indexOfObject:self.navigationController.topViewController];
//previous view controller
UIViewController *view = (UIViewController *)[self.navigationController.viewControllers objectAtIndex:currentVCIndex - 1];
if([view isKindOfClass:[CustomViewController class]])
{
[self runSomething];
}

Updating UI Elements with Controller Constructor

I have an NSSlider (slider) and an NSLabel (label) on a window. I also have a class "Controller" that updates the label whenever the slider's value is changed.
The default position of the slider is 0.5, I'm trying to get is where Controller's constructor updates the label upon program launch.
The following is the implementation file for my attempt to do this. Everything works fine except the label is always 0 when I start the program.
#implementation Controller
{
}
-(id)init
{
NSLog(#"initializing...");
[self updateLabel];
return self;
}
- (IBAction)sliderChanged:(id)sender
{
[self updateLabel];
}
- (void)updateLabel
{
label.text = [NSString stringWithFormat:#"%.1f", slider.value];
}
#end
In the console I see the text "initializing...", but the label is never updated. What am I missing?
The controller may be getting initialized (where is your call to [super init]?), but that doesn't mean the outlets are hooked up. The proper way to do that would be to rely on a viewDidLoad, windowDidLoad, or awakeFromNib method.
you should achieve this with bindings and without any "glue code" in controllers.
Here's some reference on how to use them: http://cocoadevcentral.com/articles/000080.php