iOS
Tiempo de ejecución en Objective-C
Buscar..
Usando objetos asociados
Los objetos asociados son útiles cuando desea agregar funcionalidad a las clases existentes que requieren un estado de espera.
Por ejemplo, agregando un indicador de actividad a cada UIView:
Implementación de Objective-C
#import <objc/runtime.h>
static char ActivityIndicatorKey;
@implementation UIView (ActivityIndicator)
- (UIActivityIndicatorView *)activityIndicator {
return (UIActivityIndicatorView *)objc_getAssociatedObject(self, &ActivityIndicatorKey);
}
- (void)setActivityIndicator: (UIActivityIndicatorView *)activityIndicator {
objc_setAssociatedObject(self, &ActivityIndicatorKey, activityIndicator, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (void)showActivityIndicator {
UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle: UIActivityIndicatorViewStyleGray];
[self setActivityIndicator:activityIndicator];
activityIndicator.center = self.center;
activityIndicator.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleBottomMargin;
[activityIndicator startAnimating];
[self addSubview: activityIndicator];
}
- (void)hideActivityIndicator {
UIActivityIndicatorView * activityIndicator = [self activityIndicator];
if (activityIndicator != nil) {
[[self activityIndicator] removeFromSuperview];
}
}
@end
También puede acceder al tiempo de ejecución de Objective-C a través de Swift:
código SWIFT
extension UIView {
private struct AssociatedKeys {
static var activityIndicator = "UIView.ActivityIndicatorView"
}
private var activityIndicatorView: UIActivityIndicatorView? {
get {
return objc_getAssociatedObject(self, &AssociatedKeys.activityIndicator) as? UIActivityIndicatorView
}
set (activityIndicatorView) {
objc_setAssociatedObject(self, &AssociatedKeys.activityIndicator, activityIndicatorView, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
}
}
func showActivityIndicator() {
activityIndicatorView = UIActivityIndicatorView(activityIndicatorStyle: .gray)
activityIndicatorView.center = center
activityIndicatorView.autoresizingMask = [.flexibleLeftMargin, .flexibleRightMargin, .flexibleTopMargin, .flexibleBottomMargin]
activityIndicatorView.startAnimating()
addSubview(activityIndicatorView)
}
func hideActivityIndicator() {
activityIndicatorView.removeFromSuperview()
}
}
Modified text is an extract of the original Stack Overflow Documentation
Licenciado bajo CC BY-SA 3.0
No afiliado a Stack Overflow