I’m trying to make a delegate that will send messages from UIButtons within a custom UIView called SGView and am a bit out of my depth following the error messages. I recently learned how to get a delegate to send messages successfully from within an instance method.
SGView includes a class method that arranges multiple UIButtonsin a circle and was designed to be used by different UIViews. The geometry works when the method is called from a parent UIView. I have now declared the delegate by adding @protocol and @property to the class method declaration
like so ...
SGView.h
#import <UIKit/UIKit.h>
@protocol SGViewDelegate <NSObject>
-(void)buttonPressed:(UIButton*)button;
@end
@interface SGView : UIView {
}
+(id)circleOfButtons:(int)buttonCount buttonSize:(CGFloat)buttonSize circleRadius:(CGFloat)circleRadius;
@property (assign) id<SGViewDelegate> delegate;
@end
SGView.m is essentially like this
#import "SGView.h"
@implementation SGView : UIView
+(id)circleOfButtons:(int)buttonCount buttonSize:(CGFloat)buttonSize circleRadius:(CGFloat)circleRadius
{
UIView *multipleViews = [self new];
// … circular geometry …
[circleButton addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[multipleViews addSubview:circleButton];
}
return multipleViews;
}
@end
The moment I change the target from self to self.delegate in the target action statement above, Xcode reports three errors
Definition of 'objc_class' must be imported from module 'ObjectiveC.runtime' before it is required
No member named 'delegate' in 'struct objc_class'
Member reference type 'struct objc_class *' is a pointer; did you mean to use '->'?
Xcode offers a Fixit for the last of these
Fixit Replace”.” with “->”
By accepting the Fixit I removed the three issues and introduced another
Member reference base type 'Class' is not a structure or union
If this Fixit has lead me in the right direction, what exactly should I be doing next ?
I need to ask this question to make sense of what might seem a straight forward problem before diving back into the Apple documentation and these tutorials trying to solve it. Any help is most appreciated. Thanks.
SOLUTION
This can be found in my answer to my own question.
Changing the class method to an instance method appears to have solved my problem.
This involved making the class method
+(id)circleOfButtons:... etcinto an instance method (-(id)circleOfButtons:... etc) in both.mand.hfiles. It was also necessary to change the call in the parentUIViewfromto
and change the declaration in
SGViewfromto
SGViewcan be now reused from differentUIViewsthat change the number and size ofUIButtonsarranged in a circle. The delegate inSGViewalso allows each button to send a tagged message to a method in another class - in my case theViewController.The complete listing is included below and I'd welcome suggestions or improvements from others with more experience in OO programming.
ViewController.h
ViewController.m
FamilyView.h
FamilyView.m
SGView.h
SGView.m