OS高级开发~Runtime(四)
2016-05-12 14:04
267 查看
iOS高级开发~Runtime(一)
iOS高级开发~Runtime(二)
iOS高级开发~Runtime(三)
iOS高级开发~Runtime(四)
用C代替OC:
#import <objc/runtime.h>
#import <objc/message.h>
#import <stdio.h>
extern int UIApplicationMain (int argc,char *argv[],void *principalClassName,void *delegateClassName);
struct Rect {
float x;
float y;
float width;
float height;
};
typedef struct Rect Rect;
void *navController;
static int numberOfRows =100;
int tableView_numberOfRowsInSection(void *receiver,structobjc_selector *selector,void *tblview,int
section) {
returnnumberOfRows;
}
void *tableView_cellForRowAtIndexPath(void *receiver,structobjc_selector *selector,void *tblview,void
*indexPath) {
Class TableViewCell = (Class)objc_getClass("UITableViewCell");
void *cell = class_createInstance(TableViewCell,0);
objc_msgSend(cell,
sel_registerName("init"));
char buffer[7];
int row = (int)
objc_msgSend(indexPath, sel_registerName("row"));
sprintf (buffer, "Row %d", row);
void *label =objc_msgSend(objc_getClass("NSString"),sel_registerName("stringWithUTF8String:"),buffer);
objc_msgSend(cell,
sel_registerName("setText:"),label);
return cell;
}
void tableView_didSelectRowAtIndexPath(void *receiver,structobjc_selector *selector,void *tblview,void
*indexPath) {
Class ViewController = (Class)objc_getClass("UIViewController");
void * vc = class_createInstance(ViewController,0);
objc_msgSend(vc,
sel_registerName("init"));
char buffer[8];
int row = (int)
objc_msgSend(indexPath, sel_registerName("row"));
sprintf (buffer, "Item %d", row);
void *label =objc_msgSend(objc_getClass("NSString"),sel_registerName("stringWithUTF8String:"),buffer);
objc_msgSend(vc,
sel_registerName("setTitle:"),label);
objc_msgSend(navController,sel_registerName("pushViewController:animated:"),vc,1);
}
void *createDataSource() {
Class superclass = (Class)objc_getClass("NSObject");
Class DataSource = objc_allocateClassPair(superclass,"DataSource",0);
class_addMethod(DataSource,sel_registerName("tableView:numberOfRowsInSection:"), (void(*))tableView_numberOfRowsInSection,nil);
class_addMethod(DataSource,sel_registerName("tableView:cellForRowAtIndexPath:"), (void(*))tableView_cellForRowAtIndexPath,nil);
objc_registerClassPair(DataSource);
returnclass_createInstance(DataSource,0);
}
void * createDelegate() {
Class superclass = (Class)objc_getClass("NSObject");
Class DataSource = objc_allocateClassPair(superclass,"Delegate",0);
class_addMethod(DataSource,sel_registerName("tableView:didSelectRowAtIndexPath:"), (void(*))tableView_didSelectRowAtIndexPath,nil);
objc_registerClassPair(DataSource);
returnclass_createInstance(DataSource,0);
}
void applicationdidFinishLaunching(void *receiver,structobjc_selector *selector,void *application) {
Class windowClass = (Class)objc_getClass("UIWindow");
void * windowInstance =
class_createInstance(windowClass, 0);
objc_msgSend(windowInstance,
sel_registerName("initWithFrame:"),(Rect){0,0,320,480});
//Make Key and Visiable
objc_msgSend(windowInstance,sel_registerName("makeKeyAndVisible"));
//Create Table View
Class TableViewController = (Class)objc_getClass("UITableViewController");
void *tableViewController =
class_createInstance(TableViewController, 0);
objc_msgSend(tableViewController,
sel_registerName("init"));
void *tableView = objc_msgSend(tableViewController,sel_registerName("tableView"));
objc_msgSend(tableView,
sel_registerName("setDataSource:"),createDataSource());
objc_msgSend(tableView,
sel_registerName("setDelegate:"),createDelegate());
Class NavController = (Class)objc_getClass("UINavigationController");
navController = class_createInstance(NavController,0);
objc_msgSend(navController,sel_registerName("initWithRootViewController:"),tableViewController);
void *view =objc_msgSend(navController,sel_registerName("view"));
//Add Table View To Window
objc_msgSend(windowInstance,
sel_registerName("addSubview:"),view);
}
//Create an class named "AppDelegate", and return it's name as an instance of class NSString
void *createAppDelegate() {
Class mySubclass =
objc_allocateClassPair((Class)objc_getClass("NSObject"),"AppDelegate",0);
structobjc_selector *selName =sel_registerName("application:didFinishLaunchingWithOptions:");
class_addMethod(mySubclass, selName, (void(*))applicationdidFinishLaunching,nil);
objc_registerClassPair(mySubclass);
returnobjc_msgSend(objc_getClass("NSString"),sel_registerName("stringWithUTF8String:"),"AppDelegate");
}
int main(int argc,
char *argv[]) {
returnUIApplicationMain(argc, argv,0,createAppDelegate());
}
运行效果:
iOS高级开发~Runtime(二)
iOS高级开发~Runtime(三)
iOS高级开发~Runtime(四)
用C代替OC:
#import <objc/runtime.h>
#import <objc/message.h>
#import <stdio.h>
extern int UIApplicationMain (int argc,char *argv[],void *principalClassName,void *delegateClassName);
struct Rect {
float x;
float y;
float width;
float height;
};
typedef struct Rect Rect;
void *navController;
static int numberOfRows =100;
int tableView_numberOfRowsInSection(void *receiver,structobjc_selector *selector,void *tblview,int
section) {
returnnumberOfRows;
}
void *tableView_cellForRowAtIndexPath(void *receiver,structobjc_selector *selector,void *tblview,void
*indexPath) {
Class TableViewCell = (Class)objc_getClass("UITableViewCell");
void *cell = class_createInstance(TableViewCell,0);
objc_msgSend(cell,
sel_registerName("init"));
char buffer[7];
int row = (int)
objc_msgSend(indexPath, sel_registerName("row"));
sprintf (buffer, "Row %d", row);
void *label =objc_msgSend(objc_getClass("NSString"),sel_registerName("stringWithUTF8String:"),buffer);
objc_msgSend(cell,
sel_registerName("setText:"),label);
return cell;
}
void tableView_didSelectRowAtIndexPath(void *receiver,structobjc_selector *selector,void *tblview,void
*indexPath) {
Class ViewController = (Class)objc_getClass("UIViewController");
void * vc = class_createInstance(ViewController,0);
objc_msgSend(vc,
sel_registerName("init"));
char buffer[8];
int row = (int)
objc_msgSend(indexPath, sel_registerName("row"));
sprintf (buffer, "Item %d", row);
void *label =objc_msgSend(objc_getClass("NSString"),sel_registerName("stringWithUTF8String:"),buffer);
objc_msgSend(vc,
sel_registerName("setTitle:"),label);
objc_msgSend(navController,sel_registerName("pushViewController:animated:"),vc,1);
}
void *createDataSource() {
Class superclass = (Class)objc_getClass("NSObject");
Class DataSource = objc_allocateClassPair(superclass,"DataSource",0);
class_addMethod(DataSource,sel_registerName("tableView:numberOfRowsInSection:"), (void(*))tableView_numberOfRowsInSection,nil);
class_addMethod(DataSource,sel_registerName("tableView:cellForRowAtIndexPath:"), (void(*))tableView_cellForRowAtIndexPath,nil);
objc_registerClassPair(DataSource);
returnclass_createInstance(DataSource,0);
}
void * createDelegate() {
Class superclass = (Class)objc_getClass("NSObject");
Class DataSource = objc_allocateClassPair(superclass,"Delegate",0);
class_addMethod(DataSource,sel_registerName("tableView:didSelectRowAtIndexPath:"), (void(*))tableView_didSelectRowAtIndexPath,nil);
objc_registerClassPair(DataSource);
returnclass_createInstance(DataSource,0);
}
void applicationdidFinishLaunching(void *receiver,structobjc_selector *selector,void *application) {
Class windowClass = (Class)objc_getClass("UIWindow");
void * windowInstance =
class_createInstance(windowClass, 0);
objc_msgSend(windowInstance,
sel_registerName("initWithFrame:"),(Rect){0,0,320,480});
//Make Key and Visiable
objc_msgSend(windowInstance,sel_registerName("makeKeyAndVisible"));
//Create Table View
Class TableViewController = (Class)objc_getClass("UITableViewController");
void *tableViewController =
class_createInstance(TableViewController, 0);
objc_msgSend(tableViewController,
sel_registerName("init"));
void *tableView = objc_msgSend(tableViewController,sel_registerName("tableView"));
objc_msgSend(tableView,
sel_registerName("setDataSource:"),createDataSource());
objc_msgSend(tableView,
sel_registerName("setDelegate:"),createDelegate());
Class NavController = (Class)objc_getClass("UINavigationController");
navController = class_createInstance(NavController,0);
objc_msgSend(navController,sel_registerName("initWithRootViewController:"),tableViewController);
void *view =objc_msgSend(navController,sel_registerName("view"));
//Add Table View To Window
objc_msgSend(windowInstance,
sel_registerName("addSubview:"),view);
}
//Create an class named "AppDelegate", and return it's name as an instance of class NSString
void *createAppDelegate() {
Class mySubclass =
objc_allocateClassPair((Class)objc_getClass("NSObject"),"AppDelegate",0);
structobjc_selector *selName =sel_registerName("application:didFinishLaunchingWithOptions:");
class_addMethod(mySubclass, selName, (void(*))applicationdidFinishLaunching,nil);
objc_registerClassPair(mySubclass);
returnobjc_msgSend(objc_getClass("NSString"),sel_registerName("stringWithUTF8String:"),"AppDelegate");
}
int main(int argc,
char *argv[]) {
returnUIApplicationMain(argc, argv,0,createAppDelegate());
}
运行效果:
相关文章推荐
- Linux流程解析二
- material-code-input 一个类似于 支付宝 一次输入一个字符的控件
- Monkey的基本命令
- 使用DividerGridItemDecoration遇到的坑
- iOS URLEcode URLDecode
- capturing self strongly in this block is likely to lead to a retain cycle
- Oracle 11g export and import
- RGMII布线指导 RGMII Layout Guide
- 移动web中设计尺寸与制作尺寸详解
- Php扩展--protocolbuffers消息打包
- pugixml使用总结
- TortoiseSVN文件夹及文件图标不显示解决方法
- 计算机图形学(二)输出图元_10_多边形填充区_3_内-外测试
- Java LinkedList工作原理及实现(一)
- Android使用DialogFragment创建对话框
- [hadoop]HDFS(Hadoop分布式文件系统)(一)
- 给app设置LaunchImage
- C++用指针创建堆中空间
- HttpServlet中的service和doGet的区别
- IOS高级开发~Runtime(三)