Having trouble to addsubview on viewdidload - iphone

inside the UIViewController, the tempview2 is not showing up at all. only tempview is showing up.
-(id) init:(NSData*) imageData
{
if ((self = [super init])) {
tempimage= [[[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"pic1" ofType:#"png"]] autorelease];
tempview=[[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)] autorelease];
[tempview setImage:tempimage];
[self.view addsubview tempview];
}
return self;
}
-(void) viewdidload{
tempimage2= [[[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"pic2" ofType:#"png"]] autorelease];
tempview2=[[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)] autorelease];
[tempview2 setImage:tempimage2];
[self.view addsubview tempview2];
}
if I do this then everything is OK,
-(id) init:(NSData*) imageData
{
if ((self = [super init])) {
tempimage= [[[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"pic1" ofType:#"png"]] autorelease];
tempview=[[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)] autorelease];
[tempview setImage:tempimage];
[self.view addsubview tempview];
tempimage2= [[[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"pic2" ofType:#"png"]] autorelease];
tempview2=[[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)] autorelease];
[tempview2 setImage:tempimage2];
[self.view addsubview tempview2];
}
return self;
}

This is not a direct answer to your question but a good practice of viewcontroller initialization and view loading in general:
In the init method you should only initialize ivars or other class data which does not pertain to the view. Doing otherwise breaks the notion of lazy loading. During the init method of you view controller the view does not exist yet.
In the viewDidLoad method you should perform your view setup. At this stage the view is actually loaded and it is the time to set it up properly.
I would encourage you to therefore move your view setup calls into the viewDidLoad and see if the problem persists or not.
Good luck.

I agree with MiKL.
But anyway getting back to your question did you forget to call [super viewDidLoad]? Maybe that's the issue here?
Also, as a best practice, release views after adding them as subviews to something else (adding a view as a subview to another view increases it's retain count).

Related

memory leak in singleton class

I am new to iPhone development.
I am developing an application in which I am using Single tone class.
When I am creating an object of single tone class it is giving me memory leak on analyzing my code. It is giving message as "Potential leak of an object" and "Allocated object is not referenced later". But I am using that object in my code.
following is my code where I have created single tone class object
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(#"Inside View");
self.navigationController.navigationBar.topItem.title = #"Menu List";
UIImage *image = [UIImage imageNamed:#"Navigation_bar.png"];
[_bgImage setFrame:CGRectMake(0,-45,320,510)];
[self.navigationController.navigationBar setBackgroundImage:image
forBarMetrics:UIBarMetricsDefault];
[self.tabBarController.tabBar setBackgroundImage:[UIImage imageNamed:#"Tab_bar.png"]];
[self.navigationItem setHidesBackButton:YES];
menuTableView.backgroundColor=[UIColor clearColor];
menuTableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
_hotelMenu=[SharedHotelMenu sharedInstanceMethod];
_queryFormatter=[[DatabaseQueryFormatter alloc]init];
_isSearchOn=NO;
_searchResult=[[NSMutableArray alloc]init];
_categorySearch.layer.cornerRadius = 19;
_categorySearch.clipsToBounds = YES;
_categorySearch.delegate=self;
UIView *_paddingView=[[UIView alloc] initWithFrame:CGRectMake(0,0,5,10)];
_categorySearch.leftView=_paddingView;
_categorySearch.leftViewMode=UITextFieldViewModeAlways;
[_paddingView release];
UIView *_paddingRightView=[[UIView alloc] initWithFrame:CGRectMake(0,0,30,10)];
_categorySearch.rightView=_paddingRightView;
_categorySearch.rightViewMode=UITextFieldViewModeAlways;
[_paddingRightView release];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(searchBar)
name:UITextFieldTextDidChangeNotification object:_categorySearch];
}
}
I have created single tone class object as _hotelMenu=[SharedHotelMenu sharedInstanceMethod];
As far as I can see in your code is that this line may cause memory leak
menuTableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
As this is #property(nonatomic, retain) UIView *tableFooterView and it will retain your object and thus retain count becomes 2.
Use this
UIView *footerView = [[UIView alloc] initWithFrame:CGRectZero];
menuTableView.tableFooterView = footerView;
[footerView release];

'UITabBarItem' may not respond to 'SetAction'

I have a number of tabs in my app. The tabs are working with no issues, but I'm getting some warning messages (in the title above) which I would like to get rid of. My code is as follows:
-(void)pressItem1:(id)sender {
[self presentModalViewController:settingsViewController animated:YES];
}
-(void)pressItem2:(id)sender {
[self presentModalViewController:infoViewController animated:YES];
}
-(void)pressItem3:(id)sender {
[self presentModalViewController:aboutViewController animated:YES];
}
-(void)viewDidLoad {
self.view.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:#"background.png"]];
CGRect frame = CGRectMake(0, 0, 480, 49);
UIView *v = [[UIView alloc] initWithFrame:frame];
UIImage *i = [UIImage imageNamed:#"smallMenuBackground.png"];
UIColor *c = [[UIColor alloc] initWithPatternImage:i];
v.backgroundColor = c;
[c release];
[mainTabBar insertSubview:v atIndex:0];
[v release];
[settingsBarItem setAction:#selector(pressItem1:)];
[infoBarItem setAction:#selector(pressItem2:)];
[aboutBarItem setAction:#selector(pressItem3:)];
//initialSyncSwitch = NO;
[super viewDidLoad];
}
The tabs are working, but there is probably a better way of doing it so I don't get these warnings.
Regards,
Stephen
You don't set actions directly on a UITabBarItem. Instead, you should be implementing the UITabBarDelegate in the UIViewController that creates it. Specifically, the delegate should implement:
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
In here, you can call pressItem1, pressItem2, etc based on which item is passed.

How to add two uiscrollviews as subview

After lot of research and working for days, I really need some guidance here. I am trying to add uiscrollview on top of each other. I have two UIScrollViews: ImageScrollView and MarkScrollView. I want to add MarkScrollView as subview to ImageScrollView. ImageScrollView is added to UIViewController MapImageViewController.
In ImageScrollView it consists of images which are in tiledLayer. In MarkScrollView, it consists of imageview which has a pointer image. But for some reason it cannot be added as subview to other view. I have added my code. If anybody can help me to figure out the error it will be really appreciated.
/** ImageScrollView.m It adds uiview on top of ImageScrollView***/
UIView *imageView = [[TileView alloc] initWithImageName:imageName size:imageSize];
[(TileView *)imageView setAnnotates:NO]; // ** remove this line to remove the white tile grid **
[self addSubview:imageView];
/** MarkScrollView.m It adds uiview with uiimages on top of MarkScrollView**/
UIImage *markerimage = [UIImage imageNamed:#"map.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage: markerimage];
imageView.image = markerimage;
[imageView setFrame:CGRectMake(100,100,50,50)];
imageview = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)] autorelease];
[imageview setBackgroundColor:[UIColor blackColor]];
[imageview addSubview: imageView];
[self addSubview:imageview];
/** MapImageViewController.m It adds ImageScrollView on top of another UIScrollView pagingScrollView **/
UIScrollView *pagingScrollView;
ImageScrollView *page = [[[ImageScrollView alloc] init] autorelease];
[pagingScrollView addSubview:page];
MarkScrollView *markerScroll = [[[MarkScrollView alloc] init] autorelease];
[page addSubview:markerScroll]; /**But it never adds the other scrollview here **/
/** MapImageViewController.h **/
#interface MapImageViewController : UIViewController <UIScrollViewDelegate, UIGestureRecognizerDelegate> {
ImageScrollingView *imageScrollView;
MarkerIconsScrollView *markerScrollView;
}
UIScrollView *pagingScrollView;
ImageScrollView *page = [[[ImageScrollView alloc] init] autorelease];
[pagingScrollView addSubview:page];
MarkScrollView *markerScroll = [[[MarkScrollView alloc] init] autorelease];
[page addSubview:markerScroll]; /**But it never adds the other scrollview here **/
I'm noticing some things on this code snippet:
pagingScrollView is never initialized
the views that you are adding to it (provided that it actually is initialized with a valid frame), are not initialized with a frame. Always use - (id)initWithFrame:(CGRect)aRect to initialize your UIViews
Good luck!

Splash Screen: EXC_BAD_ACCESS after calling dismissModalViewControllerAnimated delayed

I am trying to create a 1-2 second splashcreen for my application using a modal view controller however when i try to dismiss the view my application crashes with a bad access error. So in my application delegate i have:
- (BOOL) application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//create window and show it
window = [[UIWindow alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
window.backgroundColor = [UIColor greenColor];
[window makeKeyAndVisible];
//create the main view controller and add its view to the window
mainViewCtrl = [MainViewController alloc];
[window addSubview:mainViewCtrl.view];
//show splash screen
UIImage *image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"default.png" ofType:nil]];
splashViewCtrl = [[UIViewController alloc] init];
splashViewCtrl.view = [[UIImageView alloc] initWithImage:image];
[mainViewCtrl presentModalViewController:splashViewCtrl animated:NO];
//setup callback to dismiss
[self performSelector:#selector(hideSplash) withObject:nil afterDelay:2.0];
return(true);
}
//hide splash screen callback
- (void)hideSplash {
[[self mainViewCtrl] dismissModalViewControllerAnimated:YES];
}
And this all works perfectly fine except when the hideSplash is called after 2 seconds the application crashes with a EXC_BAD_ACCESS. If i comment out the perform selector line and call the hidesplash immediately after like so:
[mainViewCtrl presentModalViewController:splashViewCtrl animated:NO];
[self hideSplash];
The modal view is properly dismissed. I'm fairly sure this is a memory management problem but i'm not sure exacty what i'm doing wrong here. Does anyone have any ideas of what this could be or how to properly debug this so i can delay the dismissal?
Thanks
This looks weird:
mainViewCtrl = [MainViewController alloc];
Try adding the initialisation call to it.
//show splash screen
UIImage *image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"default.png" ofType:nil]];
splashViewCtrl = [[UIViewController alloc] init];
splashViewCtrl.view = [[UIImageView alloc] initWithImage:image];
[mainViewCtrl presentModalViewController:splashViewCtrl animated:NO];
Change the above to this below:
//show splash screen
UIImage *image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"default.png" ofType:nil]];
splashViewCtrl = [[UIViewController alloc] init];
splashViewCtrl.view = [[UIImageView alloc] initWithImage:image];
[mainViewCtrl presentModalViewController:splashViewCtrl animated:NO];
[mainViewCtrl release]; //Add this line !!!!

UIgestureRecognizer in a view inside a UIScrollView

Has anyone managed to get a UIGestureRecognizer to work on a UIView that is a subview of a UIScrollView? My callbacks never seems to get called.
As a simple example, I want to have a paging scrollview and on the third page listen for a tap with a UITapGestureRecognizer. However I can not get it to work.
Here's how I would do it:
self.scrollView = [[[UIScrollView alloc] initWithFrame:self.view.frame] autorelease];
self.scrollView.pagingEnabled = YES;
self.scrollView.contentSize = CGSizeMake(self.section1ScrollView.frame.size.width * 3, self.scrollView.frame.size.height); //3 pages
UIImageView *p0 = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"page0.png"]] autorelease];
[self.scrollView insertSubview:p0 atIndex:self.scrollView.subviews.count];
UIImageView *p1 = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"page1.png"]] autorelease];
//code to move it to the next page
[self.scrollView insertSubview:p1 atIndex:self.scrollView.subviews.count];
UIImageView *p2 = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"page2.png"]] autorelease];
//code to move it to the next page
[self.scrollView insertSubview:p2 atIndex:self.scrollView.subviews.count];
UITapGestureRecognizer *p2TapRegocnizer = [[[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(p2Tapped:)] autorelease];
//p2TapRegocnizer.delegate = self;
[p2 addGestureRecognizer:p2TapRegocnizer];
UIImageView has in default userInteractionEnabled set to NO. I would try to change it to YES.
[webView setUserInteractionEnabled:YES]