Question about Delegate / Protocol requirements
Your code snippet is not quite how UITextFieldDelegate
protocol is defined. Two observations:
The text field delegate protocol does not include a
delegate
property.Yes, the text field has a
delegate
property:@available(iOS 2.0, *)
open class UITextField : UIControl, UITextInput, NSCoding, UIContentSizeCategoryAdjusting {
...
weak open var delegate: UITextFieldDelegate? // default is nil. weak reference
...
}But the delegate protocol has no requirement for a
delegate
property in the view controller (or whatever you specify as the delegate).The methods are
optional
.The actual definition is as follows (found by pressing shift-command-o or “File” » “Open Quickly...”, making sure the Swift button is selected, and then searching for
UITextFieldDelegate
):public protocol UITextFieldDelegate : NSObjectProtocol {
@available(iOS 2.0, *)
optional func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool // return NO to disallow editing.
@available(iOS 2.0, *)
optional func textFieldDidBeginEditing(_ textField: UITextField) // became first responder
@available(iOS 2.0, *)
optional func textFieldShouldEndEditing(_ textField: UITextField) -> Bool // return YES to allow editing to stop and to resign first responder status. NO to disallow the editing session to end
@available(iOS 2.0, *)
optional func textFieldDidEndEditing(_ textField: UITextField) // may be called if forced even if shouldEndEditing returns NO (e.g. view removed from window) or endEditing:YES called
...
}
Automatically delegating all methods of a java class
Perhaps the dynamic Proxy
of java can help you. It only works if you consequently use interfaces. In this case, I will call the interface MyInterface
and set up a default implementation:
public class MyClass implements MyInterface {
@Override
public void method1() {
System.out.println("foo1");
}
@Override
public void method2() {
System.out.println("foo2");
}
@Override
public void methodN() {
System.out.println("fooN");
}
public static void main(String[] args) {
MyClass wrapped = new MyClass();
wrapped.method1();
wrapped.method2();
MyInterface wrapper = WrapperClass.wrap(wrapped);
wrapper.method1();
wrapper.method2();
}
}
The wrapper class implementation would look like:
public class WrapperClass extends MyClass implements MyInterface, InvocationHandler {
private final MyClass delegate;
public WrapperClass(MyClass delegate) {
this.delegate = delegate;
}
public static MyInterface wrap(MyClass wrapped) {
return (MyInterface) Proxy.newProxyInstance(MyClass.class.getClassLoader(), new Class[] { MyInterface.class }, new WrapperClass(wrapped));
}
//you may skip this definition, it is only for demonstration
public void method1() {
System.out.println("bar");
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
Method m = findMethod(this.getClass(), method);
if (m != null) {
return m.invoke(this, args);
}
m = findMethod(delegate.getClass(), method);
if (m != null) {
return m.invoke(delegate, args);
}
return null;
}
private Method findMethod(Class<?> clazz, Method method) throws Throwable {
try {
return clazz.getDeclaredMethod(method.getName(), method.getParameterTypes());
} catch (NoSuchMethodException e) {
return null;
}
}
}
Note that this class:
- extends
MyClass
, to inherit a default implementation (any other would do) - implements
Invocationhandler
, to allow the proxy to do reflection - optionally implement
MyInterface
(to satisfy the decorator pattern)
This solution allows you to override special methods, but to delegate all others. This will even work with sub classes of Wrapper class.
Note that the method findMethod
does not yet capture the special cases.
Delegate for UITextField not working...Return button not responding
Firstly you should check if textFieldShouldReturn:
is actually being called by adding an NSLog statement or breakpoint at the beginning of the method.
Once that's out of the way, try an manually declare that your view controller conforms to <UITextFieldDelegate>
protocol in your interface file:
@interface YourClass : ... <UITextFieldDelegate>
Also declare a property & outlet for your UITextField
, make the appropriate connections in IB and manually declare self
as the UITextField
delegate with:
self.yourUITextFieldObject.delegate = self;
Once that's done see if your method above is now being called and make sure you return YES.
add event handler an element inside a info window google maps v3
Try the "onclick" attribute for the button.
'<input type="button" id="btnPrueba" value="prueba" onclick="removeMarker()"/>'
Finding nearest point in an efficient way
Use a quad-tree for 2D
http://en.wikipedia.org/wiki/Quadtree
Related Topics
Best Way to Handle Errors from Async Closures in Swift 2
Calculate Time Difference in Swift 4
How to Draw Line Node Keep Same Size in Camera as Measure App in Iphone
Swift Objc_Getassociatedobject Always Nil
How to Check Whether an Object Is Kind of a Dynamic Class Type in Swift
Swift: Hashable Struct with Dictionary Property
Implementing Reconnection with Urlsession Publisher and Combine
Scenekit Won't Scale a Dynamic Body
Safari App Extension Crashes After a Few Seconds for Hello World Project
Updating Fetchedresultscontroller for Predicate Set by Uisearchbar
Swift Get Nsdata of a Video from Photos Library
Prevent Redirect Response with Alamofire in Swift
Curl with Alamofire - Swift - Multipart/Form-Data
Hide Status Bar in Launch Screen