ios - 如何从objective-c调用ViewController.swift类

时间:2014-11-28 02:14:10

标签: ios objective-c swift

ViewController.swift

    @IBOutlet weak var showLabel: UILabel!

    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    /* - commented out
    override init(nibName nibNameOrNil: String!, bundle nibBundleOrNil: NSBundle!) {
       super.init(nibName: nil, bundle: nil)
    }
    */
    .
    .
    .

ReadData.m

    #import <DemoApp-Swift.h>

    @implementation ReadData

    -(void) getData {

        ViewController *controller = [[ViewController alloc] init];
        controller.showLabel.text = @“xxxx”;
    .
    .
    .
    NSString* updateText = @"xxxxx";
    [controller.showLabel performSelectorOnMainThread:@selector(setText:) withObject:updateText waitUntilDone:NO];

    }
    .
    .
    .

运行时,发生以下运行时致命错误。

../DemoApp/ViewController.swift: 20: 7: fatal error: use of unimplemented initializer 'init()' for class 'DemoApp.ViewController'

我想更新我在swift中初始化的objective-c中的标签值。 有任何想法......请帮我解决一下......

谢谢。

1 个答案:

答案 0 :(得分:0)

if you are working in swift than use

class ReadData: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
  fun getData{

   let controller: ViewController = ViewController()
   controller.showLabel.text = "XXXXX"

  }

}