I created a custom subclass of UIView along with an xib file and declared IBOutlets and IBActions in a custom class.
@interface ContactUsView : UIView @property (nonatomic, weak) IBOutlet UIButton *displayCloseButton; - (IBAction)callButtonPressed:(id)sender; - (IBAction)emailButtonPressed:(id)sender; - (IBAction)displayCloseButtonPressed:(id)sender; @end
In the xib file, I dragged into a UIView to present my custom view. I have installed:
- File Owner = in my custom class
- Set drag and drop in UIView to my custom class.
Then I added a few buttons that are connected to the above methods.
Inside ContactUsView.m I have the following:
- (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; }
When I came to create this view, I do the following:
- (void)viewWillAppear:(BOOL)animated { ContactUsView *contactUs = [[ContactUsView alloc] initWithFrame:CGRectZero]; CGPoint origin = self.view.frame.origin; CGSize size = self.view.frame.size; [contactUs setFrame:CGRectMake(origin.x, CGRectGetMaxY(self.view.frame) - 100, size.width, contactUs.frame.size.height)]; [self.view addSubview:contactUs]; }
Question When I click one of the buttons, the application crashes: Topic 1: EXC_BAD_ACCESS (code = 2, address = 0xb0c
Can anyone help me with this. I feel like I'm probably mistaken in creating and loading custom uiviews from xibs.
If you need more information, let me know. Many thanks.
Future link When creating a custom view using xib, DO NOT set the owner of the files. Instead, create all your IBOutlets and IBActions as usual, and then pin them, open the Utilities tab and drag the control there.
