exc_bad_access code=1 when loading UIView - ios

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.

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;
}

Related

How do you hook up a custom UIView?

Writing my first iphone app through this tutorial--I have a custom UIView with the method showDie and I have two UIView elements on my storyboard that I've hooked up to my ViewController. However, both the UIView elements have the error message "No visible #interface for 'UIView' declares the selector 'showDie'. I think that's because the UIViews aren't subclassing XYZDieView, but when I control-clicked from my Storyboard, only UIView appeared in the dropdown--no XYZDieView. I tried just changing the text manually in the ViewController.h, but that didn't work (I didn't think it would). Am I on the right track? How do I use my subclass? (xcode 5)
XYZDieView.m
-(void)showDie:(int)num
{
if (self.dieImage == nil){
self.dieImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 90, 90)];
[self addSubview:self.dieImage];
}
NSString *fileName = [NSString stringWithFormat:#"dice%d.png", num];
self.dieImage.image = [UIImage imageNamed:fileName];
}
XYZViewController.h
#import <UIKit/UIKit.h>
#import "XYZDieView.h"
#interface XYZViewController : UIViewController
#property (weak, nonatomic) IBOutlet UILabel *sumLabel;
#property (strong, nonatomic) IBOutlet UIButton *rollButton;
#property (strong, nonatomic) IBOutlet UIView *leftDieView;
#property (strong, nonatomic) IBOutlet UIView *rightDieView;
#end
XYZViewController.m
#import "XYZViewController.h"
#import "XYZDiceDataController.h"
#interface XYZViewController ()
#end
#implementation XYZViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)rollButtonClicked:(UIButton *)sender {
XYZDiceDataController *diceDataController = [[XYZDiceDataController alloc] init];
int roll = [diceDataController getDiceRoll];
int roll2 = [diceDataController getDiceRoll];
[self.leftDieView showDie:roll];
[self.rightDieView showDie:roll2];
int sum = roll + roll2;
NSString *sumText = [NSString stringWithFormat:#"Sum is %i", sum];
self.sumLabel.text = sumText;
}
#end
Yes you're right, the problem is subclassing. In order to have a view conform to a subclass in the interface builder you need to
Click on the UIView in your storyboard
Open the right inspector panel
Click on the third tab for identity inspector
Add your class
Then try to connect it to your ViewController again.
Edit: Afterwards you may need to cast XYZDieView myView = (XYZDieView*)leftDieView;

Problems in adding object from one view to a mutableArray in another view

