iOS高级编程 runtime
代码示例:
1 | #import <objc/runtime.h> |
2 | #import <objc/message.h> |
3 | #import <stdio.h> |
4 | |
5 | extern int MyUIApplicationMain(int argc, char *argv[], void *principalClassName, void *delegateClassName); |
6 | |
7 | struct MyRect { |
8 | float x; |
9 | float y; |
10 | float width; |
11 | float height; |
12 | }; |
13 | typedef struct MyRect MyRect; |
14 | |
15 | void *navController; |
16 | static int numberOfRows = 100; |
17 | |
18 | int tableView_numberOfRowInSection(void *receiver, struct objc_selector *selector, void *tblView, int section) { |
19 | return numberOfRows; |
20 | } |
21 | |
22 | void *tableView_cellForRowAtIndexPath(void *receiver, struct objc_selector *selector, void *tblView, void *indexPath) { |
23 | Class TableViewCell = (Class)objc_getClass(“UITableViewCell”); |
24 | void *cell = class_createInstance(TableViewCell, 0); |
25 | objc_msgSend(cell, sel_registerName(“init”)); |
26 | char buffer[7]; |
27 | |
28 | int row = (int)objc_msgSend(indexPath, sel_registerName(“row”)); |
29 | sprintf(buffer, “Row %d”, row); |
30 | void *label = objc_msgSend(objc_getClass(“NSString”), sel_registerName(“stringWithUTF8String:”), buffer); |
31 | objc_msgSend(cell, sel_registerName(“setText:”), label); |
32 | |
33 | return cell; |
34 | } |
35 | |
36 | void tableView_didSelectRowAtIndexPath(void *receiver, struct objc_selector *seletor, void *tblView, void *indexPath) { |
37 | Class ViewController = (Class)objc_getClass(“UIViewController”); |
38 | void *vc = class_createInstance(ViewController, 0); |
39 | objc_msgSend(vc, sel_registerName(“init”)); |
40 | char buffer[8]; |
41 | int row = (int)objc_msgSend(indexPath, sel_registerName(“row”)); |
42 | sprintf(buffer, “Item %d”, row); |
43 | void *label = objc_msgSend(objc_getClass(“NSString”), sel_registerName(“stringWithUTF8String”), buffer); |
44 | objc_msgSend(vc, sel_registerName(“setTitle”), label); |
45 | |
46 | objc_msgSend(navController, sel_registerName(“pushViewController:animated:”), vc, 1); |
47 | } |
48 | |
49 | void *createDataSource() { |
50 | Class superclass = (Class)objc_getClass(“NSObject”); |
51 | Class DataSource = objc_allocateClassPair(superclass, “DataSource”, 0); |
52 | class_addMethod(DataSource, sel_registerName(“tableView:numberOfRowsInSection:”), (void (*))tableView_numberOfRowInSection, nil); |
53 | class_addMethod(DataSource, sel_registerName(“tableView:cellForRowAtIndexPath:”), (void (*))tableView_cellForRowAtIndexPath, nil); |
54 | |
55 | objc_registerClassPair(DataSource); |
56 | return class_createInstance(DataSource, 0); |
57 | } |
58 | |
59 | void *createDelegate() { |
60 | Class superClass = (Class)object_getClass(@”NSObject”); |
61 | Class DataSource = objc_allocateClassPair(superClass, “Delegate”, 0); |
62 | class_addMethod(DataSource, sel_registerName(“tableView:didSelectRowAtIndexPath:”), (void (*))tableView_didSelectRowAtIndexPath, nil); |
63 | |
64 | objc_registerClassPair(DataSource); |
65 | return class_createInstance(DataSource, 0); |
66 | } |
67 | |
68 | //自定义主函数 |
69 | void applicationdidFinishLaunching(void *receiver, struct objc_selector *selector, void *application) { |
70 | Class windowClass = (Class)object_getClass(@”UIWindow”); |
71 | void *windowInstance = class_createInstance(windowClass, 0); |
72 | |
73 | objc_msgSend(windowClass, sel_registerName(“initWithFrame:”), (MyRect){0, 0, 320, 480}); |
74 | |
75 | //make key and visiable |
76 | objc_msgSend(windowInstance, sel_registerName(“makeKeyAndVisible”)); |
77 | |
78 | //创建表 |
79 | Class TableViewController = (Class)object_getClass(@”UITableViewController”); |
80 | void *tableViewController = class_createInstance(TableViewController, 0); |
81 | objc_msgSend(TableViewController, sel_registerName(“init”)); |
82 | void *tableView = objc_msgSend(TableViewController, sel_registerName(“tableView”)); |
83 | objc_msgSend(tableView, sel_registerName(“setDataSource”), createDataSource()); |
84 | objc_msgSend(tableView, sel_registerName(“setDelegate”), createDelegate()); |
85 | |
86 | Class NavController = (Class)object_getClass(@”UINavigationController”); |
87 | navController = class_createInstance(NavController, 0); |
88 | objc_msgSend(navController, sel_registerName(“initWithRootViewCOntroller:”), tableViewController); |
89 | void *view = objc_msgSend(navController, sel_registerName(“view”)); |
90 | |
91 | //add TableView to window |
92 | objc_msgSend(windowInstance, sel_registerName(“addSubview:”), view); |
93 | |
94 | } |
95 | |
96 | //create an class named “AppDelegate”, and return its name as an instance of class NSString |
97 | void *createAppDelegate() { |
98 | Class mySubclass = objc_allocateClassPair((Class)object_getClass(@”NSObject”), “AppDelegate”, 0); |
99 | struct objc_selector *selName = sel_registerName(“application:didFinishLaunchingWithOptions:”); |
100 | class_addMethod(mySubclass, selName, (void (*))applicationdidFinishLaunching, nil); |
101 | objc_registerClassPair(mySubclass); |
102 | return objc_msgSend(object_getClass(@”NNString”), sel_registerName(“stringWithUTF8String:”), “AppDelegate”); |
103 | } |
104 | |
105 | int main(int argc, char *argv[]) { |
106 | return MyUIApplicationMain(argc, argv, 0, createAppDelegate()); |