简体   繁体   中英

How to use delegate in objective C and swift Viewcontroller

My project is combined with one viewcontroller written in objective C ( objCViewController ) and the other is written by swift ( SwiftViewCOntroller ). I have several variable NSString. I would like to update the string in objCViewController and access it in SwiftViewController by using delegate because I need to change between these two viewcontroller continuously and keep updating the string.

Here is the code:

objCViewController.h

#import <UIKit/UIKit.h>

@protocol MyDelegate <NSObject>
@end

@interface objCViewController : UIViewController{
  NSString * stringbeingpassed;
}

@property (nonatomic, weak) id<MyDelegate> delegate;
@property (nonatomic, retain) NSString * stringbeingpassed;
@end

objCViewController.m

@implementation objCViewController
@synthesize delegate;
@synthesize stringbeingpassed;

- (void)updatestring {
  //update string in this method
  NSString * newstring = @"testing";

  if (delegate != nil && [delegate respondsToSelector:@selector(stringUpdated:)]) {
    [delegate stringUpdated: newstring];
}

}

bridging header.h:

#import "objCViewController.h"

SwiftViewController.swift:

protocol MyDelegate {
func stringUpdated(newMessage: String)
}

import UIKit
@objc class SwiftViewController: UIViewController, MyDelegate{
override func viewDidLoad() {
    super.viewDidLoad()
}

func stringUpdated(newMessage:String) {
let newMessage = "sent string"
}

I have tried to use delegate but I have no idea how to use it. I'm completely new to swift and objective C

Q1. I would like to ask how can I assign my newstring to delegate in objCViewController and then pass it to SwiftViewController .

Q2. Another question is that how can I retrieve the data in delegate in SwiftViewController . What should I add?

Q3. Anything else that I have missed in defining delegate? Do I need to define it in both viewcontroller? Thank you.

It's hard to say whether a delegate is the right tool for the job due to the minimal information provided. In fact I might suggest researching better ways to architect your app.

That being said, it would be helpful to see the code for your protocol MyDelegate . If this protocol doesn't already it will need a 'func' that accepts a 'String' as an argument/parameter. For now let's call this function stringUpdated . With that code that's provided you're going to need to set an instance of SwiftViewController to your property delegate within objCViewController . That way when updatestring is called you can do something like this:

- (void)updatestring {
    //update string in this method
    NSString * newstring = @"testing";

    if (delegate != nil && [delegate respondsToSelector:@selector(stringUpdated:)]) {
        [delegate stringUpdated: newstring]
    }
}

In SwiftViewController you'll have to adopt the protocol like so:

@objc class SwiftViewController: UIViewController, MyDelegate {

and then adhere to the protocol by implementing the function stringUpdated .

Update

Your protocol is still missing the method. It should look like this:

@protocol MyDelegate
- (void) stringUpdated:(NSString *)updatedString;
@end

let me answer Q3 first Q3:Anything else that I have missed in defining delegate? Do I need to define it in both viewcontroller? No you haven't to define MyDelegate in both viewControllers, just add it to the objCViewControlle

Now here under I had modified you code

objCViewController.h

@protocol MyDelegate <NSObject>
- (void) stringUpdated:(NSString *)updatedString;
- (NSString *) getString;
@end

@interface objCViewController : UIViewController{
  NSString * stringbeingpassed;
}

@property (nonatomic, weak) id<MyDelegate> delegate;
@property (nonatomic, retain) NSString * stringbeingpassed;
@end

bridging header.h:

#import "objCViewController.h"

SwiftViewController.swift:

import UIKit
@objc class SwiftViewController: UIViewController, MyDelegate{
var thisIsObjectiveVCString:String?
override func viewDidLoad() {
    super.viewDidLoad()
}

func stringUpdated(newMessage:String) {
thisIsObjectiveVCString = newMessage
}

func getString() ->String {
return thisIsObjectiveVCString
}
}

Q2: how can I retrieve the data in delegate in SwiftViewController. What should I add?

on objCViewControlle

if (delegate != nil && [delegate respondsToSelector:@selector(getString)]) {
        NSString * theString = [delegate getString];
    }

Q1: how can I assign my newstring to delegate in objCViewController and then pass it to SwiftViewController

actually on objCViewController if you call updatestring then you will update SwiftViewController

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM