Swift calls code written by objective-c (color picker kkcolorlistpicker Call)

Source: Internet
Author: User

In the SWIFT project, we can import any frame, code base, etc. written in objective-c. The following is an example of a color selector kkcolorlistpicker written in Swift call Objective-c.


as follows:
implementation steps:1, first in the project to import Kkcolorlistpicker source (the entire folder, on the project has the key "ADD files to XXX"). 2, manually create the bridge header file Bridge.h to contain the OBJECTIVE-C header file you want to reference, as follows:
123 //KKColorsSchemeType.h已经包含在KKColorListViewController.h#import "KKColorListViewController.h"#import "KKColor.h"

3. Select the project and add bridging in the Objective-c bridge.h header settings, Setting, build, Swift compiler-code generation, as shown in:



4, page Use Viewcontroller.swift
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 import UIKitclass ViewController: UIViewController, KKColorListViewControllerDelegate {         //Object-C编写的颜色选择器视图    var viewColor:KKColorListViewController!    override func viewDidLoad() {        super.viewDidLoad()        // Do any additional setup after loading the view, typically from a nib.                //初始化颜色选择面板        //这个颜色选择面板是用OC开发,我们导入头文件后就能在Swift里调用了        viewColor = KKColorListViewController(schemeType:KKColorsSchemeType.Crayola)        //此类实现了颜色选择面板的代理协议,并重载了选择颜色和关闭2个回调函数        viewColor.delegate = self                //创建一个按钮        var button:UIButton = UIButton.buttonWithType(UIButtonType.System) as! UIButton;        //设置按钮位置和大小        button.frame=CGRectMake(10, 50, 100, 30);        //设置按钮文字        button.setTitle("选择背景色", forState:UIControlState.Normal)        button.addTarget(self,action:Selector("selColor:"),forControlEvents:UIControlEvents.TouchUpInside)        self.view.addSubview(button);    }         //点击选择背景色的事件操作    func selColor(sender: AnyObject) {        //显示颜色选择面板        self.presentViewController(viewColor, animated: true, completion:nil)    }        //选择颜色后回调    func colorListController(controller:KKColorListViewController,  didSelectColor color:KKColor)    {        //关闭颜色选择器视图        viewColor.dismissViewControllerAnimated(true, completion:nil)        //设置当前视图的背景颜色为用户选择的颜色        self.view.backgroundColor = color.uiColor()    }    //用户在颜色选择器视图里点击了关闭    func colorListPickerDidComplete(controller:KKColorListViewController)    {        //只需要关闭颜色选择器视图        viewColor.dismissViewControllerAnimated(true, completion:nil)    }    override func didReceiveMemoryWarning() {        super.didReceiveMemoryWarning()        // Dispose of any resources that can be recreated.    }}


SOURCE Download: Test6.zip

Swift calls code written by objective-c (color picker kkcolorlistpicker Call)

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.