Created
November 7, 2014 08:58
-
-
Save cyndibaby905/b70323c64424cd4c8fa9 to your computer and use it in GitHub Desktop.
Just a demo to show how to use runtime functions to implement UIAlertController in iOS 7 or before.
This file contains 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 characters
// | |
// AppDelegate.m | |
// RuntimeDemo | |
// | |
// Created by hangchen on 11/7/14. | |
// Copyright (c) 2014 hangchen. All rights reserved. | |
// | |
#import "AppDelegate.h" | |
#import <objc/runtime.h> | |
@interface CHAlertViewController : UIViewController | |
/* | |
* Add the same method declarations in UIAlertController here | |
*/ | |
@end | |
@implementation CHAlertViewController | |
/* | |
* Add the same method implementations in UIAlertController here | |
*/ | |
@end | |
static void CHRegisterClasses() { | |
NSDictionary *map = @{ | |
@"UIAlertController": CHAlertViewController.class | |
}; | |
[map enumerateKeysAndObjectsUsingBlock:^(NSString* UIClassName, id CHClass, BOOL *stop) { | |
Class UIClass = NSClassFromString(UIClassName); | |
if (!UIClass) { | |
if ((UIClass = objc_allocateClassPair(CHClass, UIClassName.UTF8String, 0))) { | |
objc_registerClassPair(UIClass); | |
} | |
} | |
}]; | |
} | |
@implementation AppDelegate | |
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { | |
CHRegisterClasses(); | |
//Notice: We can only initialize a UIAlertController instance in this way, you can not use UIAlertController to initialize instances directly. | |
UIAlertController *vc = [[NSClassFromString(@"UIAlertController") alloc] init]; | |
NSLog(@"%@",vc); | |
return YES; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment