iOS高级编程 runtime

下载DEMO:https://github.com/sleepsheep/RunTimeDemo04
代码示例:

#import <objc/runtime.h>
#import <objc/message.h>
#import <stdio.h>

extern int MyUIApplicationMain(int argc, char *argv[], void *principalClassName, void *delegateClassName);

struct MyRect {
    float x;
    float y;
    float width;
    float height;
};
typedef struct MyRect MyRect;

void *navController;
static int numberOfRows = 100;

int tableView_numberOfRowInSection(void *receiver, struct objc_selector *selector, void *tblView, int section) {
    return numberOfRows;
}

void *tableView_cellForRowAtIndexPath(void *receiver, struct objc_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, struct objc_selector *seletor, 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_numberOfRowInSection, nil);
    class_addMethod(DataSource, sel_registerName("tableView:cellForRowAtIndexPath:"), (void (*))tableView_cellForRowAtIndexPath, nil);

    objc_registerClassPair(DataSource);
    return class_createInstance(DataSource, 0);
}

void *createDelegate() {
    Class superClass = (Class)object_getClass(@"NSObject");
    Class DataSource = objc_allocateClassPair(superClass, "Delegate", 0);
    class_addMethod(DataSource, sel_registerName("tableView:didSelectRowAtIndexPath:"), (void (*))tableView_didSelectRowAtIndexPath, nil);

    objc_registerClassPair(DataSource);
    return class_createInstance(DataSource, 0);
}

//自定义主函数
void applicationdidFinishLaunching(void *receiver, struct objc_selector *selector, void *application) {
    Class windowClass = (Class)object_getClass(@"UIWindow");
    void *windowInstance = class_createInstance(windowClass, 0);

    objc_msgSend(windowClass, sel_registerName("initWithFrame:"), (MyRect){0, 0, 320, 480});

    //make key and visiable
    objc_msgSend(windowInstance, sel_registerName("makeKeyAndVisible"));

    //创建表
    Class TableViewController = (Class)object_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)object_getClass(@"UINavigationController");
    navController = class_createInstance(NavController, 0);
    objc_msgSend(navController, sel_registerName("initWithRootViewCOntroller:"), tableViewController);
    void *view = objc_msgSend(navController, sel_registerName("view"));

    //add TableView to window
    objc_msgSend(windowInstance, sel_registerName("addSubview:"), view);

}

//create an class named "AppDelegate", and return its name as an instance of class NSString
void *createAppDelegate() {
    Class mySubclass = objc_allocateClassPair((Class)object_getClass(@"NSObject"), "AppDelegate", 0);
    struct objc_selector *selName = sel_registerName("application:didFinishLaunchingWithOptions:");
    class_addMethod(mySubclass, selName, (void (*))applicationdidFinishLaunching, nil);
    objc_registerClassPair(mySubclass);
    return objc_msgSend(object_getClass(@"NNString"), sel_registerName("stringWithUTF8String:"), "AppDelegate");
}

int main(int argc, char *argv[]) {
    return MyUIApplicationMain(argc, argv, 0, createAppDelegate());

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值