-1

I have two classes, one view controller an another NSObject class that performs async processes, once its done I want to interrupt the view controller similar to what a button does as IBAction, to update UI. I wanted to keep this functionality in modularized for better use in later stages. Is there a way to do this on Objective-c?

CircleDetectionViewController.h:

#import <UIKit/UIKit.h>
#import "CameraController.h"



@interface CircleDetectionViewController : UIViewController <MyDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *screenPreview;

@end

CircleViewController.mm:

@interface CircleDetectionViewController () <CvVideoCameraDelegate>

@end

@implementation CircleDetectionViewController
CvVideoCamera *camera;
//code
@end

CameraController.h:

#import <Foundation/Foundation.h>
#import <AVFoundation/AVFoundation.h>
#import "CircleDetectionViewController.h"

@protocol MyDelegate <NSObject>

-(void) updateUI;
@end

@interface CameraController : NSObject <MyDelegate>
@property (nonatomic, strong) AVCaptureDevice *inputDevice;
@property (nonatomic, strong) AVCaptureSession *session;
@property (nonatomic, strong) AVCaptureStillImageOutput *photoOutput;
@property (nonatomic, weak) id <MyDelegate> delegate;
@end

CameraController.m:

#import "CameraController.h"
#import "FlashHelper.h"
#import "ImageProcessor.h"

@implementation CameraController
@synthesize delegate;
FlashHelper *Flash;
ImageProcessor *IMGProcessor;
int shootCounter;
int NUMSHOTS = 2;

-(id)init{
//code 
}

For some reason the code says "No type or protocol named MyDelegate" Any help would be really appreciated!

Abhishek Ravi
  • 137
  • 11
  • use delegates or callbacks / completion blocks. That should be terms you can google easily and use after you understood them. – luk2302 May 25 '17 at 19:51
  • 1
    Perfect time for Delegate pattern! https://developer.apple.com/library/content/documentation/General/Conceptual/CocoaEncyclopedia/DelegatesandDataSources/DelegatesandDataSources.html – DonMag May 25 '17 at 19:51
  • My view controller already has a delegate for openCV used, can i still use my own custom delegate? – Abhishek Ravi May 25 '17 at 20:20

1 Answers1

-1

In NSObject class create a delegate:

@protocol MyDelegate <NSObject>

-(void) updateUI;
@end

and declare a property of delegate

@property (nonatomic, weak) id <MyDelegate> delegate;

In viewcontroller class invoke the delegate class

NSObject class obj.delegate = self; and implement the delegate method.

Always update your UI from Main Thread.

To get the main thread use this:

dispatch_async(dispatch_get_main_queue(), ^{
   // call your delegate method or update UI here
});

For details for delegate methods check this.

Md Milan Mia
  • 121
  • 1
  • 8
  • The problem I have with delegates is that my view controller already has a delegate in use the openCV camera one, is there a way to use two delegates? – Abhishek Ravi May 25 '17 at 20:19
  • You can invoke multiple delegates in one view controller like UIViewController . Also, you can use NSNotificationCenter to achieve the same purpose. – Md Milan Mia May 25 '17 at 20:32