Forward iOS Advanced Development ~runtime (IV)

Source: Internet
Author: User
Tags sprintf


Replace OC with C:


#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 =;




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

}


Operating effect:





IOS Advanced Development ~Runtime(i)

http://blog.csdn.net/zfpp25_/article/details/9496705

IOS Advanced Development ~Runtime(ii)

http://blog.csdn.net/zfpp25_/article/details/9497187

IOS Advanced Development ~Runtime(iii)

http://blog.csdn.net/zfpp25_/article/details/9497721

IOS Advanced Development ~Runtime(iv)

http://blog.csdn.net/zfpp25_/article/details/9498233

Forward iOS Advanced Development ~runtime (IV)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.