Custom delegate method usage - objective-c

I am trying to use custom delegate between NSObject (MenuComponent) and UIViewController(fullVC) class. Delegate is declared in UIViewController, because UIViewController need to send data (title and url) to my NSObject class.
My protocol methods is in UIViewController(FullVC)
FullVC.h file
#class FullVC;
#protocol MenuComponentDelegate <NSObject>
-(void)shareToView:(NSString *)titleString inUrl:(NSString *)urlString;
#end
#interface FullVC:UIViewController<UIScrollViewDelegate,UITextViewDelegate>
#property (nonatomic,assign) id delegate;
#end
I am trying to call the delegate method in My FullVC. m File by
FullVC.m
#interface FullVC ()
#end
#implementation FullVC
- (void)viewDidLoad
{
[super viewDidLoad];
}
-(void)scrollViewInit
{
_title= #“Title of a Link”;
_urlString = #“Link of the Url”;
if ([delegate respondsToSelector:#selector(sharetoView:inUrl:)])
{
NSLog(#“Checking urlString in scrollView method: %#", _urlString);
[self.delegate shareToView:_title inUrl:_urlString];
}
}
My shareToView Method is implemented in my NSObject Class
MenuComponent.m
#interface MenuComponent() <MenuComponentDelegate>
#property (nonatomic, weak) NSString *titleToShare;
#property (nonatomic, weak) NSString *urlToShare;
#end
#implementation MenuComponent
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([self.nav_controller.topViewController isKindOfClass:[FullVC class]])
{
if (indexPath.row==0)
{
[(FullVC *)self.vav_controller.topViewController setDelegate:self];
NSLog(#“The value of title to share: %#“,titleToShare);
}
}
}
-(void)shareToView:(NSString *)titleString inUrl:(NSString *)urlString
{
NSLog(#"title string after passing is: %#", titleString);
NSLog(#"url string after passing is: %#", urlString);
}
#end
I am having problem to delegate the data. My shareToView method seems out of scope. My NSLogs are not Printing. The only NSLog printing is the one that is showing value for titleToShare. But the value is printing as Null.
The value of title to share:(null)
Could some one help me how to call delegate method since it looks like controller is not calling and not sure it is in scope? Thank you

Related

Subclass inherit Superclass Delegate

I have a hierarchy of UIViews. They are all handled differently but if nested I can not get my setDelegate of super to fire. I receive a crash exception [ThirdClass setDelegate:] unrecognized selector sent to instance. This actually happens no matter what (subclass) i use SecondClass or ThirdClass, but If I use (FirstClass) everything works as it should but any subclassing of the delegate it does not recognize the call. I have simplified what I am doing below which if I call out my first class separately inside my MainControlInterface everything works as it should. Im sure Im doing something wrong here but can't determine what that is, If anyone could help that would be greatly appreciated, thank you.
#protocol FirstClassDataSource, FirstClassDelegate;
#interface FirstClass : UIView
#property (nonatomic, weak_delegate) __nullable id<FirstClassDataSource> dataSource;
#property (nonatomic, weak_delegate) __nullable id<FistClassDelegate> delegate;
#end
#protocol FirstClassDataSource <NSObject>
- (NSInteger)doSomething:(FirstClass *)class;
#optional
- (NSInteger)doSomethingElse:(FirstClass *)class;
#end
#protocol FirstClassDelegate <NSObject>
#optional
- (void)handleMoreDelegateMethods:(FirstClass *)class;
#end
#implementation FirstClass
- (void)setDataSource:(id< FirstClassDataSource >)dataSource
{
if (_dataSource != dataSource)
{
_dataSource = dataSource;
if (_dataSource)
{
[self reloadData];
}
}
}
- (void)setDelegate:(id< FirstClassDelegate>)delegate
{
if (_delegate != delegate)
{
_delegate = delegate;
if (_delegate && _dataSource)
{
[self setNeedsLayout];
}
}
}
#end
#interface SecondClass : FirstClass
-(id)sencondClassesPrivateMethods;
#end
#interface ThirdClass : secondClass
-(id)thirdClassPrivateMethods;
#end
#interface MainControlInterface : UIView <FirstClassDataSource, FirstClassDelegate>
-(ThirdClass *)thirdClass;
#end
#implementation MainControlInterface
-(void)didMoveToSuperview{
ThirdClass *mythirdSubClass = [self thirdClass];
mythirdSubClass.delegate = self;
mythirdSubClass.dataSource = self;
}
#end
I can't tell what you're doing wrong either. But, your sample code will not compile. (It's full of typos.) I have tried to recreate what you're talking about, simplifying it further. (I've used CodeRunner, a macOS app which facilitates this sort of thing.)
#import <Foundation/Foundation.h>
#protocol FirstClassHandling <NSObject>
- (void)doTheThing;
#end
#interface FirstClass : NSObject
#property (nonatomic, weak) id<FirstClassHandling> delegate;
- (void)doSomething;
#end
#implementation FirstClass
- (void)doSomething
{
NSLog(#"First class.");
if ([[self delegate] respondsToSelector:#selector(doTheThing)]) {
[[self delegate] doTheThing];
}
}
#end
#interface SecondClass : FirstClass
#end
#implementation SecondClass
- (void)doSomething
{
NSLog(#"Second class");
[super doSomething];
}
#end
#interface Handler : NSObject <FirstClassHandling>
#end
#implementation Handler
- (void)doTheThing
{
NSLog(#"Doing my thing!!!");
}
#end
int main(int argc, char *argv[])
{
#autoreleasepool {
Handler* handler = [[Handler alloc] init];
SecondClass* sc = [[SecondClass alloc] init];
sc.delegate = handler;
[sc doSomething];
}
}
The above does not crash. Please fix your example code.

Obj-C, How do I use a category to supply methods which I will use in delegate methods?

I want to provide methods used in several view controllers called in my delegate methods.
For example, I have some CloudKit functionality (I've added this to my own framework, but I don't think thats important), where I want to provide some crash logging.
Previosuly I had a crashLog function in each of my view controllers, which worked fine, but I have a lot of duplicate code.
Therefore I'd like to produce a category with these methods instead.
However I'm having difficulty getting my delegate methods to see these category methods.
Here's my code..
UIViewController+CloudKitDelegates.h
#interface UIViewController (CloudKitDelegates) <iCloudDBDelegate>
#property (weak,nonatomic) id<iCloudDBDelegate>iCloudDBDelegate;
-(void)crashLog:(NSString*)message, ...;
#end
UIViewController+CloudKitDelegates.m
#import "UIViewController+CloudKitDelegates.h"
#implementation UIViewController (CloudKitDelegates)
#dynamic iCloudDBDelegate;
-(void)crashLog:(NSString*)message, ...
{
va_list args;
va_start(args, message);
NSLog(#"%#", [[NSString alloc] initWithFormat:message arguments:args]);
va_end(args);
}
#end
h file - my calling view controller (e.g. My View Controller)
#import "UIViewController+CloudKitDelegates.h"
m file - delegate method
-(NSString*)getDBPath
{
[self.iCloudDBDelegate crashLog: #"testing"];
From this call I'm getting an error ...
'NSInvalidArgumentException', reason: '-[MyViewController crashLog:]:
unrecognized selector sent to instance
The error is showing that my calling view controller called MyViewController doesn't have the crashLog method, which I have in my category.
Any ideas where I'm going wrong ?
The problem: identical method crashLog: in multiple classes, for example
#interface ViewController : UIViewController
#end
#implementation ViewController
- (void)someMethod {
[self crashLog:#"error"];
}
-(void)crashLog:(NSString *)message {
NSLog(#"%#", message);
}
#end
Solution A: move crashLog: to a common superclass (or a category on superclass UIViewController)
#interface CommonViewController : UIViewController
-(void)crashLog:(NSString *)message;
#end
#implementation CommonViewController
-(void)crashLog:(NSString *)message {
NSLog(#"%#", message);
}
#end
#interface ViewController : CommonViewController
#end
#implementation ViewController
- (void)someMethod {
[self crashLog:#"error"];
}
#end
Solution B: move crashLog: to a delegate and protocol
#protocol ICloudDBDelegate
-(void)crashLog:(NSString *)message;
#end
#interface DelegateClass : AnyClass <ICloudDBDelegate>
#end
#implementation DelegateClass
-(void)crashLog:(NSString *)message {
NSLog(#"%#", message);
}
#end
#interface ViewController : UIViewController
#end
#implementation ViewController
#property (weak, nonatomic) id <ICloudDBDelegate> iCloudDBDelegate;
- (void)viewDidLoad
{
[super viewDidLoad];
AppDelegate *appDel = (AppDelegate *)[[UIApplication sharedApplication] delegate];
self.iCloudDBDelegate = appDel.iCloudDBDelegate;
}
- (void)someMethod {
[self.iCloudDBDelegate crashLog:#"error"];
}
#end
#interface AppDelegate : UIResponder <UIApplicationDelegate, AppDelProtocolDelegate, iCloudDBDelegate>
#property (strong, nonatomic) id<iCloudDBDelegate>iCloudDBDelegate;
#end
#implementation AppDelegate
- (id<iCloudDBDelegate>)iCloudDBDelegate {
if (!_iCloudDBDelegate) {
_iCloudDBDelegate = [[DelegateClass alloc] init];
}
return _iCloudDBDelegate;
}
#end
Now we have new problem: property iCloudDBDelegate in multiple classes
Solution B + A: move crashLog to a delegate, move iCloudDBDelegate property to a superclass
#protocol ICloudDBDelegate
-(void)crashLog:(NSString *)message;
#end
#interface DelegateClass : AnyClass <ICloudDBDelegate>
#end
#implementation DelegateClass
-(void)crashLog:(NSString *)message {
NSLog(#"%#", message);
}
#end
#interface CommonViewController : UIViewController
#property (weak, nonatomic) id <ICloudDBDelegate> iCloudDBDelegate;
#end
#implementation CommonViewController
#end
#interface ViewController : CommonViewController
#end
#implementation ViewController
- (void)someMethod {
[self.iCloudDBDelegate crashLog:#"error"];
}
#end
Solution C:
Another approach is a singleton object like NSUserDefaults.standardUserDefaults or NSFontManager.sharedFontManager: CloudDBManager.sharedCloudDBManager. No category or protocol required, just include CloudDBManager.h and use CloudDBManager.sharedCloudDBManager from everywhere.
#interface CloudDBManager : NSObject
#property(class, readonly, strong) CloudDBManager *sharedCloudDBManager;
-(void)crashLog:(NSString *)message;
#end
#implementation CloudDBManager
+ (CloudDBManager *)sharedCloudDBManager {
static CloudDBManager *sharedInstance = nil;
static dispatch_once_t onceToken = 0;
dispatch_once(&onceToken, ^{
sharedInstance = [[CloudDBManager alloc] init];
// Do any other initialisation stuff here
});
return sharedInstance;
}
-(void)crashLog:(NSString *)message {
NSLog(#"%#", message);
}
#end
#interface ViewController : CommonViewController
#end
#implementation ViewController
- (void)someMethod {
[CloudDBManager.sharedCloudDBManager crashLog:#"error"];
}
#end
(I've added this to my own framework, but I don't think thats important)
Yep, that's the typical problem. You've failed to include -ObjC in the link flags.
See Building Objective-C static libraries with categories. This applies to frameworks as well.
ObjC does not create linker symbols for methods. It can't, they're not resolved until runtime. So the category methods aren't seen by the linker as "missing" and it doesn't bother linking the relevant compile unit. This is an important optimization that keeps you from linking all of a massive C library just because you use one function in it, but Objective-C categories break some of the linker's assumptions. The compiler saw the definition (via the header), but the linker didn't care, so there's no error until runtime.
The -ObjC flag says "this C-looking compile unit is actually Objective-C; link all of it even if you don't think you need to."

NSSoundDelegate not being called

I am updating some legacy objective C code to be able to be compiled under OSX 10.13. The legacy code worked and most of the update code does as well except for an NSSoundDelegate that needs to handle a didFinishPlaying function. The delegate method is not being called. The delegate method is contained in a class called MyClass. Here is relevant code.
In MyClass.h:
#class MyClass;
#protocol MyClass <NSObject>
#optional
- (void)sound:(NSSound *)sound didFinishPlaying:(BOOL)flag;
#end
#interface MyClass : NSObject <NSSoundDelegate>
{
}
#property (nonatomic, assign) id <NSSoundDelegate> delegate;
- (void)sound:(NSSound *)sound didFinishPlaying:(BOOL)flag;
- (id) init;
#end
Then in MyClass.m:
#implementation MyClass
#synthesize delegate;
- (void)sound:(NSSound *)sound didFinishPlaying:(BOOL)flag
{
if (flag) {
[[NSNotificationCenter defaultCenter] postNotificationName:#"handleNSSoundDidFinishPlaying" object:sound];
}
}
- (id)init
{
MyClass *thePointer;
self = [super init];
if (self) {
thePointer = self;
self.delegate = (id)thePointer;
isInitialized = NO;
isClosing = NO;
[self set_currentSounds:[NSMutableArray arrayWithCapacity:0]];
}
return self;
}
#end
Can anyone see what I'm missing?
I think you should notify the delegate object like:
if([_delegate respondsToSelector:#selector(sound: didFinishPlaying:)])
[_delegate sound:self didFinishPlaying:_flag];
Hope this will help you.
Found the problem! When allocating the sound to be played, you have to set the sounds delegate using [theSnd setDelegate:self]; so that when the sound stops playing, the delegate gets called, in this case the delegate is in the MyClass .m file.

Problems linking NSTableView handler class

I have a strange problem in my Cocoa-app. I have a main window with an NSTableView in it with a controller class (PropValTableHandler). I have made the connections between my NSTableView and the PropValTableHandler, but when the 'numberOfRowsInTableView' method is called it looks like not the 'PropValTableHandler' initialized in 'AddDelegate' is used, since the 'propMan' field is not initialized (it is like the normal init is used, so it has to be another instance of this class).
Am I doing something wrong? I have another NSTableView handler in another window, that works, but it does not have a custom init method.
Source codes:
AppDelegate
#import "AppDelegate.h"
#implementation AppDelegate
#synthesize propValTableController = _propValTableController;
-(id) init
{
self = [super init];
if (self)
{
_propMan = [[OCPropertyManager alloc]initWithPath:"./data/"];
_propValTableController = [[PropValTableHandler alloc]
[initWithPropManager:_propMan];
}
return self;
}
PropValTableHandler
#interface PropValTableHandler : NSObject <NSTableViewDataSource>
#property IBOutlet NSTableView * constants;
#property OCPropertyManager * propMan;
-(id) initWithPropManager:(OCPropertyManager*)pm;
-(NSInteger) numberOfRowsInTableView:(NSTableView *)tableView;
#end
#import "PropValTableHandler.h"
#implementation PropValTableHandler
-(id) initWithPropManager:(OCPropertyManager*)pm
{
self = [super self];
if (self)
{
self.propMan = pm;
}
return self;
}
/*********** TABLEVIEW DATASOURCE ******************/
-(NSInteger) numberOfRowsInTableView:(NSTableView *)tableView
{
NSInteger count = [_propMan.consts count];
return count;
}
/**************************************************/
#end
I have solved the problem by adding a
#property IBOutlet AppDelegate *parent;
for the PropValTableHandler class and making the connections.
This way I can use:
parent.propMan
where ever I need it without passing a reference to it in the init method.

setting new properties in category interface/implementation

Ok, so I have this, but it wont work:
#interface UILabel (touches)
#property (nonatomic) BOOL isMethodStep;
#end
#implementation UILabel (touches)
-(BOOL)isMethodStep {
return self.isMethodStep;
}
-(void)setIsMethodStep:(BOOL)boolean {
self.isMethodStep = boolean;
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
if(self.isMethodStep){
// set all labels to normal font:
UIFont *toSet = (self.font == [UIFont fontWithName:#"Helvetica" size:16]) ? [UIFont fontWithName:#"Helvetica-Bold" size:16] : [UIFont fontWithName:#"Helvetica" size:16];
id superView = self.superview;
for(id theView in [(UIView *)superView subviews])
if([theView isKindOfClass:[UILabel class]])
[(UILabel *)theView setFont:[UIFont fontWithName:#"Helvetica" size:16]];
self.font = toSet;
}
}
#end
If I take out the getter and setter methods then it doesn't work it tells me I need to create some getter and setter methods (or use #synthesize - but putting #synthesize in the #implementation throws an error too). But with the getter and setter methods I get an EXC_BAD_ACCESS and a crash. Any ideas? Thanks
Tom
It is not possible to add members and properties to an existing class via a category — only methods.
https://developer.apple.com/library/content/documentation/General/Conceptual/DevPedia-CocoaCore/Category.html
One possible workaround is to write "setter/getter-like" methods, that uses a singleton to save the variables, that would had been the member.
-(void)setMember:(MyObject *)someObject
{
NSMutableDictionary *dict = [MySingleton sharedRegistry];
[dict setObject:someObject forKey:self];
}
-(MyObject *)member
{
NSMutableDictionary *dict = [MySingleton sharedRegistry];
return [dict objectforKey:self];
}
or — of course — write a custom class, that inherits from UILabel
Note that nowadays an associated object can be injected during runtime. The Objective C Programming Language: Associative References
Checked all answers and did not find the most common solution:
#import <objc/runtime.h>
static void const *key;
#interface ClassName (CategoryName)
#property (nonatomic) BOOL myProperty;
#end
#implementation ClassName (CategoryName)
- (BOOL)myProperty {
return [objc_getAssociatedObject(self, key) boolValue];
}
- (void)setMyProperty:(BOOL)value {
objc_setAssociatedObject(self, key, #(value), OBJC_ASSOCIATION_RETAIN);
}
#end
swift:
private struct AssociatedKeys {
static var keyName = "keyName"
}
extension Foo {
var bar: Any! {
get {
return objc_getAssociatedObject(self, &AssociatedKeys.keyName)
}
set {
objc_setAssociatedObject(self, &AssociatedKeys.keyName , newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
}
}
}
There is actually a way, which may not be ideal, but does work.
For it to work, you will need to create a category for a class X and can only be used on subclasses of the same X (e.g. category UIView (Background) can be used with class MyView : UIView, but not directly with UIView)
// UIView+Background.h
#interface UIView (Background)
#property (strong, nonatomic) NSString *hexColor;
- (void)someMethodThatUsesHexColor;
#end
// UIView+Background.h
#implementation UIView (Background)
#dynamic hexColor; // Must be declared as dynamic
- (void)someMethodThatUsesHexColor {
NSLog(#"Color %#", self.hexColor);
}
#end
Then
// MyView.m
#import "UIView+Background.h"
#interface MyView : UIView
#property (strong, nonatomic) NSString *hexColor;
#end
#implementation MyView ()
- (void)viewDidLoad {
[super viewDidLoad];
[self setHexColor:#"#BABACA"];
[self someMethodThatUsesHexColor];
}
#end
Using this method, you will need to "redeclare" your properties, but after that, you can do all of its manipulation inside your category.
You could inject an associated object during runtime.
#import <objc/runtime.h>
#interface UIView (Private)
#property (nonatomic, assign) CGPoint initialTouchPoint;
#property (nonatomic, strong) UIWindow *alertWindow;
#end
#implementation UIView (Private)
#dynamic initialTouchPoint, alertWindow;
- (CGPoint)initialTouchPoint {
return CGPointFromString(objc_getAssociatedObject(self, #selector(initialTouchPoint)));
}
- (void)setInitialTouchPoint:(CGPoint)initialTouchPoint {
objc_setAssociatedObject(self, #selector(initialTouchPoint), NSStringFromCGPoint(initialTouchPoint), OBJC_ASSOCIATION_RETAIN);
}
- (void)setAlertWindow:(UIWindow *)alertWindow {
objc_setAssociatedObject(self, #selector(alertWindow), alertWindow, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (UIWindow *)alertWindow {
return objc_getAssociatedObject(self, #selector(alertWindow));
}
#end
EDIT: Warning: This property would have a unique value for all the instances of the class.
This worked for me, but only because I had only one instance of this class in my app.
#import <AVFoundation/AVFoundation.h>
#interface AVAudioPlayer (AstroAVAudioPlayer)
#property (nonatomic) BOOL redPilot;
#end
#import "AVAudioPlayer+AstroAVAudioPlayer.h"
#implementation AVAudioPlayer (AstroAVAudioPlayer)
BOOL _redPilot;
-(void) setRedPilot:(BOOL)redPilot
{
_redPilot = redPilot;
}
-(BOOL) redPilot
{
return _redPilot;
}
#end
A solution that I found to this was to just give each object that you want flagged a unique tag.
I made a UILabel category to add custom fonts to all my labels but on some i wanted them to be bold so i did this ->
- (void) layoutSubviews {
[super layoutSubviews];
[self addCustomFont];
}
- (void) addCustomFont {
if (self.tag == 22) {
[self setFont:[UIFont fontWithName:SEGOE_BOLD size:self.font.pointSize]];
}else{
[self setFont:[UIFont fontWithName:SEGOE_LIGHT size:self.font.pointSize]];
}
}
It seems as if since Xcode 7 (7.0.1, 7A1001), properties are supported in categories. I noticed that Xcode generates categories now for Core Data subclasses.
For example, I got the files:
Location+CoreDataProperties.h
#import "Location.h"
NS_ASSUME_NONNULL_BEGIN
#interface Location (CoreDataProperties)
#property (nullable, nonatomic, retain) NSNumber *altitude;
#property (nullable, nonatomic, retain) NSNumber *latitude;
#property (nullable, nonatomic, retain) NSNumber *longitude;
#end
NS_ASSUME_NONNULL_END
Location+CoreDataProperties.m
#import "Location+CoreDataProperties.h"
#implementation Location (CoreDataProperties)
#dynamic altitude;
#dynamic latitude;
#dynamic longitude;
#end
So looks like properties in categories might work now. I haven't tested on non-Core Data classes.
What I've noticed is that they do include the category file back into the original class:
Location.h
#interface Location : NSManagedObject
#end
#import "Location+CoreDataProperties.h"
This allows the original class to edit the properties specified by the category.