I am working on an app which uses a UIScrollView to scroll through a bunch of slides. When the app opens, it creates the slides and passes them to the scroll view. My app also has a timer which causes the UIScrollView to scroll through the slides.
I also have a settings button. When I press on that button, it opens the settings panel. The timer is invalidated and set to nil. When the settings panel opens, the user can change things like the application theme, the slides and the animation direction.
When the settings panel is closed, the settings are applied: The slides are removed from the UIScrollView and the UIScrollView is given a new NSArray of slides to work with. The UIScrollView then lays out the new slides in the proper order and direction. The timer is reset.
Once, approximately every 14-16 times, the app crashes while running dismissAndRestart (the "close" code). I'm not sure why. I thought I was leaking memory, and apparently I am, but I don't see where.
Here's my code:
This runs on startup:
- (void)viewDidLoad {
[scrollView setPagingEnabled:YES];
}
- (void) viewWillAppear:(BOOL)animated{
[self prepareViews];
[self applyTheme];
}
- (void) viewDidAppear:(BOOL)animated{
[self createTimerWithInterval:kScrollInterval];
}
Workhorse method that loads the views into the scroller. (It also removes the older ones):
- (void)loadViews:(NSArray *)views IntoScroller:(UIScrollView *)scroller withDirection:(NSString *)direction{
[scrollView.subviews makeObjectsPerformSelector:#selector(removeFromSuperview)];
[scrollView setShowsHorizontalScrollIndicator: NO];
[scrollView setShowsVerticalScrollIndicator:NO];
scrollView.scrollsToTop = NO;
if([direction isEqualToString:#"horizontal"]){
scrollView.frame = CGRectMake(0, 0, 1024, 768);
scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * [[NSNumber numberWithUnsignedInt:[views count]] floatValue], scrollView.frame.size.height);
}else if([direction isEqualToString:#"vertical"]){
scrollView.frame = CGRectMake(0, 0, 1024, 768);
scrollView.contentSize = CGSizeMake(scrollView.frame.size.width, scrollView.frame.size.height * [[NSNumber numberWithUnsignedInt:[views count]] floatValue]);
}
for (int i=0; i<[[NSNumber numberWithUnsignedInt:[views count]] intValue]; i++) {
[[[views objectAtIndex:[[NSNumber numberWithInt:i] unsignedIntValue]] view] setFrame:scrollView.frame];
if([direction isEqualToString:#"horizontal"]){
[[[views objectAtIndex:[[NSNumber numberWithInt:i] unsignedIntValue]] view] setFrame:CGRectMake(i * [[views objectAtIndex:[[NSNumber numberWithInt:i] unsignedIntValue]] view].frame.size.width, 0, scrollView.frame.size.width, scrollView.frame.size.height)];//CGRectMake(i * announcementView.view.frame.size.width, -scrollView.frame.origin.x, scrollView.frame.size.width, scrollView.frame.size.height)];
}else if([direction isEqualToString:#"vertical"]){
[[[views objectAtIndex:[[NSNumber numberWithInt:i] unsignedIntValue]] view] setFrame:CGRectMake(0, i * [[views objectAtIndex:[[NSNumber numberWithInt:i] unsignedIntValue]] view].frame.size.height, scrollView.frame.size.width, scrollView.frame.size.height)];
}
[scrollView addSubview:[[views objectAtIndex:[[NSNumber numberWithInt:i] unsignedIntValue]] view]];
}
}
Create the timer for automated scrolling:
#pragma mark -
#pragma mark Create the Timer to automate scrolling
- (void) createTimerWithInterval:(float)interval{
self.timer = [NSTimer scheduledTimerWithTimeInterval:interval target:self selector:#selector(scrollWrapperForTimer) userInfo:nil repeats:YES];
}
Wrapper function for the timer function. (This was necessary before the direction was an NSUserDefault, now I shouldn't need this.)
#pragma mark -
#pragma mark Scroll Automatically Every N seconds
- (void) scrollWrapperForTimer{
[self scrollToNewViewInDirection:kDirection];
}
- (void)scrollToNewViewInDirection:(NSString *)direction{
if([[NSUserDefaults standardUserDefaults] boolForKey:#"animated_scrolling"] == YES){
if([direction isEqualToString:#"horizontal"]){
if([[NSNumber numberWithFloat:scrollView.contentOffset.x] compare:[NSNumber numberWithFloat:scrollView.contentSize.width - scrollView.frame.size.width]] == (NSOrderedAscending)){
[scrollView scrollRectToVisible:CGRectMake(scrollView.contentOffset.x + scrollView.frame.size.width, 0, scrollView.frame.size.width, scrollView.frame.size.height) animated:YES];
}else if([[NSNumber numberWithFloat:scrollView.contentOffset.x] compare:[NSNumber numberWithFloat:scrollView.contentSize.width - scrollView.frame.size.width]] == (NSOrderedSame)){
[scrollView scrollRectToVisible:CGRectMake(0, 0, scrollView.frame.size.width, scrollView.frame.size.height) animated:YES];
}
}else if([direction isEqualToString:#"vertical"]){
if([[NSNumber numberWithFloat:scrollView.contentOffset.y] compare:[NSNumber numberWithFloat:scrollView.contentSize.height - scrollView.frame.size.height]] == (NSOrderedAscending)){
[scrollView scrollRectToVisible:CGRectMake(0, scrollView.contentOffset.y + scrollView.frame.size.height, scrollView.frame.size.width, scrollView.frame.size.height) animated:YES];
}else if([[NSNumber numberWithFloat:scrollView.contentOffset.y] compare:[NSNumber numberWithFloat:scrollView.contentSize.height - scrollView.frame.size.height]] == (NSOrderedSame)){
[scrollView scrollRectToVisible:CGRectMake(0, 0, scrollView.frame.size.width, scrollView.frame.size.height) animated:YES];
}
}
}else {
if([direction isEqualToString:#"horizontal"]){
if([[NSNumber numberWithFloat:scrollView.contentOffset.x] compare:[NSNumber numberWithFloat:scrollView.contentSize.width - scrollView.frame.size.width]] == (NSOrderedAscending)){
[scrollView scrollRectToVisible:CGRectMake(scrollView.contentOffset.x + scrollView.frame.size.width, 0, scrollView.frame.size.width, scrollView.frame.size.height) animated:NO];
}else if([[NSNumber numberWithFloat:scrollView.contentOffset.x] compare:[NSNumber numberWithFloat:scrollView.contentSize.width - scrollView.frame.size.width]] == (NSOrderedSame)){
[scrollView scrollRectToVisible:CGRectMake(0, 0, scrollView.frame.size.width, scrollView.frame.size.height) animated:NO];
}
}else if([direction isEqualToString:#"vertical"]){
if([[NSNumber numberWithFloat:scrollView.contentOffset.y] compare:[NSNumber numberWithFloat:scrollView.contentSize.height - scrollView.frame.size.height]] == (NSOrderedAscending)){
[scrollView scrollRectToVisible:CGRectMake(0, scrollView.contentOffset.y + scrollView.frame.size.height, scrollView.frame.size.width, scrollView.frame.size.height) animated:NO];
}else if([[NSNumber numberWithFloat:scrollView.contentOffset.y] compare:[NSNumber numberWithFloat:scrollView.contentSize.height - scrollView.frame.size.height]] == (NSOrderedSame)){
[scrollView scrollRectToVisible:CGRectMake(0, 0, scrollView.frame.size.width, scrollView.frame.size.height) animated:NO];
}
}
}
}
This method gets called when the done button is pressed in the setting panel.
#pragma mark -
#pragma mark Restart the program
- (void) dismissAndRestart{
[self prepareViews];
[self applyTheme];
[self dismissModalViewControllerAnimated:YES];
[self createTimerWithInterval:kScrollInterval];
}
This creates the proper image files and loads them into place:
#pragma mark -
#pragma mark Apply the theme to the main view
- (void) applyTheme{
//Front panel
UIImage *frontImage = [[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[[NSString stringWithFormat:#"%#_front", kTheme]description] ofType:#"png"]];
[self.overlayImage setImage:frontImage];
[frontImage release];
//Back Panel
if([kTheme isEqualToString:#"walnut"]){
[self.backgroundImage setHidden:NO];
UIImage *backImage = [[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[[NSString stringWithFormat:#"%#_back", kTheme]description] ofType:#"png"]];
[self.backgroundImage setImage:backImage];
[backImage release];
}else if([kTheme isEqualToString:#"metal"]){
[self.backgroundImage setHidden:YES];
[self.view setBackgroundColor: [UIColor scrollViewTexturedBackgroundColor]];
}
//Gabbai Button
UIImage *gabbaiImage = [[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[[NSString stringWithFormat:#"%#_settings_button", kTheme]description] ofType:#"png"]];
[self.gabbaiButton setImage:gabbaiImage forState:UIControlStateNormal];
[gabbaiImage release];
}
Another wrapper function that needs to be refactored:
#pragma mark -
#pragma mark Slide View related
- (void) prepareViews{
[self loadViews:[self createandReturnViews] IntoScroller:scrollView withDirection:kDirection];
}
Creates the Array of slides to pass to the scroller:
//recreation of the views causes a delay each time the admin panel is closed.
- (NSArray*) createandReturnViews{
NSMutableArray *announcements = [NSMutableArray array];
NSArray *announcementsArray = [NSArray arrayWithObjects: #"Welcome to\nGabbai HD!",
#"First slide",
#"Second Slide",
#"Third Slide",
#"etc.",
nil];
for (int i = 0; i<[[NSNumber numberWithUnsignedInteger:[announcementsArray count]] intValue]; i++) {
MBAnnouncementViewController *announcement = [[MBAnnouncementViewController alloc] initWithNibName:#"MBAnnouncementViewController" bundle:nil];
[announcement setAnnouncementText:[announcementsArray objectAtIndex:[[NSNumber numberWithInt:i] unsignedIntegerValue]]];
[announcements addObject:announcement];
[announcement release];
}
return announcements;
}
Why would my settings panel be crashing the app on close?
The code you show has a few alloc calls. After each one, you set a property, and release. I assume that the property is declared as retain.
If so, then eventually, you need to release these properties. Usually in the dealloc message implementation.
Your classes with retained properties need a dealloc like this
-(void) dealloc() {
self.prop = nil; // calls release
self.prop2 = nil;
[super dealloc];
}
If your properties are declared like this
#property (nonatomic, retain) Type* prop;
Then when you reassign the property with either the set message or self.prop syntax, the previous value has release called on it. If you just use prop = newVal;, then you are accessing the field directly and not calling the set message, so release will not be called.
Generally, always use the set message or dot syntax, so that you don't need to worry about it -- it's the main reason you declare the property as retain, so take advantage of it.
I haven't read your code, but it could be that you are trying to release an object that has already been released. I would recommend that you turn on zombies when developing, but rembember to turn this off when compiling the release version, because when zombies is enabled, memory is not released. Really helpful while debugging, if you are code is trying to access an object that does not exist anymore.
Google "enable zombies xcode" or something like that
You can also use xcode analyser using window+shift+a to find out memory leakage.
Related
Ok so I have a app with a left menu and a right view controller which are controlled by the UIPanGestureRecognizer. So when a user swipes the bezel (20px) of the edge of the screen it navigates.
In a settings view controller I have UISwitches which control various other things, but one of them I want to control how many touches the UIPanGestureRecognizer will take.
The switch is set up properly and sends the user defaults to the other view controller, but on the receiving end of it, it won't read what the user has selected.
UIPanGestureRecognizer * pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(panGestureCallback:)];
//Switch for SwipeNav
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
BOOL switchOn = [userDefaults boolForKey:#"SwipeNav"];
if (switchOn) {
// 2 Finger SwipeNav
[pan setMinimumNumberOfTouches:2];
[pan setMaximumNumberOfTouches:2];
}else{
[pan setMinimumNumberOfTouches:1];
[pan setMaximumNumberOfTouches:1];
}
[pan setDelegate:self];
[self.view addGestureRecognizer:pan];
How can I make it so when the switch is on, it uses 2 touches to navigate, and off it acts like normal?
Any assistance is always appreciated. Thank you.
UPDATED with settings sync code.
if (swipe){
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setBool:self.swipe.on forKey:#"SwipeNav"];
[userDefaults synchronize];
if ([swipe isOn]) {
[swipe setOn:YES animated:YES];
[self.tableView reloadData];
[[UIApplication sharedApplication] reloadInputViews];
} else {
[swipe setOn:NO animated:YES];
[self.tableView reloadData];
[[UIApplication sharedApplication] reloadInputViews];
}
}
I created a new project and tried to reproduce the problem that you are seeing and everything seems to work as expected. When switch is on the you need two buttons to pan and when switch is off you only need one. Let me know if that points you in the right direction. Your code seems to be correct.
I copy pasted the handlePan function from How to use UIPanGestureRecognizer to move object? iPhone/iPad
Created everything programmatically and copy pasted your code to save the state of the switch. Here is the code from two UIViewControllers
//
// ViewController.m
// StackOverflowSwitch
//
//
//
//
#import "ViewController.h"
#interface ViewController ()
#property (nonatomic,strong) UIButton *myButton;
#property (nonatomic,strong) UISwitch *switchOn;
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.myButton = [[UIButton alloc]initWithFrame:CGRectMake(10, 100, 150, 150)];
self.switchOn = [[UISwitch alloc]initWithFrame:CGRectMake(10, 300, 100, 100)];
self.myButton.backgroundColor =[UIColor grayColor];
[self.myButton setTitle:#"Click Me" forState:UIControlStateNormal];
[self.myButton addTarget:self action:#selector(click:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.switchOn];
[self.view addSubview:self.myButton];
// Do any additional setup after loading the view, typically from a nib.
}
-(void)click:(UIButton *)sender
{
[self performSegueWithIdentifier:#"NextViewController" sender:self];
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if(self.switchOn.on){
NSLog(#"switch is on");
}else{
NSLog(#"switch is off");
}
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setBool:self.switchOn.on forKey:#"SwipeNav"];
[userDefaults synchronize];
}
#end
//////
#import "SecondViewController.h"
#interface SecondViewController() <UIGestureRecognizerDelegate>
#property (strong,nonatomic) UIView *myView;
#end
#implementation SecondViewController
-(void)viewDidLoad
{
[super viewDidLoad];
self.myView = [[UIView alloc]initWithFrame:CGRectMake(10, 10, 200, 200)];
self.myView.backgroundColor = [UIColor redColor];
[self.view addSubview:self.myView];
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(handlePan:)];
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
BOOL switchOn = [userDefaults boolForKey:#"SwipeNav"];
if (switchOn) {
// 2 Finger SwipeNav
[pan setMinimumNumberOfTouches:2];
[pan setMaximumNumberOfTouches:2];
}else{
[pan setMinimumNumberOfTouches:1];
[pan setMaximumNumberOfTouches:1];
}
[pan setDelegate:self];
[self.view addGestureRecognizer:pan];
[self.myView addGestureRecognizer:pan];
}
-(void)handlePan:(UIPanGestureRecognizer *)recognizer
{
CGPoint translation = [recognizer translationInView:self.view];
recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x,
recognizer.view.center.y + translation.y);
[recognizer setTranslation:CGPointMake(0, 0) inView:self.view];
if (recognizer.state == UIGestureRecognizerStateEnded) {
CGPoint velocity = [recognizer velocityInView:self.view];
CGFloat magnitude = sqrtf((velocity.x * velocity.x) + (velocity.y * velocity.y));
CGFloat slideMult = magnitude / 200;
NSLog(#"magnitude: %f, slideMult: %f", magnitude, slideMult);
float slideFactor = 0.1 * slideMult; // Increase for more of a slide
CGPoint finalPoint = CGPointMake(recognizer.view.center.x + (velocity.x * slideFactor),
recognizer.view.center.y + (velocity.y * slideFactor));
finalPoint.x = MIN(MAX(finalPoint.x, 0), self.view.bounds.size.width);
finalPoint.y = MIN(MAX(finalPoint.y, 0), self.view.bounds.size.height);
[UIView animateWithDuration:slideFactor*2 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
recognizer.view.center = finalPoint;
} completion:nil];
}
}
#end
///// Update
I am not exactly sure how [[UIApplication sharedApplication] reloadInputViews]; calls the function to update the UIPanGestureRecongizer in your code but i tried to recreate your functionality as close as possible by having the setupView method called every time i click the UISwitch
I created a custom view with UIPanGestureReconginzer in there and a method that I call from the UIViewController to update setNumberOfTouches. The view works as expected. The key here is not to initialize a new UIPanGuestureRecongizer but update the existing one. I assume in your app UIViewController or UIView which has the UIPanGuestureRecongizer is not being fully reloaded.
I created a property UIPanGuestureRecongizer which gets initialized the first time it's being accessed and then gets updated when switch is being pressed. Here is the custom UIView code. Let me know if you have any questions regarding the code.
#import "MyTestView.h"
#interface MyTestView() <UIGestureRecognizerDelegate>
#property(nonatomic,strong) UIPanGestureRecognizer *panGesture;
#end
#implementation MyTestView
-(instancetype)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self) {
[self setupView];
}
return self;
}
-(UIPanGestureRecognizer *)panGesture
{
//lazy instantiation
if (!_panGesture) {
_panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(handlePan:)];
}
return _panGesture;
}
-(void)setupView
{
//this gets called everytime the UISwitch is being pressed.
NSLog(#"Setup views");
self.backgroundColor = [UIColor redColor];
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
BOOL switchOn = [userDefaults boolForKey:#"SwipeNav"];
if (switchOn) {
// 2 Finger SwipeNav
[self.panGesture setMinimumNumberOfTouches:2];
[self.panGesture setMaximumNumberOfTouches:2];
}else{
[self.panGesture setMinimumNumberOfTouches:1];
[self.panGesture setMaximumNumberOfTouches:1];
}
[self.panGesture setDelegate:self];
[self addGestureRecognizer:self.panGesture];
}
-(void)handlePan:(UIPanGestureRecognizer *)recognizer
{
CGPoint translation = [recognizer translationInView:self];
recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x,
recognizer.view.center.y + translation.y);
[recognizer setTranslation:CGPointMake(0, 0) inView:self];
if (recognizer.state == UIGestureRecognizerStateEnded) {
CGPoint velocity = [recognizer velocityInView:self];
CGFloat magnitude = sqrtf((velocity.x * velocity.x) + (velocity.y * velocity.y));
CGFloat slideMult = magnitude / 200;
NSLog(#"magnitude: %f, slideMult: %f", magnitude, slideMult);
float slideFactor = 0.1 * slideMult; // Increase for more of a slide
CGPoint finalPoint = CGPointMake(recognizer.view.center.x + (velocity.x * slideFactor),
recognizer.view.center.y + (velocity.y * slideFactor));
finalPoint.x = MIN(MAX(finalPoint.x, 0), self.bounds.size.width);
finalPoint.y = MIN(MAX(finalPoint.y, 0), self.bounds.size.height);
[UIView animateWithDuration:slideFactor*2 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
recognizer.view.center = finalPoint;
} completion:nil];
}
}
#end
Alright, so after much testing/code flipping, I think I have found my solution.
So a little more backstory, I use MMDrawerController, so the method above about placing [self setupView]; in (instancetype)initWithCoder didn't do anything. The setupView or in my case is called "setupGestureRecognizers" is loaded in the viewDidLoad. Anywhere else it wouldn't load the gesture's at all.
With my current switch code and gesture loading I have this:
-(UIPanGestureRecognizer *)panGestureNav {
if (!_panGesture) {
_panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(panGestureCallback:)];
}
return _panGesture;
}
-(void)setupGestureRecognizers{
switchOn = [[NSUserDefaults standardUserDefaults] boolForKey:#"SwipeNav"];
if (switchOn == YES) {
// 2 Finger SwipeNav
[self.panGestureNav setMinimumNumberOfTouches:2];
[self.panGestureNav setMaximumNumberOfTouches:2];
NSLog(#"2 Finger Nav");
}else {
[self.panGestureNav setMinimumNumberOfTouches:1];
[self.panGestureNav setMaximumNumberOfTouches:1];
NSLog(#"Normal Nav");
}
[self.panGestureNav setDelegate:self];
[self.view addGestureRecognizer:self.panGestureNav];
UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapGestureCallback:)];
[tap setDelegate:self];
[self.view addGestureRecognizer:tap];
}
Which would normally work, but this library does things differently, so I have to look into their gesture calling and what I ended up doing was calling it here:
-(MMOpenDrawerGestureMode)possibleOpenGestureModesForGestureRecognizer:(UIGestureRecognizer*)gestureRecognizer withTouch:(UITouch*)touch{
CGPoint point = [touch locationInView:self.childControllerContainerView];
MMOpenDrawerGestureMode possibleOpenGestureModes = MMOpenDrawerGestureModeNone;
if([gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]]){
[self setupGestureRecognizers]; <------------
So now it loads and functions as I want.
Thank you Yan so all of your assistance. Hope this helps somebody.
I have a page controller and that method pageAction defined like this :
-(void)pageAction:(UIPageControl*)control
{
NSLog(#"page changed");
[self getVehicules];
[self.tableViewVehiculesPossedes release];
int page = pageControlVehiculePossedee.currentPage;
NSLog(#"page %d", page);
[self loadScrollViewWithPage:page];
CGRect frame = pageControlVehiculePossedee.frame;
frame.origin.x = (vosvehiculeScrollView.frame.size.width * page);
[vosvehiculeScrollView scrollRectToVisible:frame animated:YES];
pageControlUsed = YES;
}
Every time I change the page I need to display a tableview with some labels. Here is the code :
- (void)viewDidLoad
{
[super viewDidLoad];
[self getVehicules];
vosvehiculeScrollView.pagingEnabled = YES;
vosvehiculeScrollView.showsHorizontalScrollIndicator = NO;
vosvehiculeScrollView.showsVerticalScrollIndicator = NO;
vosvehiculeScrollView.scrollsToTop = NO;
pageControlVehiculePossedee.numberOfPages=[vehiculesPossede count];
pageControlVehiculePossedee.currentPage=0;
[self loadScrollViewWithPage:0];
[pageControlVehiculePossedee addTarget:self action:#selector(pageAction:) forControlEvents:UIControlEventValueChanged];
votreVehiculeLabel.text=#"Votre véhicule";
vehiculesPossedesArray = [[NSMutableArray alloc] initWithObjects:#"Annee modele", #"Transmission",#"Carburant", nil];
}
- (void) loadScrollViewWithPage: (int) page {
if (page < 0) return;
if (page >= [vehiculesPossede count]) return;
tableViewVehiculesPossedes=[[UITableView alloc] initWithFrame:CGRectMake(3, 80, 315, 171) style:UITableViewStyleGrouped];
tableViewVehiculesPossedes.tag=page;
tableViewVehiculesPossedes.bounces=NO;
tableViewVehiculesPossedes.backgroundColor=[UIColor clearColor];
[tableViewVehiculesPossedes setDelegate:self];
[tableViewVehiculesPossedes setDataSource:self];
[self.vosvehiculeScrollView addSubview:tableViewVehiculesPossedes];
nameVehiculeLabel.text=[[vehiculesPossede objectAtIndex:page] valueForKey:#"modele"];
self.transmissionString=[[vehiculesPossede objectAtIndex:page]valueForKey:#"transmision"];
self.carburantString=[[vehiculesPossede objectAtIndex:page] valueForKey:#"carburant"];
self.anneeModelString=[[vehiculesPossede objectAtIndex:page] valueForKey:#"modele_annee"];
self.anneeString=[[vehiculesPossede objectAtIndex:page]valueForKey:#"annee"];
if(page==0){
NSLog(#"0");
self.transmissionString=#"ttt";
}
else NSLog(#"1");
}
The problem is that even I put [self.tableViewVehiculesPossedes release]; on the change page method, the tableView appear overlay and the text from labels are overlay. What can I do with the tableview to make it dissapear when a new page will be display?
Please help me..I spent a lot of time with this :|
If I get it correctly I think you should keep an ivar to the table you create with the alloc in loadScrollViewWithPage, and when switching page you should removing it from the view by calling :
[mytableview removeFromSuperview];
And most likely your should take care of releasing it as well as I can't see it in your code.
i have a strange problem.
On my view i have two segmented controls and one button.
The segmented controls work perfectly but my button crashes the app when i touch it.
NSLog won't be called anymore.
PageViewController.h
paintingOptions = [[UIButton alloc] initWithFrame:CGRectMake(200,30,100,100)];
[paintingOptions setTitle:#"Ok" forState:UIControlStateNormal];
[paintingOptions setBackgroundColor:[UIColor blackColor]];
[paintingOptions addTarget:self action:#selector(showPaintingOptions:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:paintingOptions];
-(void) showPaintingOptions: (id) sender {
NSLog(#"Button pressed");
}
Any hints on how to solve that ?
Debug:
The debug output says nothing but (gdb).
Xcode shows me this line:
PDFViewController.h
- (UIView *)view {
return self.scrollView; }
0x32c37102 <+0014> ldr r1, [pc, #16] (0x32c37114 <-[UIViewController nextResponder]+32>)
But this function is in another view controller.
So the view where i have the button in is a subview of this scrollView.
Adding PageViewController to PDFViewController:
- (void)loadScrollViewWithPage:(int)page
{
if (page < 0)
return;
if (page >= kNumberOfPages)
return;
// replace the placeholder if necessary
PageViewController *controller = [viewControllers objectAtIndex:page];
if ((NSNull *)controller == [NSNull null])
{
//page+1 cause CGPDF indexing starts with 1
controller = [[PageViewController alloc] initWithPageNumberAndUrl:page+1: [chapter urlOnFilesystem]];
[viewControllers replaceObjectAtIndex:page withObject:controller];
[controller release];
}
// add the controller's view to the scroll view
if (controller.view.superview == nil)
{
CGRect frame = scrollView.frame;
frame.origin.x = frame.size.width * page;
frame.origin.y = 0;
controller.view.frame = frame;
[scrollView addSubview:controller.view];
}
}
From the crash log it looks like -(void) showPaintingOptions: (id) sender is not in your target self. Please make sure that.
I have this page control code same from the apple sample. Here i have a subview (controller.view) which contains a ImageView. Now problem is with memory management. All works fine. But when i scroll 5-10 pages. RAM gets filled.
I tried to release the view+controller but did not find any proper place/way that work. I want to release the views which are not currently visible. (except current,previous & next view)
- (void)applicationDidFinishLaunching:(UIApplication *)application {
NSMutableArray *controllers = [[NSMutableArray alloc] init];
for (unsigned i = 0; i < kNumberOfPages; i++) {
[controllers addObject:[NSNull null]];
}
self.viewControllers = controllers;
[controllers release];
scrollView.pagingEnabled = YES;
scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * kNumberOfPages, scrollView.frame.size.height);
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.showsVerticalScrollIndicator = NO;
scrollView.scrollsToTop = NO;
scrollView.delegate = self;
pageControl.numberOfPages = kNumberOfPages;
pageControl.currentPage = 0;
[self loadScrollViewWithPage:0];
[self loadScrollViewWithPage:1];
}
- (void)loadScrollViewWithPage:(int)page {
if (page < 0) return;
if (page >= kNumberOfPages) return;
PageControlExampleViewControl *controller = [viewControllers objectAtIndex:page];
if ((NSNull *)controller == [NSNull null]) {
controller = [[PageControlExampleViewControl alloc] initWithPageNumber:page];
[viewControllers replaceObjectAtIndex:page withObject:controller];
[controller release];
}
if (nil == controller.view.superview) {
CGRect frame = scrollView.frame;
frame.origin.x = frame.size.width * page;
frame.origin.y = 0;
controller.view.frame = frame;
[scrollView addSubview:controller.view];
}
}
- (void)scrollViewDidScroll:(UIScrollView *)sender {
if (pageControlUsed) {
return;
}
CGFloat pageWidth = scrollView.frame.size.width;
int page = floor((scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
pageControl.currentPage = page;
[self loadScrollViewWithPage:page - 1];
[self loadScrollViewWithPage:page];
[self loadScrollViewWithPage:page + 1];
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
pageControlUsed = NO;
}
- (IBAction)changePage:(id)sender {
int page = pageControl.currentPage;
[self loadScrollViewWithPage:page - 1];
[self loadScrollViewWithPage:page];
[self loadScrollViewWithPage:page + 1];
CGRect frame = scrollView.frame;
frame.origin.x = frame.size.width * page;
frame.origin.y = 0;
[scrollView scrollRectToVisible:frame animated:YES];
pageControlUsed = YES;
}
I tried for several hours the code of Deepmist. It did the job but I received memory warnings and my app crashed every 25-30 pages scrolled (I am using big images in the pages). In Instruments I noticed a very large use of memory: even if the unnecessary views were removed time by time from superview and the relative viewControllers were replaced with NSNulls, Instruments showed that real memory increased on every pagescroll of 4-5MB!
Searching on the web I found that this is a common problem. If you also have this problem, you should try the following checks:
1) in each view, be sure to use imageWithContentsOfFile instead of imageNamed. As documented imageNamed cache images and increase memory size.
2) in the Deepmist code, after:
[controller.view removeFromSuperview];
you also have to set to nil the view:
controller.view=nil;
This trick solved the memory consumption which now is stable for the only three views loaded (current, current-1 and current+1 to avoid flashing in the pagescroll).
Hope this helps!
I didn't test this but I imagine you could write a method that does the opposite of loading like so:
- (void)unloadScrollViewWithPage:(int)page {
if (page < 0) return;
if (page >= kNumberOfPages) return;
PageControlExampleViewControl *controller = [viewControllers objectAtIndex:page];
if ((NSNull *)controller != [NSNull null]) {
if (nil != controller.view.superview)
[controller.view removeFromSuperview];
[viewControllers replaceObjectAtIndex:page withObject:[NSNull null]];
}
}
Then add some code to your didScroll method like so:
[self unloadScrollViewWithPage:page - 2];
[self loadScrollViewWithPage:page - 1];
[self loadScrollViewWithPage:page];
[self loadScrollViewWithPage:page + 1];
[self unloadScrollViewWithPage:page + 2];
in loadScrollViewWithPage method, just before if (nil == controller.view.superview), cycle trough all views and remove them all except current -1, current and current + 1 but only if method was called with current index view. Also, don't forget to replace those view controllers in viewControllers array with NSNulls.
try this VSScrollview , it reuses its views like UITableview reuses its cell.
I wonder if someone could quickly help me with the following, do I need to add a [myTableView release]; after I call [view addSubview:[self myTableView]]; ? Initially I was thinking no, and running it through CLANG produced to memory warnings
Here is my thinking:
[self setMyTableView:tempTableView]; retainCount = (+1)
[view addSubview:[self myTableView]]; retainCount = (+2)
//[myTableView release]; << HERE retainCount = (+1)
-dealloc [myTableView release]; retainCount = ( 0)
.
#property (nonatomic, retain) UITableView *myTableView;
.
- (void)loadView {
NSLog(#"%s", __PRETTY_FUNCTION__);
[self setTitle:#"Location Data"];
CGRect viewFrame = CGRectMake(0, 20, 320, 460);
UIView *view = [[UIView alloc] initWithFrame:viewFrame];
CGRect tableFrame = CGRectMake(0, 0, 320, 416);
UITableView *tempTableView = [[UITableView alloc] initWithFrame:tableFrame];
[self setMyTableView:tempTableView];
[tempTableView release];
[view addSubview:[self myTableView]];
//[myTableView release]; << HERE
[[self myTableView] setDelegate:self];
[[self myTableView] setDataSource:self];
[self setView:view];
[view release];
}
.
- (void)dealloc {
[myTableView release];
[dataModel release];
[super dealloc];
}
EDIT: hmm maybe I don't as [view addSubview:[self myTableView]]; retains it and will release when done. Your right Carl, my bad. I was getting confused with: alloc, set, release, when this is simply the view taking ownership (and the responsibility to release that later)
You're right when you say that [view addSubview:[self myTableView]]; will retain your table view, but since it's the view who is retaining it, it's the view that is supposed to release it. And it will, when the view gets dealloc'ed. You only have to release what you retained yourself, i.e., you only have to release the table view once in your dealloc method.
Your method doesn't retain myTableView, so it shouldn't release it.