Skip to content
This repository has been archived by the owner on Oct 4, 2019. It is now read-only.

Examples of usage

hfossli edited this page Oct 27, 2014 · 4 revisions

UIView

Relayout and redraw the view

- (void)updateOnClassInjection {    
   [self setNeedsLayout];  
   [self setNeedsDisplay];
}

UIViewControllers view re-creation

Recreate the views

- (void)updateOnClassInjection {    
   if ([self isViewLoaded]) {
      UIView * viewSuperView = view.superview;
      [self.view removeFromSuperview];
      self.view = [self recreateView]; // <-- Custom metod of view recreation
      [viewSuperView addSubview:[self view]];
   }
}

Notifications

You can observe for changes in any class by saying

#if TARGET_IPHONE_SIMULATOR

- (void)someMethod {
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(classInjectionNotification:)
                                                 name:@"SFInjectionsClassInjectedNotification"
                                               object:nil];
}

- (void)classInjectionNotification:(NSNotification *)notification {
    NSLog(@"Class '%@' was injected", NSStringFromClass((Class)notification.object));
}

#endif

Or by specifying a specific class

#if TARGET_IPHONE_SIMULATOR

- (void)someMethod {
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(myViewControllerClassInjectionNotification:)
                                                 name:@"SFInjectionsClassInjectedNotification"
                                               object:(id)[MyViewController class]];
}

- (void)myViewControllerClassInjectionNotification:(NSNotification *)notification {
    NSLog(@"MyViewController got injected");
}

#endif

UIViewControllers locations (Debugging purposes)

// Helpful, when you are new to the project and 
// have lost in big about of view controllers
// And you don't know which controller are you currently see
// This method was found in base (__PROJECTPREFIX__)ViewController
- (void)updateOnClassInjection {
   if ([self isViewLoaded]) {

      // Adding to each view controller view
      // Label with it's class name
      [[[self view] viewWithTag:1000] removeFromSuperview];

      UILabel * label = [[UILabel alloc] init];
      label.text = NSStringFromClass([self class]);
      [label sizeToFit];
      label.center = CGPointMake(self.view.width / 2, 0);
      label.top = 20;
      label.tag = 1000;
      [[self view] addSubview:label];

      [label setBackgroundColor:[UIColor greenColor]];
      
   }
}