How to use "takeStringValueFrom" with Button onclick Event? - objective-c

I have a button that correctly wired such that when it is clicked, the "login" IBAction is hit.
- (IBAction) login: (id)sender
{
NSLog(#"OK");
}
Now, I wired the button to the username/password text fields using "takeStringValueFrom" but I don't understand how to then get at those values?
Hope that makes sense.
EDIT: so basically, when i click the "login" button the above event fires. I'd like to grab the values from two text boxes on that same window, what is the best way to accomplish this? I suppose I could use IBOutlet for each of the text boxes ... is this the correct way?
Re-reading the doc, it's possible "takeStringValueFrom" isn't what i thought it was.

You want to declare your username and password textfields as IBOutlets, and then hook them up in Interface Builder. Then, in your login handler, you use the stringValue message to extract their values:
- (IBAction) login: (id)sender
{
NSString *username = [usernameTextField stringValue];
NSString *password = [passwordTextField stringValue];
// check username & password
}

Related

NSWindow, press key ENTER: how to limit the key listening to the focused NSControl?

I have an NSWindow with a main "OK" button. This button has as "key equivalent" property in interface builder, the key ENTER i.e ↵.
It works good, but now I have a new NSComboBox, which is supposed to invoke a method when the user selects a list item, or he preses Enter / ↵.
However, when I press Enter, the main Button receive the notification and the window close. How to prevent this?
This is the normal behavior what you are getting, but you can hack a bit, by removing and adding the key-equivalent.
Add following delegates of NSComboBox:
- (void)comboBoxWillPopUp:(NSNotification *)notification;{
[self.closeButton setKeyEquivalent:#""];
}
- (void)comboBoxWillDismiss:(NSNotification *)notification;{
[self.closeButton setKeyEquivalent:#"\r"];
}
One way you can workaround for prevent enter notification is like that below:-
//Connect this action method to your combobbox and inside that set one BOOL flag to yes
- (IBAction)comBoxItm:(id)sender
{
self.isEnterCalled=YES;
}
//Now check this flag to your some method where close window is called
-(void)someMethod
{
//Check the flag value if it is yes then just ignore it
if (!self.isEnterCalled)
{
//Close window logic
}
self.isEnterCalled=NO;
}
Ran into the same problem. Had "hot key" which I'd like to switch off while editing some text fields. I found solution for myself. There's no need in override lots of NSTextField base methods.
Firstly, I removed all the "key equivalents". I used to detect Enter key down with the + (void)addLocalMonitorForEventsMatchingMask:(NSEventMask)mask handler:(NSEvent *(^)(NSEvent *))block class method of NSEvent. You pass block as a parameter, where you can check for some conditions. The first parameter is the event mask. For your task it would be NSKeyDownMask, look for other masks at the NSEvent Reference Page
The parameter block will perform each time the user pushes the button. You should check if it is right button pushed, and - generally - if the current window first responder isn't some editable control. For that purposes we need NSWindow category class just not to implement this code each time we deal with NSKeyDownMasked local monitors.
NSWindow+Responders class listing:
#interface NSWindow (Responders)
- (BOOL)isEditableFirstResponder;
#end
#implementation NSWindow (Responders)
- (BOOL)isEditableFirstResponder
{
if (!self.firstResponder)
return NO; // no first responder at all
if ([self.firstResponder isKindOfClass:[NSTextField class]]) // NSComboBox is NSTextField subclass
{
NSTextField *field=(NSTextField *)self.firstResponder;
return field.isEditable;
}
if ([self.firstResponder isKindOfClass:[NSButton class]]) // yep, buttons may be responders
return YES;
return NO; // the first responder is not NSTextField or NSButton subclass - not editable
}
#end
Don't know if there's another way to check if we are now editing some text field or combo box. So, there's at least the part you add the local monitor somewhere in your class (NSWindow, NSView, some controller etc.).
- (void)someMethod
{
id monitor=[NSEvent addLocalMonitorForEventsMatchingMask:NSKeyDownMask handler:(NSEvent *)^(NSEvent *theEvent){
if (theEvent.keyCode==/*Enter key code*/ && ![self.window.isEditableFirstResponder]) // you should check the key modifiers too
{
// your code here
}
return theEvent; // you may return the event to pass the key to the receiver
}];
}
Local monitors is safe remedy about the Apple rules. It works only inside your application. For global key down events you may use addGlobalMonitor but Apple may reject your app from the AppStore.
And don't forget to remove the monitor when there's no need in it.
- (void)viewControllerShutdownMethod
{
[NSEvent removeMonitor:monitor];
}
Good luck.

How to call a method when values has been entered in text box

In my app, i have a text box and handheld scanner. when i scanned something, the barcode values comes in text box. I don't need any special code to get the bar code value inside the text box. Till here i am fine. But now when i have a value in textbox , i want to call a method which will further do some changes in Core data. I have the code to make changes into core data for the row with value = value in text box.
But i don't know how to call this method or how do i know that values been entered in text box and i can call the method.
Please help me in doing that.
Sometimes handheld device add return character to the end and if yours do that add delegate to the text field
self.textField.delegate = self;
and see is that method called:
- (void)textFieldDidEndEditing:(UITextField *)textField
{
NSLog(#"textFieldDidEndEditing method called");
}
If this method is not called add notification to the text field which will be called when the text changed:
[self.textField addTarget:self action:#selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
and add method which will be called:
-(void)textFieldDidChange(id)sender
{
NSLog(#"textFieldDidChange method called");
}
You need to create an IBOutlet variable and link it to your textbox.
#interface YourClassOrController
{
IBOutlet NSTextField *myTextField;
}
...
#end
In interface Builder, you need to Ctrl-Drag to link your textfield to your variable.
Finally in your code you use
NSString code = [myTextField stringValue];

How to enable the next textfield in Objective C

I have two textfields (a username and a password) field and I remember that sometimes the keyboard has a little < or > to navigate through the textfields and I was wondering if there is an option or code that would allow me to do this natively in my app. Any tips or hints are appreciated. Thanks!
If you would like an acceptable solution that doesn't involve implementing a third party library, you can do it like the following:
Set the return button on the virtual keyboard to be a "Next" button for the username field, and a "Go" button for the password field, and make your view controller a UITextFieldDelegate. Set your view controller as the delegate for both text fields, then implement the textFieldShouldReturn: like the following:
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
if (textField == self.username) {
[self.password becomeFirstResponder];
}
else if (textField == self.password) {
[self.password resignFirstResponder];
[self signInPressed];
}
return YES;
}
This will make it so if you have the username field selected, the "Next" button on the keyboard will advance to the password field, and when the password field is selected, the "Go" button will call the signInPressed method, which you just change to whatever you have your sign in method named.
There's no builtin way to add these buttons.
Look into inputAccessoryView to understand how to extend the keyboard.

Writing a function in objective C that can automatically print something using the iphone keyboard in a text filed

Hi people I have the following problem I have made an iphone application in which I have made the UI of the application using the interface builder and have a text field, a button and a label whenever a user enters some text in the text field and presses the button the message "Welcome : text field content" gets displayed in the label I have written the following IBAction for the button click
-(IBAction)updateText:(id)sender{
NSString *text;
if( [ textName.text length] == 0 )
{
text = #"Please Enter Your Name";
}
else
{
text = [ [NSString alloc] initWithFormat:#"Hello : %#" , textName.text ];
}
lblHello.text = text;
[text release];
}
here textName is the name of the text field and lblHello is the label
but I want to automate this process of entering the text in the text field and pressing the button by writing some objective C function so that the values are entered automatically using the iphone keyboard and then the button is clicked. Can anyone help me write that function and where to call that function so that the task gets done. Please help me out with this and do tell if you need more code for reference.
Thanks in advance
For this you should use the UITextFieldDelegateMethods.
Have a look at:
textField:shouldChangeCharactersInRange:replacementString:
The text field calls this method whenever the user types a new character in the text field or deletes an existing character.
For delegation you have to set the delegate of the textfield to your viewcontroller via code or Interfacebuilder. And you have to declare in your viewcontrollers header file that the class conforms to the UITextFieldDelegate protocol.
As safecase mentioned you could use the delegate method
textFieldDidEndEditing:
too. The difference between the to is the time when the label gets updated: textFieldDidEndEditing: is called after the editing in the textfield and textField:shouldChangeCharactersInRange:replacementString: is called during typing.
Code:
In you .h file:
#interface YourClass : YourClassSuperClass <UITextFieldDelegate>
#propertys
-(void)someMethod;
#end
in your .m file you select the delegate method you'd like to implement for demonstrating I'll choose textFieldDidEndEditing::
#implementation YourClass
- (void)viewDidLoad {
textField.delegate = self;
}
#pragma mark - UITextFieldDelegate methods
- (void)textFieldDidEndEditing:(UITextField *)aTextField {
lblHello.text = aTextField.text;
}
If you like you can set textField.delegate via IB too. Don't forget to set the delegate or otherwise the delegate methods won't get called.
Please get familiar with the delegation pattern since it is often used!
For more information how delegation works and how to use it you can read
Concepts in Objective-C Programming -> Delegates and Data Sources
Use UITextFieldDelegate method for this. When user will click on return button of iphone keyboard this method will be called:
- (void)textFieldDidEndEditing:(UITextField *)textField
{
if( [textName.text length] == 0 )
{
text = #"Please Enter Your Name";
}
else
{
text = [ [NSString alloc] initWithFormat:#"Hello : %#" , textName.text ];
}
lblHello.text = text;
}

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.");
}
}