I have a table view controller that a user can select from and it passes a dictionary to a UITabBarController.
How do I get the different views to access the same data stored in the UITabController?
In my UITabBarController
#interface MasterTabController : UITabBarController
#property (nonatomic,strong) NSMutableDictionary * detailDictionary;
#end
Is it common practice to keep passing the same data dictionary around? I want to be able to manipulate the data so I can later post it online.
Should I create a singleton? Can I call just the detailDictionary from bView?
It sounds like you want to want to have three different table views, in there different tabs, and set the delegates to their views, and the data source to a custom class in your model.
You can create singleton class like this
+ (id)sharedManager {
static MyManager *sharedMyManager = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedMyManager = [[self alloc] init];
});
return sharedMyManager;
}
- (id)init {
if (self = [super init]) {
someProperty = [[NSString alloc] initWithString:#"Default Property Value"];
}
return self;
}
For reference http://www.galloway.me.uk/tutorials/singleton-classes/
Related
How to create CollectionView global object Which can be accessible from any Controller?
I want to create an UICollectionView which can by accessible from any view controller, and display CollectionView on Same Controller from where it called.
You can a use singleton:
#interface CollectionViewSingleton : UICollectionView
+ (id)shared;
#end
#implementation CollectionViewSingleton
+ (id)shared {
static CollectionViewSingleton *shared = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
shared = [[self alloc] init];
});
return shared;
}
#end
I'm trying to build MVC Application with Objective-C, I'm trying to allocate and init my model once in my superclass witch is UIViewController, my idea is to do it once in superclass and to have access from every subclass of my superclass.
superclass.h
#property (nonatomic, strong) Lecturer *lecturer;
superclass.m
- (void) viewDidLoad {
[super viewDidLoad];
}
#pragma mark - GET
- (Lecturer *)lecturer {
if (!_lecturer) {
_lecturer = [Lecturer alloc]init];
}
return _lecturer;
}
My idea is to call self.lecturer from all subclasses and set/get the lecturer class property's but every time when i call self.lecturer its creating a new instance, i know i can use SINGLETONE but is there any way i can do it differently without singletone design pattern?
Thanks for attention.
How about using static variable?
- (Lecturer *)lecturer {
static dispatch_once_t onceToken;
static Lecturer *o;
dispatch_once(&onceToken, ^{
o = [[Lecturer alloc] init];
});
return o;
}
In addition dispatch_once helps to do it thread-safely.
I am trying to create a singleton NSMutableArray so I can use it on TableView as Titles. How to declare it in a TableView?
e.g.
cell.textLabel.text = ??? //Don't know how to declare it from the singleton.
Here is my singleton.m:
#import "Singleton.h"
#implementation Singleton
#synthesize Trees;
+(Singleton *) theTrees {
static dispatch_once_t pred;
static Singleton *theTrees = nil;
dispatch_once(&pred, ^ {
theTrees = [[Singleton alloc] init];
theTrees.Trees = [[NSMutableArray alloc] init];
});
return theTrees;
}
-(id) init {
self = [super init];
if(self) {
// Variables
[Trees addObject:#"Berzas"];
[Trees addObject:#"Liepa"];
[Trees addObject:#"Drebule"];
}
return self;
}
#end
Please point me in the right direction, thank you.
It seems there is no need to create a singleton class here. Just have a class or instance method somewhere
- (NSDictionary *)trees
and the implementation about the same as your method that returns a singleton, but just creating a singleton NSDictionary once.
Properties like "Trees" should be written in lowercase: "trees", not "Trees".
Instance variables should start with an underscore, so
[_trees addObject:#"Berzas"];
That's done so that you always can distinguish between using accessor methods and accessing instance variables; they are not the same thing. Usually you would only access instance variables in your init method, and when implementing getters and setters for properties.
This question already has answers here:
Passing data between view controllers
(45 answers)
Closed 9 years ago.
I need to make one instance of an array that can be access by multiple view controllers. It will contain objects that will be displayed in a table view and created by a modular view and then displayed in the table view. I can't figure out how to access it once I make a #property for it in the main view controller or the AppDelegate class.
You should make a singleton and the recommended way to do that in objective-c is to create a class and add a method that looks something like:
+ (YourClass *)sharedYourClass
{
static dispatch_once_t onceToken;
static YourClass *sharedInstance;
dispatch_once(&onceToken, ^{
sharedInstance = [[self alloc] init];
});
return sharedInstance;
}
Put the array as a property in your class.
//YourClass.h
#interface YourClass : NSObject
#property(nonatomic, strong)NSArray *yourArray;
+(YourClass *)sharedYourClass;
#end
And in every class you want to use your singleton start by importing YourClass.h and then use it like:
NSArray *arr = [YourClass sharedYourClass].yourArray;
[YourArray sharedYourClass].yourArray = [[NSArray alloc] init];
etc..
What I do is put the data I want shared, in your instance the array, in the AppDelegate. Then I define a protocol that the app delegate conforms to. This lets me access the data anywhere. For example, say I have an array I want everywhere:
First define a protocol:
#protocol ApplicationState<NSObject>
#property(nonatomic, strong) NSArray* data;
#end
Then make your app delegate conform to it:
#interface AppDelegate : UIResponder <UIApplicationDelegate, ApplicationState>
#property (strong, nonatomic) UIWindow *window;
#property(nonatomic, strong) NSArray* data;
#end
Then reading and writing this shared object is simple:
id<ApplicationState> appState = (id<ApplicationState>) [UIApplication sharedApplication].delegate;
appState.data = [[NSArray alloc] init];
I am going to assume you can make the class of the array inherit from NSObject, and then pass it to the View Controller from there...
You have 2 ways to do this:
1.- Instantiate 1 arra on the main class and pass the reference to each viewcontroller what you need.
2.- Make a singleton class to hold the array an use this in your project.
First create a class like this
//GlobalDataClass.h
#interface GlobalDataClass : NSObject
#property(nonatomic,retain)NSArray *myArray;
+(GlobalDataClass*)getInstance;
#end
#import "GlobalDataClass.h"
//GlobalDataClass.m
#implementation GlobalDataClass
#synthesize myArray;
static GlobalDataClass *instance =nil;
+(GlobalDataClass *)getInstance
{
#synchronized(self)
{
if(instance==nil)
{
instance = [GlobalDataClass new];
}
}
return instance;
}
#end
Then you can use it in your viewControllers like this:
-(void)viewDidLoad{
[super viewDidLoad];
self.dataObj = [GlobalDataClass getInstance];
NSLog(#"%#",self.dataObj.myArray);
}
Hope it helps!
I was wondering how can I share a NSDictionary objects among several view controllers which basically are tabs in my application.
I tried using a protocol, like in Java, so that I can cast to the protocol and access the property. That doesn't seem to work.
Also I had a look at similar question at
How to share data globally among multiple view controllers
But I observed that the appDelegate method is not safe and may lead to memory leak.
Similarly injection on class A into class B will create the same problem.
So can anyone suggest me any method which i should study or implement in my code?
If you wants to share only Dictionary, why don't you go for class method using from helper class.
+(NSDictionary *)shareMethod
{
return dict;
}
You can use singleton class for sharing the data
Check this Singleton class
MyManger.h
#import <foundation/Foundation.h>
#interface MyManager : NSObject {
NSMutableDictionary *_dict
}
#property (nonatomic, retain) NSMutableDictionary *dict;
+ (id)sharedManager;
#end
MyManger.m
#import "MyManager.h"
static MyManager *sharedMyManager = nil;
#implementation MyManager
#synthesize dict = _dict;
#pragma mark Singleton Methods
+ (id)sharedManager {
#synchronized(self) {
if(sharedMyManager == nil)
sharedMyManager = [[super allocWithZone:NULL] init];
}
return sharedMyManager;
}
- (id)init {
if (self = [super init]) {
dict = [[NSMutableDictionary alloc] init];
}
return self;
}
You can access your dictionary from everywhere like this
[MyManager sharedManager].dict
I found a way out. Since I want a dictionary to be shared across, I declared a method in my protocol
- (void) setSingleHouse:(NSDictionary*) singleHouse;
In each of my controller I implemented the method in a appropriate manner. Hence I was able to share across them for now.
Also I figured out that I was casting in a wrong way earlier i.e (#protocol(protocol name)). Now changed it into NSObject .
Sorry for the fuss.
I advise you not to use a property in your appDelegate instance. This pattern doesn't improve code reusability.
Basicly your shared NSDictionnary is your model. So, if you want your code to follow the MVC design parttern, you should create a class with a shared instance that would contain your NDDictionnary in a public property.
This is well explained in this post
This way, you will access your shared NSDictionnary this way
:
NSDictionary* sharedDictionnary = [MyModel sharedInstance].sharedDictionary;