Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 10-01-2011, 04:51 PM   #1 (permalink)
Registered Member
 
Join Date: May 2011
Posts: 53
pavlicky is on a distinguished road
Default Singleton of UIView subclass

Hi there,

I have UIView subclass, which is supposed to be tableHeaderView for UITableViews. Seen the fact I have multiple UITableViews, I want to "share" instance of UIView subclass between them. Therefore I have following shared method:

HeaderView.h
PHP Code:
static HeaderView *sharedHeader nil;

+ (
HeaderView *)sharedInstance {
    
    @
synchronized(self) {
        if (
sharedHeader == nil) {
            
sharedHeader = [[self allocinitWithFrame:CGRectMake(00320120)];
        }
    }
    return 
sharedHeader;
}

- (
id)initWithFrame:(CGRect)frame {
    
self = [super initWithFrame:frame];
    if (
self) {
        
// Initialization code
        
NSArray *nib = [[NSBundle mainBundleloadNibNamed:@"HeaderView" owner:self options:nil];
        
self = [nib objectAtIndex:0];
    }
    return 
self;

I call sharedInstance in my UITableViewControllers in viewWillAppear methods:
PHP Code:
- (void)viewWillAppear:(BOOL)animated{
    [
super viewWillAppear:animated];
    
HeaderView *= [HeaderView sharedInstance];
    
self.tableView.tableHeaderView h;

When the first UITableViewController appears, it works fine. When the second UITableViewController appears it works fine as well. Problem occurs when I go back to the first UITableViewController -> there is only frame of size 320 x 120, but nothing inside the frame! I have tried to NSLog the tableHeaderView and it prints me <HeaderView: 0x5a75080; frame = (0 0; 320 120); autoresize = RM+BM; layer = <CALayer: 0x5a74f20>> which means there is an object. But why the NIB doesn't show up???
pavlicky is offline   Reply With Quote
Old 10-01-2011, 05:07 PM   #2 (permalink)
Registered Member
 
Join Date: May 2010
Posts: 140
MatthewD is on a distinguished road
Default

You shouldn't have a singleton class that extends UIView. A UIView object can only be added to one display hierarchy - add it to one view as a subview, and it will be removed from its other superview just like you witnessed.
MatthewD is offline   Reply With Quote
Old 10-01-2011, 07:58 PM   #3 (permalink)
Registered Member
 
Join Date: May 2011
Posts: 53
pavlicky is on a distinguished road
Default

Quote:
Originally Posted by MatthewD View Post
You shouldn't have a singleton class that extends UIView. A UIView object can only be added to one display hierarchy - add it to one view as a subview, and it will be removed from its other superview just like you witnessed.
Oh yeah, I can see. Found out other solution. Thank you for your help ;-)
pavlicky is offline   Reply With Quote
Old 11-21-2011, 07:49 AM   #4 (permalink)
Registered Member
 
Join Date: Aug 2011
Posts: 1
raghunath is on a distinguished road
Default

Quote:
Originally Posted by pavlicky View Post
Oh yeah, I can see. Found out other solution. Thank you for your help ;-)
Hi pavlicky,
I'm facing the same problem too. Tried to make the subclass of UIView as Singleton, but came across this post and avoided it. How did you achieve the other way? Did you try creating another subclass of UIView and instantiated the singleton class??

i've got a singleton class too with an Array of instances like this

Code:
@interface MyControl:UIControl {

   UIButton *red, *green, *blue;
   UISlider  *colorSlider;
   UIView    *palleteView;
} //Buttons and slider will be added into <palleteView>

//Some properties here

@end;


//Singleton Class
#import "MyControl.h"

@interface ColorPallete:NSObject {
   
   NSMutableArray *arrayOfControls;
}//arrayOfControls will be loaded with instances of MyControl class

+(ColorPallete*) sharedColorPallete;

@end

//I'll be sharing the ColorPallete data in other view controllers of my project

//In the View Controller
#import "ColorPallete.h"

@interface MyViewController :UIViewController {

  ColorPallete *colorPallete;
}

@end;

@implementation MyViewController 

- (void) viewDidLoad {

  colorPallete = [ColorPallete sharedColorPallete];

  [self.view addSubView: ?????];
}

@end
The question is how do I load the array of controls residing inside the singleton class ColorPallete into the ViewController?
raghunath is offline   Reply With Quote
Old 11-21-2011, 08:17 AM   #5 (permalink)
Registered Member
 
Join Date: May 2011
Posts: 53
pavlicky is on a distinguished road
Default

Quote:
Originally Posted by raghunath View Post
Hi pavlicky,
I'm facing the same problem too. Tried to make the subclass of UIView as Singleton, but came across this post and avoided it. How did you achieve the other way? Did you try creating another subclass of UIView and instantiated the singleton class??

i've got a singleton class too with an Array of instances like this

Code:
@interface MyControl:UIControl {

   UIButton *red, *green, *blue;
   UISlider  *colorSlider;
   UIView    *palleteView;
} //Buttons and slider will be added into <palleteView>

//Some properties here

@end;


//Singleton Class
#import "MyControl.h"

@interface ColorPallete:NSObject {
   
   NSMutableArray *arrayOfControls;
}//arrayOfControls will be loaded with instances of MyControl class

+(ColorPallete*) sharedColorPallete;

@end

//I'll be sharing the ColorPallete data in other view controllers of my project

//In the View Controller
#import "ColorPallete.h"

@interface MyViewController :UIViewController {

  ColorPallete *colorPallete;
}

@end;

@implementation MyViewController 

- (void) viewDidLoad {

  colorPallete = [ColorPallete sharedColorPallete];

  [self.view addSubView: ?????];
}

@end
The question is how do I load the array of controls residing inside the singleton class ColorPallete into the ViewController?
You need to have instance of that UIView subclass. As Matthew said:

Originally Posted by MatthewD
You shouldn't have a singleton class that extends UIView. A UIView object can only be added to one display hierarchy
pavlicky is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 386
16 members and 370 guests
13dario13, 7twenty7, eski, EvilElf, glenn_sayers, HemiMG, iOS.Lover, jarv, LunarMoon, n00b, pbart, Pudding, sacha1996, Sami Gh, UMAD, VinceYuan
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,672
Threads: 94,122
Posts: 402,906
Top Poster: BrianSlick (7,990)
Welcome to our newest member, yuncarl28
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 05:20 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0