-
-
Save eytanbiala/5bf68aef5ed1f14e4750 to your computer and use it in GitHub Desktop.
Revisions
-
Rudy Jahchan revised this gist
Feb 20, 2013 . 1 changed file with 24 additions and 17 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,18 +1,25 @@ @implementation UIViewController (TourGuide) + (void)load { static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ SEL viewWillAppearSelector = @selector(viewWillAppear:); SEL tourGuideWillAppearSelector = @selector(tourGuideWillAppear:); Method originalMethod = class_getInstanceMethod(self, viewWillAppearSelector); Method newMethod = class_getInstanceMethod(self, tourGuideWillAppearSelector); BOOL methodAdded = class_addMethod([self class], viewWillAppearSelector, method_getImplementation(newMethod), method_getTypeEncoding(newMethod)); if (methodAdded) { class_replaceMethod([self class], tourGuideWillAppearSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod)); } else { method_exchangeImplementations(originalMethod, newMethod); } }); } -
Rudy Jahchan revised this gist
Feb 20, 2013 . 2 changed files with 10 additions and 8 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,16 +1,16 @@ SEL firstMethodSelector = @selector(firstMethod); SEL secondMethodSelector = @selector(secondMethod); Method firstMethod = class_getInstanceMethod(self, firstMethodSelector); Method secondMethod = class_getInstanceMethod(self, secondMethodSelector); BOOL methodAdded = class_addMethod([self class], firstMethodSelector, method_getImplementation(secondMethod), method_getTypeEncoding(secondMethod)); if (methodAdded) { class_replaceMethod([self class], secondMethodSelector, method_getImplementation(firstMethod), method_getTypeEncoding(firstMethod)); } else { This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,14 +1,16 @@ SEL viewWillAppearSelector = @selector(viewWillAppear:); SEL tourGuideWillAppearSelector = @selector(tourGuideWillAppear:); Method originalMethod = class_getInstanceMethod(self, viewWillAppearSelector); Method newMethod = class_getInstanceMethod(self, tourGuideWillAppearSelector); BOOL methodAdded = class_addMethod([self class], viewWillAppearSelector, method_getImplementation(newMethod), method_getTypeEncoding(newMethod)); if (methodAdded) { class_replaceMethod([self class], tourGuideWillAppearSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod)); } else { -
Rudy Jahchan revised this gist
Feb 20, 2013 . 2 changed files with 27 additions and 9 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,16 +1,18 @@ SEL firstMethodSelector = @selector(firstMethod) SEL secondMethodSelector = @selector(secondMethod) Method firstMethod = class_getInstanceMethod(self, firstMethodSelector); Method secondMethod = class_getInstanceMethod(self, secondMethodSelector); BOOL methodAdded = class_addMethod([self class], firstMethod, method_getImplementation(secondMethod), method_getTypeEncoding(secondMethod)); if (methodAdded) { class_replaceMethod([self class], secondMethod, method_getImplementation(firstMethod), method_getTypeEncoding(firstMethod)); } else { method_exchangeImplementations(firstMethod, secondMethod); } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,16 @@ Method originalMethod = class_getInstanceMethod(self, @selector(viewWillAppear:)); Method newMethod = class_getInstanceMethod(self, @selector(tourGuideWillAppear:)); BOOL methodAdded = class_addMethod([self class], originalSelector, method_getImplementation(newMethod), method_getTypeEncoding(newMethod)); if (methodAdded) { class_replaceMethod([self class], newSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod)); } else { method_exchangeImplementations(originalMethod, newMethod); } -
Rudy Jahchan revised this gist
Feb 20, 2013 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,5 +1,5 @@ Method originalMethod = class_getInstanceMethod(self, @selector(viewWillAppear:)); Method newMethod = class_getInstanceMethod(self, @selector(tourGuideWillAppear:)); BOOL methodAdded = class_addMethod([self class], originalSelector, -
Rudy Jahchan revised this gist
Feb 20, 2013 . 1 changed file with 18 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,18 @@ #import "UIViewController_TourGuide.h" @implementation UIViewController (TourGuide) //... - (void) tourGuideWillAppear:(BOOL)animated { // call the old implementation, now under the new message signature [self tourGuideWillAppear:animated]; if (![self isTourRan]) { [self displayTour]; } } //... @end -
Rudy Jahchan revised this gist
Feb 20, 2013 . 1 changed file with 16 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,16 @@ Method originalMethod = class_getInstanceMethod(self, originalSelector); Method newMethod = class_getInstanceMethod(self, newSelector); BOOL methodAdded = class_addMethod([self class], originalSelector, method_getImplementation(newMethod), method_getTypeEncoding(newMethod)); if (methodAdded) { class_replaceMethod([self class], newSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod)); } else { method_exchangeImplementations(originalMethod, newMethod); } -
Rudy Jahchan created this gist
Mar 25, 2012 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,8 @@ #import <Foundation/Foundation.h> @interface NSObject (Swizzle) + (void)swizzleInstanceSelector:(SEL)originalSelector withNewSelector:(SEL)newSelector; @end This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,27 @@ #import "NSObject+Swizzle.h" #import <objc/runtime.h> @implementation NSObject (Swizzle) + (void) swizzleInstanceSelector:(SEL)originalSelector withNewSelector:(SEL)newSelector { Method originalMethod = class_getInstanceMethod(self, originalSelector); Method newMethod = class_getInstanceMethod(self, newSelector); BOOL methodAdded = class_addMethod([self class], originalSelector, method_getImplementation(newMethod), method_getTypeEncoding(newMethod)); if (methodAdded) { class_replaceMethod([self class], newSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod)); } else { method_exchangeImplementations(originalMethod, newMethod); } } @end This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,7 @@ #import <UIKit/UIKit.h> @interface UIViewController (TourGuide) @property (nonatomic, retain) NSArray* tourSteps; @end This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,42 @@ #import "UIViewController_TourGuide.h" #import "NSObject_Swizzle.h" static char tourStepsKey; @implementation UIViewController (TourGuide) + (void)load { static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ [self swizzleInstanceSelector:@selector(viewWillAppear:) withNewSelector:@selector(tourGuideWillAppear:)]; }); } - (NSArray*) getTourSteps { return objc_getAssociatedObject(self, &tourStepsKey); } - (void) setTourSteps:(NSArray*)tourSteps { objc_setAssociatedObject(self, &tourStepsKey, tourSteps, OBJC_ASSOCIATION_RETAIN_NONATOMIC); } - (void) displayTour { // run the tour, and save the fact it was run } - (BOOL) isTourRan { // return TRUE if tour has been run } - (void) tourGuideWillAppear:(BOOL)animated { // call the old implementation, now under the new message signature [self tourGuideWillAppear:animated]; if (![self isTourRan]) { [self displayTour]; } } @end