Swift Foundation calls OC language files using steps

Source: Internet
Author: User

In the swift language, there are many encapsulation classes that do not, and if needed, need to bridge the classes in the OC language, then you need to use the bridge header file, which is the step to use:

Create a swift project demo, and then build a new OC language file

Create a Bgimgview class, select the corresponding Object-c language, click the Next button

A prompt will pop up asking if you need to configure a bridge header file, click the Create button (the third one)


A bridge header file is automatically generated at this time

Now start adding the relevant code in the OC file that you need to use

. h file

{
ID _targett;
SEL _selt;
}

Number of rows without parameters
-(void) Testt;
Parameters to return
-(NSString *) nameStr;
Add a Click event method to mimic the click of a button
-(void) AddTarget: (ID) targettt withselector: (SEL) SELECTT;

. m file

Number of rows without parameters
-(void) TESTT
{
NSLog (@ "called the Testt Method .... ");
}
Parameters to return
-(NSString *) nameStr
{
Return @ "Study hard, day up";
}
Add a Click event method to mimic the click of a button
-(void) AddTarget: (ID) targettt withselector: (SEL) SELECTT
{
_targett = TARGETTT;
_selt = SELECTT;

Whether to interact
self.userinteractionenabled = YES;
}
-(void) touchesended: (Nsset<uitouch *> *) touches withevent: (Uievent *) event
{
if (_targett)
{
[_targett Performselector:_selt withobject:self];
}
}

After writing the required code, you need to add a header file in the bridge file, only the OC header files that need to be used, do not need to write all the OC files


And then in. Used in Swift files,

Create a Bgimgview object
Let BGIMGV = Bgimgview.init (Frame:cgrectmake (10, 100, 200, 100));
Bgimgv.image = Uiimage.init (named: "22.jpg");

Calling methods
Bgimgv.testt ();

Return Parameter method
Let nameStr = Bgimgv.namestr ();
NSLog ("Output ... %@ ", NAMESTR);

Add a Click event
Bgimgv.addtarget (Self, Withselector: #selector (Viewcontroller.imgclick));

Self.view.addSubview (BGIMGV);

}

Func Imgclick (Imgv:bgimgview) {
NSLog ("Click on the picture ..... ");
}

Final output effect:




Swift Foundation calls OC language files using steps

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.