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());

}

运行效果:

时间: 2024-11-06 12:16:55

IOS高级开发~Runtime(四)的相关文章

IOS高级开发 runtime(1)

一. 简介 IOS 开发中灵活使用runtime 会提高我们的程序性能和开发速度.要想使用runtime,首先要引入系统的头文件. #import <objc/runtime.h> 当我们查看runtime.h的时候,我们会发现,其实runtime是很有条理的 const char *object_getClassName(id obj) //获取对象的类 Ivar *class_copyIvarList(Class cls, unsigned int *outCount) //获取类的变量列

(转发)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;

(转发)IOS高级开发~Runtime(二)

一些公用类: @interface ClassCustomClass :NSObject{ NSString *varTest1; NSString *varTest2; NSString *varTest3; } @property (nonatomic,assign)NSString *varTest1; @property (nonatomic,assign)NSString *varTest2; @property (nonatomic,assign)NSString *varTest3

(转发)IOS高级开发~Runtime(三)

11.系统类的方法实现部分替换 - (void) methodExchange { Method m1 = class_getInstanceMethod([NSStringclass],@selector(lowercaseString)); Method m2 = class_getInstanceMethod([NSStringclass],@selector(uppercaseString)); method_exchangeImplementations(m1, m2); NSLog(

(转发)IOS高级开发~Runtime(一)

IOS高级开发-Runtime(一) IOS高级开发-Runtime(二) IOS高级开发-Runtime(三) IOS高级开发-Runtime(四) 一些公用类: @interface CustomClass : NSObject - (void) fun1; @end @implementation CustomClass - (void) fun1 { NSLog(@"fun1"); } @end @interface TestClass : NSObject @end @imp

iOS高级开发——CollectionView的cell中按钮的点击实现

在我刚初学iOS的时候,我就问一些大神,iOS开发中最难的哪些部分.有些人就说是自定义控件.UI和交互设计.那个时候我将信将疑,随着自己开发的深入,自己的确是深有体会.开发一款App产品,很大一部分时间是在和UI打交道.因为开发中很多功能是直接封装好的或者有现成模板可以用的,唯有UI是根据不同的App千变万化的.所以今天我们继续来研究iOS中比较高级的控件--UICollectionView,来实现cell中按钮的点击操作.该demo我已经提交到: https://github.com/chen

IOS高级开发~Runtime(一)

一些公用类: @interface CustomClass : NSObject - (void) fun1; @end @implementation CustomClass - (void) fun1 { NSLog(@"fun1"); } @end @interface TestClass : NSObject @end @implementation TestClass @end 别忘记引入库:#include<objc/runtime.h> 1.对象拷贝:id o

IOS高级开发~Runtime(二)

一些公用类: @interface ClassCustomClass :NSObject{ NSString *varTest1; NSString *varTest2; NSString *varTest3; } @property (nonatomic,assign)NSString *varTest1; @property (nonatomic,assign)NSString *varTest2; @property (nonatomic,assign)NSString *varTest3

ios高级开发,runtime(一)

创建公共类: @interface CustomClass : NSObject - (void) fun1; @end @implementation CustomClass -(void)fun1 { NSLog(@"fun1"); } @end @interface TestClass : NSObject @end @implementation TestClass @end 1.对象拷贝:id object_copy(id obj, size_t size) CustomCl