iphone beginner : Cannot figure out where the leak is - iphone

I am trying to get my hands on some iphone development. To try to better understand things, I'm going first without IB.
I managed to build a basic app that displays some text. No big deal, until I ran it through Instruments. It shows me some leaks and I cannot understand them.
Any help on this would be greatly appreciated.
MyAppDelegate.h
#interface MyAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
MyViewController *viewController;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) MyViewController *viewController;
#end
MyAppDelegate.m
#implementation MyAppDelegate
#synthesize window;
#synthesize viewController;
- (void)applicationDidFinishLaunching:(UIApplication *)application {
// NEXT LINE LEAKS
[window addSubview:viewController.view];
[window makeKeyAndVisible];
}
- (void)dealloc {
[viewController release];
[window release];
[super dealloc];
}
#end
MyViewController.h
#interface MyViewController : UIViewController {
UILabel *firstMessage;
}
MyViewController.m
-(void)loadView {
// Background
CGRect mainFrame = [[UIScreen mainScreen] applicationFrame];
UIView *contentView = [[UIView alloc] initWithFrame:mainFrame];
contentView.backgroundColor = [UIColor blackColor];
self.view = contentView;
[contentView release];
// Add UILabel
// NEXT LINE LEAKS
firstMessage = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 30)];
firstMessage.font = [UIFont fontWithName:#"Helvetica" size:16];
firstMessage.textColor = [UIColor whiteColor];
firstMessage.backgroundColor = [UIColor blackColor];
[self.view addSubview:firstMessage];
[firstMessage release];
}
-(void) viewDidLoad {
NSString * msg;
msg=[[NSString alloc] initWithFormat:#"stuff here"];
firstMessage.text=msg;
[msg release];
}
-(void)dealloc {
[firstMessage release];
[super dealloc];
}

Try using #property (nonatomic, retain) UILabel* firstMessage in your header, #synthesize firstMessage in your implementation and then release your firstMessage object in the dealloc method.

Try not to release your firstMessage in loadView. Only release it in your controller's dealloc method.

Related

iOS - adding/removing a subview programmatically

Ok I want to add a UIImageView as a subview and then remove it after a couple of seconds in the way a splash screen works. I found three different approaches to do it but I can not understand which one is the best approach according to Objective-C and Apple.
Below are the three different approaches:
1)
In my MyAppDelegate.h
#interface MyAppDelegate : NSObject <UIApplicationDelegate> {
MyViewController *myViewController;
UIImageView *myImageView;
}
#property (nonatomic, retain) IBOutlet MyViewController *myViewController;
#property (nonatomic, retain) IBOutlet UIWindow *window;
#end
and in MyAppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
myImageView =[[UIImageView alloc] initWithFrame:CGRectMake(0.0,0.0,self.window.frame.size.width,self.window.frame.size.height)];
myImageView.image=[UIImage imageNamed:#"Yoga.png"];
[self.window addSubview:myImageView ];
[self.window bringSubviewToFront:myImageView];
[self performSelector:#selector(removeImage) withObject:nil afterDelay:2.5];
return YES;
}
-(void) removeImage
{
[myImageView removeFromSuperview];
[myImageView release];
[self.window addSubview:myViewController.view];
[self.window makeKeyAndVisible];
}
2) in the second approach:
In my MyAppDelegate.h
#interface MyAppDelegate : NSObject <UIApplicationDelegate> {
MyViewController *myViewController;
UIImageView *myImageView;
}
#property (nonatomic, retain) IBOutlet UIImageView *myImageView;
#property (nonatomic, retain) IBOutlet MyViewController *myViewController;
#property (nonatomic, retain) IBOutlet UIWindow *window;
#end
and in MyAppDelegate.m
#synthesize myImageView;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
myImageView =[[UIImageView alloc] initWithFrame:CGRectMake(0.0,0.0,self.window.frame.size.width,self.window.frame.size.height)];
myImageView.image=[UIImage imageNamed:#"Yoga.png"];
[self.window addSubview:myImageView ];
[self.window bringSubviewToFront:myImageView];
[self performSelector:#selector(removeImage) withObject:nil afterDelay:2.5];
return YES;
}
-(void) removeImage
{
[myImageView removeFromSuperview];
[myImageView release];
[self.window addSubview:myViewController.view];
[self.window makeKeyAndVisible];
}
- (void)dealloc
{
[myViewController release];
[myImageView release];
}
3) in the third approach:
In my MyAppDelegate.h
#interface MyAppDelegate : NSObject <UIApplicationDelegate> {
MyViewController *myViewController;
}
#property (nonatomic, retain) IBOutlet MyViewController *myViewController;
#property (nonatomic, retain) IBOutlet UIWindow *window;
#end
and in MyAppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UIImageView *myImageView =[[UIImageView alloc] initWithFrame:CGRectMake(0.0,0.0,self.window.frame.size.width,self.window.frame.size.height)];
myImageView.image=[UIImage imageNamed:#"Yoga.png"];
myImageView.tag=22;
[self.window addSubview:myImageView ];
[myImageView release];
[self.window bringSubviewToFront:myImageView];
[self performSelector:#selector(removeImage) withObject:nil afterDelay:2.5];
return YES;
}
-(void) removeImage
{
for (UIView *subview in [self.view subviews]) {
if (subview.tag == 22){
[subview removeFromSuperview];
}
}
[self.window addSubview:myViewController.view];
[self.window makeKeyAndVisible];
}
- (void)dealloc
{
[myViewController release];
}
So to sum up.. The first approach does not use a property for the UIImage only a variable, the second one uses a property and the third one just creates the UIImage and adds it as a subview and then removes it based on its tag..
Which is the right approach to follow..I believe that all three options sound right.. But is there any certain way I should follow. Is any of these options better in terms of memory and performance?
Thanks in advance,
Andreas
You could use an animation attached to the view's layer. Code below fades the view out - but there are many other ways to remove it. (you need to attach the QuartzCore framework)
myImageView.layer.opacity = 0.0;
// this is the state the view will be in after the animation (e.g. invisible)
CABasicAnimation *theFade;
theFade = [CABasicAnimation animationwithKeyPath:#"opacity"];
theFade.duration = 10.0;
theFade.fromValue = [NSNumber numberWithFloat:1.0]; // i.e. visible
theFade.toValue = [NSNumber numberWithFloat:0.0]; // i.e. invisible
[myImageView.layer addAnimation:theFade forKey:#"animateOpacity"];
If you are not going to use the image again, there is no need to keep a pointer to it. Further, if you use IBOutlet, you need to add the view in IB as well. In this specific example I would say option 3 makes the most sence, especially considering that with this choice you can began with a standard "view based application" template and just add the bits about the image view and leave the rest alone. One last observation of choice 3 though; the 2 messages to window;
[self.window addSubview:myViewController.view];
[self.window makeKeyAndVisible];
Appear to be outside the scope of any method. This is likely just a copy and paste error, but make note that they should located within "didFinishLaunchingWithOptions:"

iOS (iPhone/iPad) SDK - Application not changing orientation

In my custom Tab Bar Application, the orientation never seems to change, even if I force rotate the status bar. Here is my code in my AppDelegate:
AppDelegate.h:
#import <UIKit/UIKit.h>
#import "MBProgressHUD.h"
#class exampleViewContoller;
#class example1ViewController;
#class example2ViewController;
#class example3ViewController;
#class example4ViewController;
#interface <appname>AppDelegate : NSObject <UIApplicationDelegate, MBProgressHUDDelegate> {
UIWindow *window;
UITabBarController *rootController;
exampleViewContoller *viewController;
example1ViewController *viewController1;
example2ViewController *viewController2;
example3ViewController *viewController3;
example4ViewController *viewController4;
NSMutableData *responseData;
NSMutableArray *tweets;
MBProgressHUD *HUD;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet UITabBarController *rootController;
#property (nonatomic, retain) IBOutlet exampleViewContoller *viewController;
#property (nonatomic, retain) IBOutlet example1ViewController *viewController1;
#property (nonatomic, retain) IBOutlet example2ViewController *viewController2;
#property (nonatomic, retain) IBOutlet example3ViewController *viewController3;
#property (nonatomic, retain) IBOutlet example4ViewController *viewController4;
#property (nonatomic, retain) NSMutableArray *tweets;
#end
AppDelegate.m:
#import "<appname>AppDelegate.h"
#import "exampleViewContoller.h"
#import "example1ViewController.h"
#import "example2ViewController.h"
#import "example3ViewController.h"
#import "example4ViewController.h"
#import "SBJson.h"
#define TMP NSTemporaryDirectory()
#implementation <appname>AppDelegate
#synthesize window = _window;
#synthesize rootController;
#synthesize viewController;
#synthesize viewController1;
#synthesize viewController2;
#synthesize viewController3;
#synthesize viewController4;
#synthesize tweets;
#pragma mark -
#pragma mark Application lifecycle
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
CGFloat width = self.rootController.view.bounds.size.width;
CGFloat height = self.rootController.view.bounds.size.height;
UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, width, height)];
UIImage *imageView = [UIImage imageNamed:#"TabBarBackground.png"];
UIColor *kMainColor = [[UIColor alloc] initWithPatternImage:imageView];
[v setBackgroundColor:kMainColor];
[kMainColor release];
[self.rootController.tabBar insertSubview:v atIndex:0];
[imageView release];
[v release];
responseData = [[NSMutableData data] retain];
tweets = [NSMutableArray array];
NSURLRequest *request = [NSURLRequest requestWithURL:
[NSURL URLWithString:#"http://api.twitter.com/1/statuses/user_timeline.json?screen_name=ENTER_USER_HERE&count=20"]];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
NSAssert(nil != self.rootController, #"tab bar controller not hooked up!");
BOOL iPad = NO;
#ifdef UI_USER_INTERFACE_IDIOM
iPad = (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad);
#endif
if (iPad) {
self.viewController = [[[exampleViewContoller alloc] initWithNibName:#"exampleViewController_iPad" bundle:nil] autorelease];
self.viewController1 = [[example1ViewController alloc] initWithNibName:#"example1ViewController_iPad" bundle:nil] autorelease];
self.viewController2 = [[[example2ViewController alloc] initWithNibName:#"example2ViewController_iPad" bundle:nil] autorelease];
self.viewController3 = [[[example3ViewController alloc] initWithNibName:#"example3ViewController_iPad" bundle:nil] autorelease];
self.viewController4 = [[[example4ViewController alloc] initWithNibName:#"example4ViewController_iPad" bundle:nil] autorelease];
} else {
self.viewController = [[[exampleViewContoller alloc] initWithNibName:#"exampleViewContoller_iPhone" bundle:nil] autorelease];
self.viewController1 = [[[example1ViewController alloc] initWithNibName:#"example1ViewController_iPhone" bundle:nil] autorelease];
self.viewController2 = [[[example2ViewController alloc] initWithNibName:#"example2ViewController2_iPhone" bundle:nil] autorelease];
self.viewController3 = [[[example3ViewController alloc] initWithNibName:#"example3ViewController_iPhone" bundle:nil] autorelease];
self.viewController4 = [[[example4ViewController alloc] initWithNibName:#"example4ViewController_iPhone" bundle:nil] autorelease];
}
self.rootController.viewControllers = [NSArray arrayWithObjects:self.viewController, self.viewController4, self.viewController1, self.viewController3, self.viewController2, nil];
[viewController release];
[viewController1 release];
[viewController2 release];
[viewController3 release];
[viewController4 release];
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_4_0
self.window.rootViewController = self.rootController;
#else
[self.window addSubview:rootController.view];
#endif
[self.window makeKeyAndVisible];
HUD = [[MBProgressHUD alloc] initWithView:viewController.view];
[viewController.view addSubview:HUD];
[HUD show:NO];
HUD.delegate = self;
HUD.labelText = #"Loading";
return YES;
}
//[---CODE CLIP---]
- (void)application:(UIApplication *)application didChangeStatusBarOrientation:(UIInterfaceOrientation)oldStatusBarOrientation {
CGFloat width = self.rootController.view.bounds.size.width*2;
CGFloat height = self.rootController.view.bounds.size.height;
UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, width, height)];
UIImage *imageView = [UIImage imageNamed:#"TabBarBackground.png"];
UIColor *kMainColor = [[UIColor alloc] initWithPatternImage:imageView];
[v setBackgroundColor:kMainColor];
[kMainColor release];
[self.rootController.tabBar insertSubview:v atIndex:0];
[imageView release];
[v release];
}
- (void)hudWasHidden {
[HUD removeFromSuperview];
}
//[---CODE CLIP---]
- (void)dealloc
{
[_window release];
[rootController release];
[HUD release];
[super dealloc];
}
#end
The problem is that when I rotate the device in iOS Simulator, the application won't rotate. Any ideas would be much appreciated!
UPDATE
I have also noticed that the launch image is also not rotating (for iPad that is - iPhone doesn't do landscape launch images).
NOTE FOR JMANS
I overrode UITabBarController:
#implementation UITabBarController (MyApp)
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
if(self.selectedIndex==4)
return (toInterfaceOrientation == UIInterfaceOrientationPortrait);
else
return (toInterfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
#end
I would start by including this in all of your view controllers:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}
Also, make sure that you are supporting multiple orientations in your Info.plist.
Tab Bar Controllers and View Rotation
Tab bar controllers support a portrait orientation by default and do not rotate to a landscape orientation unless all of the contained view controllers support such an orientation. When a device orientation change occurs, the tab bar controller queries its array of view controllers. If any one of them does not support the orientation, the tab bar controller does not change its orientation.

change view's property from method of other class

I'm trying to remotely adjust an object myImageView's alphato 0 of MyViewControllerclass From Another class Assistant(it's a NSObject subclass).
in Assistant.h
#import <Foundation/Foundation.h>
#class MyViewController;
#interface Assistant : NSObject {
MyViewController *myViewController;
UIImageView *button;
}
- (void)adjustAlpha:(id)sender;
#property (nonatomic, retain) UIImageView *button;
in Assistant.m
#import "MyViewController.h"
#implementation Assistant
#synthesize button;
- (id)init
{
self = [super init];
if (self)
{
myViewController = [[MyViewController alloc] init];
NSLog(#"alloced?: %#", myViewController ? #"YES" : #"NO");
}
return self;
}
- (void)adjustAlpha:(id)sender
{
NSLog(#"method called");
myViewController.myImageView.alpha = 0;
}
the method did get called, But myViewController.myImageView.alpha didn't change, why? where i need to fix? thank you for reading ^_^
Edit
This is MyViewController class:
MyViewController.h
#class Assistant;
#class MyAppAppDelegate;
#interface MyViewController : UIViewController <UITextViewDelegate, UIScrollViewDelegate>
{
UIImageView *myImageView;
Assistant *assistant;
}
#property (nonatomic, retain) UIImageView *myImageView;
MyViewController.m
#import "MyViewController.h"
#import "Assistant.h"
#implementation MyViewController
#synthesize myImageView;
- (void)viewDidLoad
{
[super viewDidLoad];
assistant = [[Assistant alloc] init];
myScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0,0,320,460);
[self.view addSubview: myScrollView];
myImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"weather.png"]];
myImageView.frame = CGRectMake(19, 54, 48, 48);
myImageView.userInteractionEnabled = YES;
[myScrollView addSubview:myImageView];
//this button uses to call adjustAlpha
assistant.button = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"button.png"]];
assistant.button.frame = CGRectMake(19, 126, 48, 48);
assistant.button.userInteractionEnabled = YES;
[myScrollView addSubview:assistant.button];
//here is how i call adjustAlpha:
assistant.adjustAlphaTap = [[UITapGestureRecognizer alloc] initWithTarget:assistant action:#selector(adjustAlpha:)];
[assistant.button addGestureRecognizer:assistant.adjustAlphaTap];
}
this question got complex and need some experiments, so just hanging there, will make it clear when i have time. for developer met the same problems: declare the object you need to access in a singleton class, can be easily accessed from any other classes, and Merry Christmas!
This will only work if myImageView is declared as a global scope variable (also called an iVar) in the .h file of your MyViewController class AND that it has a property of (nonatomic, retain) with a matching #synthesize in the .m.
If this sounds like what you're doing then can you please post the contents of your MyViewController h and m files.
If you have everything defined like Thomas described, can you check if the adjustAlpha method is called on the main thread?

UITabBarController rotation problem with popViewControllerAnimated and selectedIndex (iPhone SDK)

This is a very important auto rotate issue and easy to reproduce.
My application has a UITabBarController. Each tab is a UINavigationController. Auto rotation is handled with normal calls to shouldAutorotateToInterfaceOrientation and didRotateFromInterfaceOrientation.
The interface rotates normally until I call UIViewController.popViewControllerAnimated and change UITabBarController.selectedIndex.
Steps to reproduce:
Create a demo Tab Bar Application.
Add the following code to the App Delegate .h file:#import <UIKit/UIKit.h>
#interface TestRotationAppDelegate : NSObject {
UIWindow *window;
UITabBarController *tabBarController;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet UITabBarController *tabBarController;
#end
// Redefine the interface to cach rotation messages
#interface UITabBarController (TestRotation1AppDelegate)
#end
Add the following code to the App Delegate .m file:#import "TestRotationAppDelegate.h"
#implementation TestRotationAppDelegate
#synthesize window;
#synthesize tabBarController;
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[window addSubview:tabBarController.view];
[window makeKeyAndVisible];
return YES;
}
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}
-(void)dealloc {
[tabBarController release];
[window release];
[super dealloc];
}
#end
#implementation UITabBarController (TestRotation1AppDelegate)
-(void)viewDidLoad {
[super viewDidLoad];
// Add a third tab and push a view
UIViewController *view1 = [[[UIViewController alloc] init] autorelease];
view1.title = #"Third";
UINavigationController *nav = [[[UINavigationController alloc] initWithRootViewController:view1] autorelease];
NSMutableArray *array = [[NSMutableArray alloc] init];
[array addObjectsFromArray:self.viewControllers];
[array addObject:nav];
self.viewControllers = array;
// Push view2 inside the third tab
UIViewController *view2 = [[[UIViewController alloc] init] autorelease];
[nav pushViewController:view2 animated:YES];
// Create a button to pop view2
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(50, 50, 220, 38);
[button setTitle:#"Pop this view" forState:UIControlStateNormal];
[button addTarget:self action:#selector(doAction) forControlEvents:UIControlEventTouchUpInside];
[view2.view addSubview:button];
}
-(void) doAction {
// ROTATION PROBLEM BEGINS HERE
// Remove one line of code and the problem doesn't occur.
[self.selectedViewController popViewControllerAnimated:YES];
self.selectedIndex = 0;
}
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}
#end
The interface auto rotates normally until you tap the button on tab #3.
Your help will be geatly appreciated!
iPhone SDK 3.2 solves this issue.
With previous SDK use [self.selectedViewController popViewControllerAnimated:NO].

Trying to get a UILabel to Show in a View, without a NIB

I'm new to iPhone programming, and I'm trying to make a simple program without a NIB. I have worked through some NIB tutorials, but I'd like to try some things programmatically.
My code loads without errors, makes the status bar black, and makes the background white. But, I don't think I'm loading my view with a label correctly after that. I presume I'm doing something just fundamentally wrong, so if you could point me in the right direction, I'd appreciate it. I think if I can get the label to show, I'll get some understanding. Here's my code:
//helloUAppDelegate.h
#import <UIKit/UIKit.h>
#import "LocalViewController.h"
#interface helloUAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
LocalViewController *localViewController;
}
#property (nonatomic, retain) UIWindow *window;
#property (nonatomic, retain) LocalViewController *localViewController;
#end
//helloUApDelegate.m
#import "helloUAppDelegate.h"
#implementation helloUAppDelegate
#synthesize window, localViewController;
- (void)applicationDidFinishLaunching:(UIApplication *)application {
application.statusBarStyle = UIStatusBarStyleBlackOpaque;
window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
if (!window) {
[self release];
return;
}
window.backgroundColor = [UIColor whiteColor];
localViewController = [[LocalViewController alloc] init];
[window addSubview:localViewController.view];
// Override point for customization after application launch
[window makeKeyAndVisible];
}
//LocalViewController.h
#import <UIKit/UIKit.h>
#interface LocalViewController : UIViewController {
UILabel *myLabel;
}
#property (nonatomic, retain) UILabel *myLabel;
#end
//LocalViewController.m
#import "LocalViewController.h"
#implementation LocalViewController
#synthesize myLabel;
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
self.myLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 100, 200, 100)];
self.myLabel.text = #"Lorem...";
self.myLabel.textColor = [UIColor redColor];
}
- (void)dealloc {
[super dealloc];
[myLabel release];
}
Add your label to your LocalViewController's view:
- (void)loadView {
[super loadView];
self.myLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 100, 200, 100)];
self.myLabel.text = #"Lorem...";
self.myLabel.textColor = [UIColor redColor];
[self addSubview:self.myLabel];
[self.myLabel release]; // since it's retained after being added to the view
}