Tony Law
Tony Law

Reputation: 395

exc_bad_access code=1 when loading UIView

I have an app where I need to present an overlay for feedback. I started by simply creating the exact thing I wanted within the UIViewController I wanted. However this presents to 2 problems. 1) I can't reuse this in another view (As I need to now) and 2) Because it's an overlay, it covers the entire UIViewController on the storyboard so I can't see the controls beneath it.

I looked at moving to an external UIView .xib file and loading dynamically which worked great, except whatever I did, I couldn't never get a handle on the labels within the nib to update the text.

Then I decided that making it a class and creating a delegate method for it would probably be the best way forward.

I have created a very simply .xib and laid it out as well as a .h and .m file (overlayView) and wired it all in an it looks good, except when trying to present the overlayView I get a exc_bad_access on the line

[window addSubview:self];

And I can't work out why. Full code below:

overlayView.h

#import <UIKit/UIKit.h>

@class overlayView;

@protocol overlayDelegate;

@interface overlayView : UIView

@property (nonatomic, strong) id <overlayDelegate> delagate;

-(instancetype)initWithTitle:(NSString *)title
                dateFrom:(NSString *)dateFrom
                  dateTo:(NSString *)dateTo
             description:(NSString *)description;

@property (strong, nonatomic) IBOutlet UILabel *overlayTitleLbl;
@property (strong, nonatomic) IBOutlet UILabel *overlayDateFromLbl;
@property (strong, nonatomic) IBOutlet UILabel *overlayDateToLbl;
@property (strong, nonatomic) IBOutlet UILabel *overlayDescLbl;
@property (strong, nonatomic) IBOutlet UILabel *overlayIcon;

-(void)showOverlay;
-(void)dismissOverlay;

@end

@protocol overlayDelegate <NSObject>

@optional

@end

overlayView.m

#import "overlayView.h"
#import "NSString+FontAwesome.h"

@implementation overlayView


- (instancetype)initWithTitle:(NSString *)title dateFrom:(NSString *)dateFrom dateTo:(NSString *)dateTo description:(NSString *)description {
self.overlayViewTitleLbl.text = title;
self.overlayViewDateFromLbl.text = dateFrom;
self.overlayViewDateToLbl.text = dateTo;
self.overlayViewDescLbl.text = description;
self.overlayViewIcon.text = [NSString fontAwesomeIconStringForIconIdentifier:@"fa-calendar"];

return self;
}

-(void)showOverlay {

UIWindow *window = [[UIApplication sharedApplication] keyWindow];
[window addSubview:self]; <-- Code causing issue
[window makeKeyAndVisible];

}

-(void)dismissOverlay {
  // Not wired in yet  
}

@end

The being called in my main view controller like:

overlay = [[overlayView alloc] initWithTitle:[tmpDict objectForKeyedSubscript:@"Title"] dateFrom:startDate dateTo:stopDate description:[tmpDict objectForKeyedSubscript:@"Desc"]];

[overlay showOverlay];

Any ideas why this doesn't want to play ball? I have breakpointed the initWithTitle method and all information is being passed correctly so I think I am very close to what I am trying to achieve.

Upvotes: 0

Views: 191

Answers (1)

Mortgy
Mortgy

Reputation: 563

you need to initiate your view first, you're returning self without initiating it

- (instancetype)initWithTitle:(NSString *)title dateFrom:(NSString *)dateFrom dateTo:(NSString *)dateTo description:(NSString *)description {
self = [super init]; 
if (self) {
     self.overlayViewTitleLbl.text = title;
     self.overlayViewDateFromLbl.text = dateFrom;
     self.overlayViewDateToLbl.text = dateTo;
     self.overlayViewDescLbl.text = description;
     self.overlayViewIcon.text = [NSString fontAwesomeIconStringForIconIdentifier:@"fa-calendar"];
   }
return self;
}

Upvotes: 1

Related Questions