Deallocated NSMutableArray? - xcode

I can't work out what I am doing wrong with an NSMustableArray. It is fair to say that I don't really understand memory allocation that well, so I apologize if this is a simple questions.
I have a tab bar application that is working well, and on one of the tab bars I have a front view and a back view of a person.
In the .h I have
#interface BurnsCalculatorViewController : UIViewController// <UIPickerViewDelegate, UIPickerViewDataSource>
{
UIView *frontView;
UIView *backView;
UIButton *frontButton;
UIButton *backButton;
NSMutableArray *frontBurnsArray;
}
#property (nonatomic, retain) UIView *frontView;
#property (nonatomic, retain) UIView *backView;
#property (nonatomic, retain) UIButton *frontButton;
#property (nonatomic, retain) UIButton *backButton;
#property (nonatomic, retain) NSMutableArray *frontBurnsArray;
-(IBAction)frontButtonSelect:(id)sender;
-(IBAction)backButtonSelect:(id)sender;
#end
Then I have the .m file
#implementation BurnsCalculatorViewController
#synthesize frontView;
#synthesize backView;
#synthesize frontButton;
#synthesize backButton;
#synthesize frontBurnsArray;
-(IBAction)frontButtonSelect:(id)sender
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:[self view] cache:YES];
[self.view addSubview:backView];
[self.view addSubview:backButton];
[UIView commitAnimations];
NSLog(#"%#", frontBurnsArray);
}
-(IBAction)backButtonSelect:(id)sender
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:[self view] cache:YES];
[self.view addSubview:frontView];
[self.view addSubview:frontButton];
[UIView commitAnimations];
NSLog(#"%#", frontBurnsArray);
}
-(void)viewDidLoad
{
frontBurnsArray = [NSMutableArray arrayWithObjects: #"frontHead", #"frontChest", #"frontAbdomen", #"frontGroin",#"frontLeftArm", #"frontLeftForeArm", #"frontRightArm", #"frontRightForearm", #"frontLeftThigh", #"frontLeftLowerLeg", #"frontRightThigh", #"frontRightLowerLeg",nil];
CGRect viewframe = CGRectMake(0, 0, 320, 480);
frontView = [[UIView alloc] initWithFrame:viewframe];
frontView.backgroundColor = [UIColor blueColor];
[self.view addSubview:frontView];
frontButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[frontButton addTarget:self action:#selector(frontButtonSelect:) forControlEvents:UIControlEventTouchDown];
[frontButton setTitle:#"Show Back" forState:UIControlStateNormal];
frontButton.frame = CGRectMake(210, 10, 100, 30);
[self.view addSubview:frontButton];
backView = [[UIView alloc] initWithFrame:viewframe];
backView.backgroundColor = [UIColor blackColor];
backButton = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
[backButton addTarget:self action:#selector(backButtonSelect:)forControlEvents:UIControlEventTouchDown];
[backButton setTitle:#"Show Front" forState:UIControlStateNormal];
backButton.frame = CGRectMake(210, 10, 100, 30);
}
-(void)dealloc
{
[super dealloc];
[frontButton release];
[backButton release];
[frontBurnsArray release];
}
#end
What I can't figure out is why the NSLogs in the IBActions are both telling me that the instance has been deallocated. Apart from the "release" in the dealloc, I am have said to retain the array and have not released it elsewhere.
I have spent ages searching for an answer to this, but just can't figure it out.
Thanks!!

frontBurnsArray = [NSMutableArray arrayWithObjects: #"frontHead", #"frontChest", #"frontAbdomen", #"frontGroin",#"frontLeftArm", #"frontLeftForeArm", #"frontRightArm", #"frontRightForearm", #"frontLeftThigh", #"frontLeftLowerLeg", #"frontRightThigh", #"frontRightLowerLeg",nil];
You are not using the retaining property
try
self.frontBurnsArray = [NSMutableArray arrayWithObjects: #"frontHead",
#"frontChest",
#"frontAbdomen",
//....
nil];
You are creating an array, that is autorealesed, meaning it will disappear on the next run-loop. If you assign it to the property, it get automatically retained and will exists until you release it. you also could call
frontBurnsArray = [[NSMutableArray arrayWithObjects: #"frontHead",
#"frontChest",
#"frontAbdomen",
//....
nil] retain];

Related

Custom Checkbox Not Working As Expected

I'm attempting to create this checkbutton and then call that check button where ever I'd like in my project and have it work by adding an image like it's supposed to but I can't even get it to NSLog...for testing purposes...
Below are the button .h and .m files. and the view that I'm calling the button into and trying to use...
CheckButton.h
#import
#interface CheckButton : UIButton {
BOOL _checked;
}
#property (nonatomic, setter=setChecked:) BOOL checked;
-(void) setChecked:(BOOL)check;
#end
CheckButton.m
#import "CheckButton.h"
#implementation CheckButton
#synthesize checked = _checked;
-(id) init {
if (self=[super init]) {
self.checked = NO;
[self addTarget:self action:#selector(setChecked:) forControlEvents:UIControlEventTouchUpInside];
}
return self;
}
-(void) awakeFromNib {
self.checked = NO;
[self addTarget:self action:#selector(OnCheck:) forControlEvents:UIControlEventTouchUpInside];
}
- (void) setChecked:(BOOL)check{
_checked = check;
if(_checked) {
UIImage *img = [UIImage imageNamed:#"check.png"];
[self setImage:img forState:UIControlStateNormal];
NSLog(#"Checked");
} else {
UIImage *img = [UIImage imageNamed:#"uncheck.png"];
[self setImage:img forState:UIControlStateNormal];
NSLog(#"UnChecked");
}
}
-(void) OnCheck:(id) sender {
self.checked = _checked;
}
#end
GoToBedPopUp.h
#import "CheckButton.h"
#interface GoToBedPopup : PopupContainer{
IBOutlet CheckButton *checkboxButton;
}
// Checkboxes - (CheckButton)
#property (nonatomic, strong) CheckButton *checkHR;
#end
GoToBedPopUp.m
#import "GoToBedPopup.h"
#implementation GoToBedPopup
#synthesize checkHR, checkO2, checkMovement, checkNoise, checkSkinTemp;
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
// Checkbox Button
checkHR = [[CheckButton alloc] initWithFrame:CGRectMake(17, 31, 23, 23)];
return self;
}
hi i tested your code every thing okay u just change only one thing....
- (void) setChecked:(BOOL)check{
_checked = !check; //** this is the only one change needed
if(_checked) {
UIImage *img = [UIImage imageNamed:#"Check.png"];
[self setImage:img forState:UIControlStateNormal];
NSLog(#"Checked");
} else {
UIImage *img = [UIImage imageNamed:#"Uncheck.png"];
[self setImage:img forState:UIControlStateNormal];
NSLog(#"UnChecked");
}
}
Sample project here

Core Image help: Setting CIImage from UIImage via UIImagePicker (then filtering it)

Searched around on SO for an answer. Found some interesting stuff but I am still stuck.
In a view controller I have a UIImageView which I set an image taken from UIImagePicker when the view loads.
(I also set a CIImage at the same time)
I have two sliders (one for brightness, one for contrast). Upon moving the slider, a filter is applied to the CIImage, then the CIImage is rendered into the the UIImageView's UIImage.
Fist off, the UIImage taken from UIImagePicker does show up correctly when first selected.
I also have my slider ranges set correctly and have verified the proper float values are being passed to the delegate functions (via NSLog).
However, when I try playing with the sliders, my UIImageView turns white! Perhaps you folks can help. I'll post some code snippets:
First, my #interface:
#interface PrepViewController : UIViewController <UIImagePickerControllerDelegate , UINavigationControllerDelegate>
#property (weak, nonatomic) IBOutlet UIImageView *editingImage;
#property (weak, nonatomic) CIImage *editingCIImage;
#property (weak, nonatomic) CIContext *editingCIContext;
#property (weak, nonatomic) CIFilter *editingCIFilter;
#property (nonatomic) BOOL didAskForImage;
#property (weak, nonatomic) IBOutlet UISlider *brightnessSlider;
#property (weak, nonatomic) IBOutlet UISlider *contrastSlider;
- (void) doImageFilter:(NSString *)filter withValue:(float)value;
#end
My viewDidLoad, pretty simple just sets didAskForImage to NO
- (void)viewDidLoad
{
[super viewDidLoad];
self.didAskForImage = NO;
}
viewDidAppear is where I check if I've already asked for the image, then ask for it
- (void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
if(!self.didAskForImage){
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = NO;
UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
sourceType = UIImagePickerControllerSourceTypeCamera;
}
picker.sourceType = sourceType;
[self presentModalViewController:picker animated:YES];
}
}
Here is the imagePickerController didFinishPickingImage delegate method
this is where I actually set the UIImageView's image property and initialize the CIImage, CIContext, and CIFilter objects.
I also set the didAskForImage boolean and the slider targets.
-(void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingImage : (UIImage *)image
editingInfo:(NSDictionary *)editingInfo
{
self.didAskForImage = YES;
self.editingImage.image = image;
self.editingCIImage = [[CIImage alloc] initWithImage:image];
self.editingCIContext = [CIContext contextWithOptions:nil];
self.editingCIContext = [CIContext contextWithOptions:nil];
self.editingCIFilter = [CIFilter filterWithName:#"CIColorControls" keysAndValues:kCIInputImageKey, self.editingCIImage, nil];
[self.contrastSlider addTarget:self action:#selector(contrastMove:) forControlEvents:UIControlEventValueChanged];
[self.brightnessSlider addTarget:self action:#selector(brightnessMove:) forControlEvents:UIControlEventValueChanged];
[picker dismissModalViewControllerAnimated:YES];
}
and the cancel delegate
-(void)imagePickerControllerDidCancel:(UIImagePickerController *) picker
{
[picker dismissModalViewControllerAnimated:YES];
picker = nil;
[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:0] animated:YES];
}
Here are the sliders' delegate methods. Again, the NSLogs display the expected values.
- (void) contrastMove:(id)sender{
NSLog(#"%f", [(UISlider *)sender value]);
[self doImageFilter:#"inputContrast" withValue:[(UISlider *)sender value]];
}
- (void) brightnessMove:(id)sender{
NSLog(#"%f", [(UISlider *)sender value]);
[self doImageFilter:#"inputBrightness" withValue:[(UISlider *)sender value]];
}
And finally, here is the doImageFilter method I created, which actually does the filter and reassigns the UIImageView (this is where it turns white)
- (void) doImageFilter:(NSString *)filter withValue:(float)value{
[self.editingCIFilter setValue:[NSNumber numberWithFloat:value] forKey:filter];
CIImage *output = [self.editingCIFilter outputImage];
CGImageRef cgi = [self.editingCIContext createCGImage:output fromRect:[output extent]];
self.editingCIImage = output;
self.editingImage.image = [UIImage imageWithCGImage:cgi];
}
So I have a hunch that the CIImage isn't even being initialized properly in the first place; but I don't know why. Been at this for a couple hours now.
Thanks for all your help!
Cheers
Hahaha, oh wow.
So I figured it out, my CIContext and CIFilter need to be set to strong instead of weak.
Awesome.

Animation code stopped working

I have animation code in my AppDelegate file thats making the Default.png splashscreen to fade
out to the first controller of the app.
Since I implemented TabBar in my code, the animation stopped working.
Any idea why?
- (void) splashFade
{
splashView = [[UIImageView alloc]
initWithFrame:CGRectMake(0,0, 320, 480)];
splashView.image = [UIImage imageNamed:#"Default.png"];
[_window addSubview:splashView];
[_window bringSubviewToFront:splashView];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:2.0];
[UIView
setAnimationTransition:UIViewAnimationTransitionNone
forView:_window cache:YES];
[UIView setAnimationDelegate:self];
[UIView
setAnimationDidStopSelector:
#selector(startupAnimationDone:finished:context:)];
splashView.alpha = 0.0;
[UIView commitAnimations];
}
- (void)startupAnimationDone:(NSString *)animationID
finished:(NSNumber *)finished
context:(void *)context
{
[splashView removeFromSuperview];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
sleep(2);
[self splashFade];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
UIViewController *viewController1 = [[RootViewController alloc] initWithNibName:#"RootViewController" bundle:nil];
UIViewController *viewController2 = [[TableViewController alloc] initWithNibName:#"TableViewController" bundle:nil];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1, viewController2, nil];
self.window.rootViewController = self.tabBarController;
[self.window addSubview:self.rootViewController.view];
[self.window addSubview:self.navigationController.view];
[self.window makeKeyAndVisible];
return YES;
}
Try using
[self performSelector:#selector(splashFade) withObject:nil afterDelay:2];
better than sleep(2);

UIWebView Delegate Code Doesn't Seem Right

I've implemented sample code using the UIWebView that I found, but it doesn't look right to me, though it works. Specifically because it sets the UIWevView delegate twice (in viewDidLoad and viewWillAppear). Also, myWebView is set as an autorelease object, but then it's released in dealoc. I'm hoping someone can tell me how to clean this up.
// *** WebViewController.h ***
#interface WebViewController : UIViewController
<UIWebViewDelegate>
{
UIWebView *myWebView;
UIActivityIndicatorView *activityIndicator;
}
#property (nonatomic, retain) UIWebView *myWebView;
#property (nonatomic, retain) UIActivityIndicatorView *activityIndicator;
#end
// *** WebViewController.m ***
#synthesize myWebView;
- (void) viewDidLoad {
[super viewDidLoad];
// - - - - -> Create the UIWebView
CGRect webFrame = [[UIScreen mainScreen] applicationFrame];
webFrame.origin.y += 42.0;
webFrame.size.height -= 106.0;
self.myWebView = [[[UIWebView alloc] initWithFrame:webFrame] autorelease];
self.myWebView.backgroundColor = [UIColor whiteColor];
self.myWebView.scalesPageToFit = YES;
self.myWebView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
self.myWebView.delegate = self;
[self.view addSubview: self.myWebView];
// - - - - -> Create the UIActivityIndicatorView
self.activityIndicator = [[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite] autorelease];
[self.view addSubview: self.activityIndicator];
self.activityIndicator.center = CGPointMake(135,438);
[self.myWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://www.someURL.com/"]]];
- (void) viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
self.myWebView.delegate = self;
}
- (void) viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[self.myWebView stopLoading];
self.myWebView.delegate = nil;
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}
- (void) dealloc {
myWebView.delegate = nil;
[myWebView release];
[activityIndicator release];
[super dealloc];
}
It's pretty much fine to do that. The idea is pretty basic. You do not want the delegate to receive any delegation messages when off screen. So the idea of setting the delegate -viewWillAppear: and -viewWillDisappear: is correct. But I don't see any delegate methods being implemented in the code.

splash screen won't show

EDIT 1: changed the code to:
delegate.h:
#import <UIKit/UIKit.h>
#class ViewController;
#interface AppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
ViewController *viewController;
UIImageView *splashView;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet ViewController *viewController;
#property (nonatomic, retain) IBOutlet UIImageView *splashView;
- (void)removeSplash;
#end
delegate.m:
#import "AppDelegate.h"
#import "ViewController.h"
#implementation AppDelegate
#synthesize window;
#synthesize viewController;
#synthesize splashView;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
splashView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
splashView.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"Splash" ofType:#"png"]];
[window addSubview:splashView];
[window bringSubviewToFront:splashView];
[window makeKeyAndVisible];
[self performSelector:#selector(removeSplash) withObject:nil afterDelay:5.0];
[window addSubview:viewController.view];
return YES;
}
- (void)removeSplash {
[splashView removeFromSuperview];
[splashView release];
}
- (void)dealloc {
[viewController release];
[window release];
[super dealloc];
}
#end
EDIT 2:
when I use:
splashView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
splashView.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"Splash" ofType:#"png"]];
if (splashView.image == nil) {
NSLog(#"splashView is nil");
}
it logs "splashView is nil"
My Viewcontroller is empty, just for debugging purposes.
As you probably already know, splash screens are discouraged. Since your image is Default.png, isn't it already being shown on app launch automatically?
In any case, the sleep() call is probably blocking the UI. Remove the sleep() and move the statements after it (the removeFromSuperview, etc) to another method in the app delegate. Call this method using performSelector:withObject:afterDelay:. Put the performSelector call where you currently have the sleep call.
Also, you should use the didFinishLaunchingWithOptions method instead of the old applicationDidFinishLaunching method.

Resources