Runtime(四)
2016-04-12 22:00
483 查看
用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
d426
*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());
}
运行效果:
#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
d426
*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());
}
运行效果:
相关文章推荐
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 不可修补的 iOS 漏洞可能导致 iPhone 4s 到 iPhone X 永久越狱
- iOS 12.4 系统遭黑客破解,漏洞危及数百万用户
- 每日安全资讯:NSO,一家专业入侵 iPhone 的神秘公司
- [转][源代码]Comex公布JailbreakMe 3.0源代码
- Java Runtime Environment 5.0 Update 12 下载
- php set_magic_quotes_runtime() 函数过时解决方法
- Asp.Net 程序错误Runtime Error原因与解决
- 讲解iOS开发中基本的定位功能实现
- iOS中定位当前位置坐标及转换为火星坐标的方法
- js判断客户端是iOS还是Android等移动终端的方法
- iOS应用中UISearchDisplayController搜索效果的用法
- IOS开发环境windows化攻略
- iOS应用中UITableView左滑自定义选项及批量删除的实现
- 浅析iOS应用开发中线程间的通信与线程安全问题
- 检测iOS设备是否越狱的方法
- System 类 和 Runtime 类的常用用法介绍
- .net平台推送ios消息的实现方法