Hi everyone I have been building an app and have met some problems.
My app has two viewControllers. One is MenuViewController and another one is MainViewController.
I want to pass a string from MainViewController to a mutableArray in MenuViewController, but have no idea how.
Here are my codes:
<MenuViewController.h>
#import <UIKit/UIKit.h>
#interface MenuViewController : UITableViewController {
NSMutableArray *secondFavourite;
}
#property (nonatomic, strong) NSMutableArray *secondFavourite;
#end
.
<MenuViewController.m>
#import "MenuViewController.h"
#import "MainViewController.h"
#interface MenuViewController ()
#property (strong, nonatomic) NSArray *menu;
#end
#implementation MenuViewController
#synthesize menu;
#synthesize secondFavourite;
- (void)viewDidLoad
{
[super viewDidLoad];
self.secondFavourite = [[NSMutableArray alloc] init];
self.menu = self.secondFavourite;
}
.
<MainViewController.h>
#import <UIKit/UIKit.h>
#import <social/Social.h>
#interface MainViewController : UIViewController {
IBOutlet UIImageView *imagepost;
UILabel *predictionLabel;
}
- (IBAction)sampleSelector:(UIButton *)sender;
- (IBAction)showAllClick:(id)sender;
#property (nonatomic, retain) IBOutlet UILabel *predictionLabel;
#property (strong, nonatomic) NSArray *predictionArray;
#property (strong, nonatomic) UIButton *menuBtn;
#property (strong, nonatomic) NSMutableArray *fav;
#property (strong, nonatomic) IBOutlet UILabel *favLabel;
#property (strong, nonatomic) IBOutlet UITableView* tableView;
#property (nonatomic, strong) NSMutableArray *favourite;
#end
.
<MainViewController.m>
- (void)viewDidLoad
{
[super viewDidLoad];
self.predictionArray = [[NSArray alloc] initWithObjects:#"Hey gurl", nil];
}
//Add to favourite
- (void) addToFav {
self.favourite = [[NSMutableArray alloc] init];
[self.favourite addObject:self.predictionLabel.text];
[self.tableView reloadData];
NSLog(#"%#", self.favourite);
}
//add to favourite button action
- (IBAction)addToFavButton:(id)sender {
[self addToFav];
//pass data from favourite here to secondFacourite in MenuViewController (found on stack overflow)
MenuViewController *menuViewController = [[MenuViewController alloc]initWithNibName:#"MenuViewController" bundle:nil];
menuViewController.secondFavourite = [[NSMutableArray alloc]initWithArray:self.favourite];
[self.navigationController pushViewController:menuViewController animated:YES];
}
I used NSLog to check that the menuViewController.secondFavourite in MainViewController successfully added the string into the array, isn't the array the same array in MenuViewController? Why doesn't the menu.tableView update and show the new string added? I am very confused and I hope someone will help me out.
Thanks for reading this.
This has to do with the fact that your menu viewDidLoad is overwriting the value in these two lines:
self.secondFavourite = [[NSMutableArray alloc] init];
self.menu = self.secondFavourite;
That first line is setting your secondFavourite property to an empty NSMutableArray instance. And since viewDidLoad will be called only after the view has been loaded into memory (in this case, when you try to push the view controller onto the stack), your initial values in the secondFavourite property will be lost.
Instead, you should move that initialization code into the an init method.

I'm struggling to implement a subclass of UIDynamicBehavior

I'm trying to move a UIView across the screen.
UIGravityBehavior and UIPushBehavior both take things like density, velocity and friction into account. I tried to implement my own dynamic behavior that ignores those physics.
My UIDynamicBehavior subclass and some basic implementation
// MYDynamicBehavior.h
#interface MYDynamicBehavior : UIDynamicBehavior
- (void)addItem:(id<UIDynamicItem>)item;
- (void)removeItem:(id<UIDynamicItem>)item;
#end
// MYDynamicBehavior.m
#interface MYDynamicBehavior ()
#property (strong, nonatomic) NSMutableArray *items;
#end
#implementation MYDynamicBehavior
- (void (^)(void))action
{
__weak MYDynamicBehavior *weakSelf = self;
for (UIView *item in weakSelf.items)
item.center = CGPointMake(item.center.x + 10.0, item.center.y);
}
- (instancetype)init
{
if (self=[super init]) {
__weak MYDynamicBehavior *weakSelf = self;
weakSelf.action = ^{
for (UIView *item in weakSelf.items)
item.center = CGPointMake(item.center.x + 10.0, item.center.y);
};
}
return self;
}
- (void)addItem:(id<UIDynamicItem>)item
{
[self.items addObject:item];
}
#end
// ViewController.m
// #includes
#interface ViewController ()
#property (strong, nonatomic) UIDynamicAnimator *ani;
#property (strong, nonatomic) UIView *kin;
#property (strong, nonatomic) MYDynamicBehavior *skywalk;
#end
#implementation ViewController
…
- (void)viewDidLoad
{
[super viewDidLoad];
self.ani = [[UIDynamicAnimator alloc] init];
self.kin = // some view
self.skywalk = [[MYDynamicBehavior alloc] init];
[self.ani addBehavior:self.skywalk];
[self.skywalk addItem:kin];
}
#end
I'm trying to recreate this from memory, I think the basics are here
Anyway, it's my impression from the documentation that the action property is where I need to implement my animation. It doesn't appear that my action black is ever called, however.
This is the closest I've come to a solution, but I still haven't solved this problem yet.
What am I missing? Why isn't my custom UIDynamicBehavior subclass working?
I haven't found the documentation that states this explicitly, and I can't guess at the underlying reason, but I have found that if my custom UIDynamicBehavior classes call their action blocks only if there is a child behavior added which has at least one item in it.
It's weird enough that I think I'm experiencing a side effect rather than this working as intended, but that does reliably get the action block to fire. Would be really interested if anybody could shed light on the why, though. :1

Can't send data between views using a protocol & delegate with presentModalViewController

I'm making elevator thing. I'm having trouble sending data with different views using presentModalViewController. I got red message "favoriteColorString" property not found. I copied exactly the same but different form names and buttons. The "favoriteColorString" appears an error and unable to send elevator2 data.
I tried two different thing.
Elevator2View.favoriteColorString = [[NSString alloc] initWithFormat:#"Your favorite color is %#", favoriteColorTextField.text];
And
favoriteColorString = [[NSString alloc] initWithFormat:#"Your favorite color is %#", favoriteColorTextField.text];
Here's my code:
ElevatorView.h
#import <UIKit/UIKit.h>
#import "Elevator2View.h"
#interface ElevatorView : UIViewController<PassSecondColor>
{
Elevator2View *Elevator2View;
IBOutlet UITextField *favoriteColorTextField;
IBOutlet UILabel *favoriteColorLabel;
IBOutlet UILabel *secondFavoriteColorLabel;
NSString *secondFavoriteColorString;
}
#property (nonatomic, retain) Elevator2View *Elevator2View;
#property (nonatomic, retain) IBOutlet UITextField *favoriteColorTextField;
#property (nonatomic, retain) IBOutlet UILabel *favoriteColorLabel;
#property (nonatomic, retain) IBOutlet UILabel *secondFavoriteColorLabel;
#property (copy) NSString *secondFavoriteColorString;
#end
ElevatorView.m
#import "ElevatorView.h"
#import "Elevator2View.h"
#implementation ElevatorView
#synthesize Elevator2View, favoriteColorTextField, favoriteColorLabel, secondFavoriteColorLabel;
#synthesize secondFavoriteColorString;
-(IBAction)level1:(id)sender;{
favoriteColorTextField.text = #"1";
Elevator2View.favoriteColorString = [[NSString alloc] initWithFormat:#"Your favorite color is %#", favoriteColorTextField.text];
[self presentModalViewController:[[[Elevator2View alloc] init]
autorelease] animated:NO];
}
Elevator2View.h
#import <UIKit/UIKit.h>
#protocol PassSecondColor <NSObject>
#required
- (void) setSecondFavoriteColor:(NSString *)secondFavoriteColor;
#end
#interface Elevator2View : UIViewController{
IBOutlet UITextField *secondFavoriteColorTextField;
IBOutlet UILabel *favoriteColorLabel;
IBOutlet UILabel *secondFavoriteColorLabel;
NSString *favoriteColorString;
id <PassSecondColor> delegate;
}
#property (copy) NSString *favoriteColorString;
#property (nonatomic, retain) IBOutlet UITextField *secondFavoriteColorTextField;
#property (nonatomic, retain) IBOutlet UILabel *favoriteColorLabel;
#property (nonatomic, retain) IBOutlet UILabel *secondFavoriteColorLabel;
#property (retain) id delegate;
#end
Elevator2View.m
#import "Elevator2View.h"
#interface Elevator2View ()
#end
#implementation Elevator2View
#synthesize secondFavoriteColorTextField, favoriteColorLabel, secondFavoriteColorLabel;
#synthesize favoriteColorString;
#synthesize delegate;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void) viewWillAppear:(BOOL)animated
{
favoriteColorLabel.text = favoriteColorString;
}
- (void) viewWillDisappear:(BOOL) animated
{
// [[self delegate] setSecondFavoriteColor:secondFavoriteColorTextField.text];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
favoriteColorLabel.text = favoriteColorString;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
See http://www.theappcodeblog.com/?p=90
The reason for your "property not found" is that you named your ivar same as class.
Dot notation is just a syntactic sugar: object.property = value is equivalent to [object setProperty:value]. In Objective C classes are also objects, and when you call Elevator2View.favoriteColorString = whatever, Xcode apparently thinks that you are attempting to call class method setFavoriteColorString of class Elevator2View.
Getting rid of this error is easy: just rename your ivar Elevator2View *Elevator2View to something else. In fact, Xcode 4.4 and newer autosynthesizes ivars for your properties: if you have a property propertyName, then Xcode will autosynthesize ivar _propertyName. Your property Elevator2View will have _Elevator2View ivar. So unless you really really need to have ivars with different naming scheme, you can get rid of your #synthesize, and you also don't need to declare ivars for you properties.
(Though I prefer to declare ivars for properties (following Xcode naming scheme), because far too often lldb doesn't show autosynthesized-without-declaring ivars in object inspector.)
That was about properties, ivars and naming conventions. But what are you doing in this code?
-(IBAction)level1:(id)sender;{
favoriteColorTextField.text = #"1";
Elevator2View.favoriteColorString = [[NSString alloc] initWithFormat:#"Your favorite color is %#", favoriteColorTextField.text];
[self presentModalViewController:[[[Elevator2View alloc] init]
autorelease] animated:NO];
}
You set value of Elevator2View's - your instance variable's - property, then create brand new object of Elevator2View class and present that as modal view controller. (By the way, presentModalViewController:animated: is deprecated in iOS 6.0). Of course, this brand new Elevator2View object has no idea what Elevator2View's (your instance variable's) properties are!

Won't call on method in other class, Objective-C

I am trying to learn Objective-C for iOS. i am currently following the "coding together" on iTunesU. Although i have got stuck since i can't get my controller to call on a method from another class. Can't find what i am doing wrong and thought that StackOverflow might have the solution to it!
The method "flipCardAtIndex" is the one that isn't working. I have debugged using nslog and from the method "flipCard" i get an output. But when i put in the implementation for flipCardAtIndex i don't get anything.. So my guess is that it never calls it...
I have made the code a bit shorter so it is only the parts i think is important, this is controller:
#import "ViewController.h"
#import "PlayingCardDeck.h"
#import "CardMatchingGame.h"
#interface ViewController ()
#property (weak, nonatomic) IBOutlet UILabel *flipsLabel;
#property (nonatomic) int flipCount;
#property (weak, nonatomic) IBOutlet UILabel *scoreLabel;
#property (strong, nonatomic) IBOutletCollection(UIButton) NSArray *cardButtons;
#property (strong, nonatomic) CardMatchingGame *game;
#end
#implementation ViewController
- (CardMatchingGame *) game{
if (_game) _game = [[CardMatchingGame alloc] initWithCardCount:[self.cardButtons count]
usingDeck:[[PlayingCardDeck alloc] init]];
return _game;
}
- (IBAction)flipCard:(UIButton *)sender {
[self.game flipCardAtIndex:[self.cardButtons indexOfObject:sender]];
self.flipCount++;
[self updateUI];
}
And implementation:
- (void)flipCardAtIndex:(NSUInteger)index
{
NSLog(#"ALL YOUR BASE ARE BELONG TO US");
Card *card = [self cardAtIndex:index];
}
Fix?
- (CardMatchingGame *) game{
if (!_game) _game = [[CardMatchingGame alloc] initWithCardCount:[self.cardButtons count] usingDeck:[[PlayingCardDeck alloc] init]];
return _game;
}

Resources