Event triggered on entering a textbox. - xcode

Is there any way of knowing when a text-box has been clicked on so that this can trigger an event? This may not be the best way to do it but I want to animate the text-box to a different part of the screen when it is clicked and then return to its original position when finished.

If your question related to iOS, Set delegate for the UITextField, and in following delegaqte method, add your code.
- (void)textFieldDidBeginEditing:(UITextField *)textField
Setting delegate

Related

NSSplitView Collapse Event

I'm trying to implement an NSSplitView similar to Xcode where you can collapse a view by dragging its handle to under half its width and it will collapse automatically. I have that part working, but I need to update the state of a button in the toolbar when this happens. I tried listening to splitViewDidResizeSubviews and checking if the splitView's view is collapsed, but that method fires 16 times with collapsed == true, so I don't think I want to update the button's state 16 times. Is there a cleaner way to do this? I'm new to Cocoa, but from what I've seen, I would expect there to be some way to just say bind this button's state to the isCollapsed property and be done with it. Does such a thing exist?
If you subclass your NSSplitViewController you can add a listener for the SplitViewItem's isCollapsed property:
class MySplitViewController: NSSplitViewController {
var observer: NSKeyValueObservation?
override func viewDidLoad() {
super.viewDidLoad()
// Do view setup here.
let sideViewSplitViewItem = splitViewItems[0]
observer = sideViewSplitViewItem.observe(\.isCollapsed, options: [.initial, .new]) {splitViewItem, _ in
print("Sidebar collapsed state changed to: \(splitViewItem.isCollapsed)")
}
}
}
The best way to see what bindings are available is to check the docs, specifically the Cocoa Bindings Reference (look in the sidebar for the view you're after).
NSSplitView doesn't have the binding you describe, so I think you're on the right track with your current approach. Of course, you don't need to update the button's state sixteen times, just check it's value each time, and update it if needs be. It might seem a bit wasteful, but checking the value of a bool is a very cheap operation, and you won't notice any kind of performance hit.
While NSSplitView has no event or observable property for when one of it's subviews is "collapsed", the immediate subview itself will have its hidden property set to YES.
So you can either observe the subview's hidden property yourself, or if you're creating your own custom subview of NSView, you can override the -(void) setHidden:(BOOL)hidden to catch the event.
I do the latter, and it works correctly every time.
-(void)setHidden:(BOOL)hidden
{
[super setHidden:hidden];
// Insert code to react to the "collapsed" event here.
// (You're likely going to tell your parent view,
// self.window.windowController, or something.)
}
Of course the setHidden method / hidden property can in theory be called for purposes other than from NSSplitView "collapsing" your view, but this is very unlikely to ever happen in practice.

How to cancel touches on a child UIControl?

I have a view that contains a subclass of a UIControl.
Occasionally I want to add a tap gesture recognizer to the view that should override any other touches on the children views.
By default gesture recognizer should cancel all other touches in view. However, my UIControl still gets triggered. Is that expected behavour?
I haven't found an answer to this particular problem. So the solution in my situation was to use a different combination of actions to achieve the desired outcome.

Remove Focus from UITextField

I'm trying to remove focus from a UITextField and even though I resign it from being first responder, I'm still not able to have the cursor not focus on the text field.
I don't have any other input on the view to move the focus to and I don't want to create a dummy one either. What is a good workaround for this?
As per the documentation.
To dismiss the keyboard, send the resignFirstResponder message to the text field that is currently the first responder. Doing so causes the text field object to end the current editing session (with the delegate object’s consent) and hide the keyboard.
If you call resignFirstResponder on your textfield then it will end the editing session and the cursor wont be focussing on that textfield.
So please verify one more time whether resignFirstResponder is getting called on that textfield which you want to remove the focus.
Please try to set your current class as delegate of your UITextField. I think you forget to set the delegate that's why it's not working as you are expecting.

Stop UIPickerViewAnimation

Is there a way to instantly stop the animation of rolling UIPIckerView or UIDatePicker on button click.
Look into - (UIView *)viewForRow:(NSInteger)row forComponent:(NSInteger)component
Since Picker can only show limited number of results at a time. You can implement an algorithm to determine what is in the middle of the result when this method is called. It gets called every time a new option appears (just like tableView viewForIndexPath). So scrolling up or down will call this method.
Beware, you would need to add padding options to first few choices and last few choices just so that it works correctly. This will get you the effect you want. Be warned, Apple might not agree with this method as it doesnt act like what Apple intended picker to do.
Let me know if it works.
If you look at the UIPickerView class reference, you'll see:
selectRow:inComponent:animated:
Selects a row in a specified component
of the picker view.
- (void)selectRow:(NSInteger)row
inComponent:(NSInteger)component
animated:(BOOL)animated
You could call this method when your button is pushed, sending NO for the animated parameter, to instantly select a particular row.

Hide/disable NSComboBox button

Is there a way to hide the pop-up button of an NSComboBox? I can't find anything in the documentation for NSComboBox or NSComboBoxCell. There is a setButtonBordered: method on NSComboBox, but this just changes to an alterate button style.
If I can't hide it, can I at least disable it?
If the combo box has no items, clicking the pop-up button doesn't do anything.
Maybe you can work around the limitation by emptying the list when you want to disable the button.
It makes clicking have no effect, but it doesn't hide the button or draw it as disabled.
I don't think this is possible. An NSComboBox without the button is effectively an NSTextField, so I guess it was deemed unnecessary. You could probably do this by subclassing NSComboBoxCell and override -drawWithFrame:inView: or -drawInteriorWithFrame:inView:.
Safest way would probably be to add your own buttonHidden property and use the ObjC runtime method class_getMethodImplementation to look up the IMP for the same method in NSTextField and just call that when the button is hidden. You'd effectively be calling super's super, so you'd get a regular text field look.
you can disable it by doing:
myComboBox.Enabled = false;

Resources