I am a beginner in Ios developement and I'm developping a tabed application but I would like a property accessible from each view so i write this in appDelegate:
#property (strong, nonatomic) NSString *test;
But now I don't know how to access this string from my firstViewController.m and secondViewController.m
You need to create Object of Appdelegate class like:-
AppDelegate *del = [[UIApplication sharedApplication]delegate];
and used this defined string in any class using del.test. Do not forget to import your Appdelegate class in to particular class that you want to use it's string. And also #synthesize your string in .m appdelegate class as well.
UPDATE:-
If you wish to use Appdelegate Object in particular class then Import App-delegate in to .h file like:-
and .m class in ViewDidLoad
Related
I'm using JSONModel in my objective c application. I get all data to my JSONModel in a first abBarController. Then I need get this data in other viewController. I'm trying send this data to the others viewControllers like:
First ViewController:
#implementation FirstViewController
...
SecondViewController* infoController = [self.storyboard instantiateViewControllerWithIdentifier:#"secondViewController"];
SecondViewController.model = self.model;//Here send the model with data
[self.navigationController pushViewController:infoController animated:YES];
...
#end
Second ViewController:
#interface SecondViewController :UIViewController{
MyModel *model;
}
#property MyModel *model;
There is a better form to keep this data model instantiated and get the model data from another viewController without send this in a property?
Create object class
In .h object class
#interface FirstModel : NSObject{
}
#property(nonatomic,strong)NSMutableArray *productsArray;
In .m object Class
-(id)init{
self=[super init];
if (self) {
_productsArray=[[NSMutableArray alloc]init];
}
return self;
}
Create another one Object Class
#interface SecondModel : NSObject
#property (nullable,nonatomic, retain) NSString *name;
#end
In TableviewViewcontroller .h file
Import Two Object class and insert the following coding
#property(nonatomic,strong)FirstModel *firstListObject;
In .m file
//cell for rowAt Indexpath
SecondModel *prodObj=_firstListObject.productsArray[indexPath.item];
cell.productNameLabel.text=prodObj.name;
You may have access this object class wherever you need…
You can use a singleton class, create your model property.
In another viewController, you can access your model through instance of singleton. Reference http://www.idev101.com/code/Objective-C/singletons.html
or you can archive it to the local files using plist or data-base
I'm trying to reload my SingleGrid's UITableView from my FirstViewController.
UITableView's declaration is IBOutlet and singleGrid variable declared in my AppDelegate.
I want to reload singleGrid's UITableView from FirstViewController.Table view name singleGridTable isn't coming after dot.
appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate.singleGrid.singleGridTable reloadData]
Make sure you have declared singleGrid as a property in your appdelegate.h like
#property (nonatomic, strong) SingleGridViewController * SingleGrid;
Those properties which are declared in .h files are public otherwise they are private and you can't access them in other classes.
Also make sure singleGridTable is also declared as property in your .h file.
#property (nonatomic, weak) IBOutlet UITableView * singleGridTable;
More code might help (relevant headers and implementations), but I can take a shot in the dark. Does firstViewController know about what properties would exist on a SingleGrid? Where ever you would want to access a property on SingleGrid you'll need to include the header that declares that the property exists. So does your firstViewController ha
i want to know if the following situation can be done. I have inherited a project of iOS 8. I'd like to add a method to NSObject so that all objects can see it. and I have done this already. Here is the category implementation i have created:
#import "NSObject+myCategory.h"
#implementation NSObject (myCategory)
-(void)localizeStringIncludeDefault{
NSLog(#"about to localize String");
}
#end
Now i go to a MyViewController.m for example and try to call the method but its not working its not seen:
heres the .h:
#import <UIKit/UIKit.h>
#interface MyViewController : UIViewController {
BOOL someFakeBoolean;
IBOutlet UIView *someView;
//etc
}
#property (nonatomic,assign) IBOutlet MyViewController *myViewController;
-(void)localizeStringIncludeDefault;
and here is the implementation *.m and my issue:
#implementation MyViewController
- (void) viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self localizeStringIncludeDefault] //this call cant be done, the method is not visible
}
- (void) viewDidDisappear:(BOOL)animated
{
//etc
}
//etc
I mean it makes sense to me that i'd have to import the "NSObject+myCategory.h" into the MyViewController header to use the category but because i've inherited this code it already has a base. I dont want to go into every .h file and import this. Is a easy way to extend object so that EVERY object class sees my method ?
One option would be to add the category .h file to the pch file. Then it will be seen by every class in your project without the need to import it explicitly.
Declare your global variables or declarations in your pch file or rather make a Global.h and just import this in your pch file (helps a lot in reusability). You can declare extern items as well in your Global.h and populate in App Delegate
I am trying to create a property for an instance of a viewController, like so,
#property (strong, nonatomic) DetailsViewController *videoViewController;
but I get an error saying that:
DetailsViewController is an unknown type name
The view controller is definitely imported like so,
#import "DetailsViewController.h"
Why is this happening?
To avoid Circular imports, always write Import statements in .m file, and use forward declaration in .h file.
In .h file
#class DetailsViewController;
In .m file
#import "DetailsViewController.h"
OR
For private properties, use Objective - C extensions, i.e,
Im .m file
#import "DetailsViewController.h"
#interface MasterViewController ()<YourProtocolList>
#property(nonatomic, strong) DetailsViewController *detailViewController;
#end
#implementation MasterViewController
//Your implementation stuff
#end
In case of inheritance, you may need to import in .h file.
I imported the AppDelegate.h in a lot of classes with:
#import "AppDelegate.h"
#interface LoginViewController : UIViewController<UITextFieldDelegate>
{
}
#property (nonatomic, retain) AppDelegate *app;
but somehow it stopped working in my loginviewcontroller.h. It says:
unknown type name 'AppDelegate' [1]
property with 'retain (or strong)' attribute must be of object type [3]
I made this class at the start and it always worked like it should. I didn't make any changes to the class or the AppDelegate when it starting with this error.
I can import it in other classes without problems. I also tried to recreate the class but it didn't help.
Anyone got any idea how to solve this weird error?
It's not a good idea to use this line of code
#property (nonatomic, retain) AppDelegate *app;
in every class you need it. The simple way to access the delegate app where you need it is to do the following:
AppDelegate* appDel = (AppDelegate*)[[UIApplication sharedApplication] delegate];
obviously you need to do:
#import "AppDelegate.h"
in the class where you use it.
If you want a cleaner way to do this, you can create a class method in your AppDelegate.h like the following:
+(AppDelegate*)sharedAppdelegate;
in AppDelegate.m is defined as follow:
+(AppDelegate*)sharedAppdelegate
{
return (AppDelegate*)[[UIApplication sharedApplication] delegate];
}
Then, where you need it you could just call (after importing AppDelegate.h):
AppDelegate* sharedApp = [AppDelegate sharedAppdelegate];
Hope it helps.
P.S. Why do you need to access the delegate?
Declare a forward reference in .h file
#class AppDelegate
#interface LoginViewController : UIViewController<UITextFieldDelegate>
{
}
// keep it as assign rather than retain to keep retainCount leveled for the variable
#property (nonatomic, assign) AppDelegate *app;
in .m file, grab the pointer to Appdelegate by importing AppDelegate.h and then assigning the variable
#import "AppDelegate.h"
- (void)viewDidLoad
{
self.app = (AppDelegate*)[[UIApplication sharedApplication] delegate];
//use the variable.
}