I asked a question HERE about my app crashing when a button is clicked on my custom UIView. I am now wondering if what I am trying to do is correct.
My aim is to create a custom view that can be reused. To do this I am creating the custom UIView with IBActions to handle the button presses. These IBActions will call a delegate methods. In a sense pass of the method presses to the view controller that has created the view. This means that the view can be created inside any view controller and all that view controller has to do is implement the delegate methods.
I have a xib file that contains a UIView that is set to my custom view class. The files owner is also set to my custom class.
#import <UIKit/UIKit.h>
@protocol ContactUsViewDelegate <NSObject>
- (void)callPressed;
- (void)emailPressed;
- (void)displayCloseContactViewPressed;
@end
@interface ContactUsView : UIView
@property (nonatomic, weak) id <ContactUsViewDelegate> delegate;
- (IBAction)callButtonPressed:(id)sender;
- (IBAction)emailButtonPressed:(id)sender;
- (IBAction)displayCloseButtonPressed:(id)sender;
@end
Then the implementation will look as follows:
#import "ContactUsView.h"
@implementation ContactUsView
- (id)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
NSArray* array = [[NSBundle mainBundle] loadNibNamed:@"ContactUsView" owner:self options:nil];
for (id object in array) {
if ([object isKindOfClass:[ContactUsView class]])
self = (ContactUsView *)object;
}
}
return self;
}
- (IBAction)callButtonPressed:(id)sender
{
[self.delegate callPressed:sender];
}
- (IBAction)emailButtonPressed:(id)sender
{
[self.delegate emailPressed:sender];
}
- (IBAction)displayCloseButtonPressed:(id)sender
{
[self.delegate displayCloseButtonPressed:sender];
}
Is this a valid approach to trying to create a reusable view that can be implemented in any view controller?