Declare project wide variable in Objective C - ios

I was wondering if there is a way to declare a project wide variable like you get when you start a application with a UINavigationController and you can call that navigation controller from anywhere in you application because of how its been declared in the applications Delegate.
I am wanting to create a project (global) var that allows me to call SVProgress hud or dismiss it from anywhere in my application. I have this issue where I load a UIViewController onto the navigation stack, start the SVProgress hud then make a request to my DB.. if I get an error back i need to handle a few things one of them is to be able to dissmiss the SVProgress hud.
This is the code I have so far
AppDelegate.h
#import "SVProgressHUD.h"
#interface MYAPPAppDelegate : UIResponder <UIApplicationDelegate> {
//..
}
#property (strong, nonatomic) SVProgressHUD *svprogressHud;
AppDelegate.m
#synthesize svprogressHud;
not really sure if this is possible hopefully someone out there will be able to help me out.
Here is a link to SVProgress git

One thing you can do from your view controller is grab the app delegate and cast it to the type of your delegate like so:
#import "MYAPPDelegate.h"
MYAPPDelegate *ad = (MYAPPDelegate *)[[UIApplication sharedApplication] delegate];
[ad.svprogressHud displayOrWhatever];
Not really the cleanest but should get the job done.

Just add the header file name into you pre-compiled header (<projectname>_Prefix.pch) file inside of the #ifdef __OBJC__
Like as:
#ifdef __OBJC__
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import "SVProgressHUD.h"
#endif
Then it will give you access to the file across all files.....

Just extending Roberts answer, In order to avoid repeating the code to get the app delegate and then cast it into MYAPPDelegate, I would create a header file/class in your project with a utility function.
Create new header called AppUtils.h
#import "MYAPPAppDelegate.h"
#ifndef AppName_AppUtils_h
#define AppName_AppUtils_h
static inline MYAPPAppDelegate* AppDelegate() {
return (MYAPPDelegate *)[[UIApplication sharedApplication] delegate];
}
#endif
Where ever you need access to these variables import the header AppUtils.h and use the helper function. Example:
#import "AppUtils.h"
...
- (void)foo {
// use app delegate
UIViewController *rootVC = AppDelegate().rootViewController;
}

Related

Inheritance from an Objective-c base view controller from Swift

I am trying to migrate a UIViewController Objective-C class to Swift. This view controller is inheriting from a BaseViewController where I have common functionality that I want to have in all controllers. The problem I am having is that the generated myproject-Swift.h is not able to find my BaseViewController.
Is there any way to implement a UIViewController in swift that inherits from a BaseViewController (subclass of UIViewController) written in Objective-C? Is there a bridging problem?
It can be reproduced with this minimal code:
BaseViewController.h
#import <UIKit/UIKit.h>
#interface BaseViewController : UIViewController
#end
BaseViewController.m
import "BaseViewController.h"
#implementation BaseViewController
#end
ViewController.swift
import UIKit
class ViewController : BaseViewController {
}
AppDelegate.m
#import "AppDelegate.h"
#import "projectname-Swift.h" // Replace with your project name
#implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
ViewController *vc = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
self.window.rootViewController = vc;
[self.window makeKeyAndVisible];
return YES;
}
projectname-Bridging-Header.h
#import "BaseViewController.h"
As pointed out in the accepted answer on How can I add forward class references used in the -Swift.h header?
Interoperability guide (Importing Swift into Objective-C):
If you use your own Objective-C types in your Swift code, make sure to
import the Objective-C headers for those types prior to importing the
Swift generated header into the Objective-C .m file you want to access
the Swift code from.
The example is solved by importing BaseViewController before the importing projectname-Swift.h in:
AppDelegate.m
#import "AppDelegate.h"
#import "BaseViewController.h"
#import "projectname-Swift.h" // Replace with your project name
// ...
It looks like they have fixed the issue. Currently under XCode6-Beta6 the problem reported by #atxe does not occur anymore. Therefor you can finally roll your AppDelegate.m header back to:
#import "AppDelegate.h"
#import "projectname-Swift.h" // Replace with your project name

How to access global Array without using AppDelegate in ios

I have implemented Opencv in one of my project. In an opencv .mm file, I need to access the appdelegate object for global access of an array but it is giving Linker Error all the time (When I am trying to import the AppDelegate Class). So I have created an NSObject Class, still I can not access the Global Array which is created as the #property of the NSObject (I have imported the NSObject class in the .mm file). Array is giving the NULL value.
So where do I create the Array by which I can access the array in my whole project?
I can not use the NSUSerDefaults and DocumentDirectory. Because I want to save a lots of images and video links in that array, So I can not use NSUserDefaults and I don't want to use again the phone memory for the temp file as because I am picking the images from the Photolibrary, so DocumentDirectory is discarded, So Is there any way by which I can create the global array and access them from the openCV files i.e objective CPP files?
you can make class as UserdataSingleton which overrides NSObject. which you can use all over your application to share data globally (for your case array). this code template may help you:
#import <Foundation/Foundation.h>
#interface UserDataSingleton : NSObject
{
#private
NSArray *globalArray;
}
+(UserDataSingleton *) getInstance;
-(void)saveInUserDatasingletonWithArray:(NSArray *)array;
-(NSDictionary *)getGlobalArray;
#end
and implementation file will be some thing like:
#import "UserDataSingleton.h"
#implementation UserDataSingleton
static UserDataSingleton *userDataSingletonInstance;
+(UserDataSingleton *) getInstance
{
if (userDataSingletonInstance == nil) {
userDataSingletonInstance = [[UserDataSingleton alloc] init];
}
return userDataSingletonInstance;
}
-(void)saveInUserDatasingletonWithArray:(NSArray *)array
{
globalArray = array;
}
-(NSDictionary *)getGlobalDictionary
{
return globalArray;
}
#end
================== usage:
#import "UserDataSingleton.h"
#define USERDATASINGLETON (UserDataSingleton *)[UserDataSingleton getInstance]
......................your code...
NSArray *this_IS_Array_Populated_here_For_Global_Access = [NSArray alloc] initWith....];
[USERDATASINGLETON saveInUserDatasingletonWithArray:this_IS_Array_Populated_here_For_Global_Access];//you put your array for global access.
later some where in any other view or view controller you can get that global array for example lets say you have YourViewController class:
NSMutableArray *yourArrayFromWebResponse = [USERDATASINGLETON getGlobalArray];
thanks
Note that Objective C is a superset of C. Thus, you can have regular C variables in your program. My favorite is to make a global variable for my appDelegate. Put this in your app delegate's .h file:
MyAppDelegateClass * appDelegate;
(change "MyAppDelegateClass" to your appDelegate's class name) and put this in application: didFinishLaunchingWithOption: method in your app delegate's .m file:
appDelegate = self; // set universal global variable
Now just #import your app delegate's .h file and have access to your appDelegate from anywhere.
You may try to create a singleton objet like and access it from everywhere
you could define a macro in your delegate like so:
#define sharedAppDelegate ((AppDelegate *)[UIApplication sharedApplication].delegate)
and then in the class which you want to access your array import your AppDelegate class, boiler plate would be:
#import AppDelegate.h
and then just use:
NSArray *arrayYouWanted = [AppDelegate sharedAppDelegate].yourPropertyArrayName;
You can also store it in NSUserDefaults if you want to persist it.

No managedObjectContext defined in my AppDelegate

I'm trying to test my core data scheme. However, it seems I am unable to create the context because it says No visible #interface for 'MyAppDelegate' declares the selector 'managedObjectContext'.
In online tutorials this method seems to be auto-generated when we create the app. However, in my case it doesn't exist.
This is MyAppDelegate:
Header
#import <UIKit/UIKit.h>
#interface MyAppDelegate : UIResponder <UIApplicationDelegate>
#property (strong, nonatomic) UIWindow *window;
#end
.m file
#import "MyAppDelegate.h"
#implementation MyAppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSManagedObjectContext *context = [self managedObjectContext];
// Override point for customization after application launch.
return YES;
}
How should I fix this in Xcode 5 with iOS 7?
I think the best way for you is to create a Master-Detail Application with Xcode 5 and don't forget to check Use Core Data :
With that, you will have an AppDelegate.h and an AppDelegate.m configured with a managedObjectContext.
You will have a project configured correctly with Core Data and a .xcdatamodeld to use easily your SQLite database.

linking a static library in ios

I have created a math based application in Xcode 4.4. I am using tabbar based app with the help of storyboard.
I have written all my math functions in a separate class called CalculationMethods which is the subclass of NSObject.
My ViewController:
// FirstViewController.h
#import <UIKit/UIKit.h>
#import "CalculationMethods.h"
#interface FirstViewController : UIViewController
#end
// FirstViewController.m
#import "FirstViewController.h"
#import "CalculationMethods.h"
#interface FirstViewController ()
#end
#implementation FirstViewController
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(#"%f",[self julianDateFinder: [self currentDayMonthAndYearFinder]]);
}
#end
As you can see I have included my CalculationMethod.h file in both FirstViewController.h and the FirstViewController.m file, but when I use the methods of that class such as julianDateFinder and currentDayMonthAndYearFinder, Xcode errors, saying:
"No Visible #interface for 'FirstViewController' declares the selector 'CurrentDayMonthAndYearFinder'"
I am new to iOS and XCode. Can anyone help me solve the error?
In the FirstViewController, to use any of the methods in the CalculationMethods class, you need to create an instance of CalculationMethods. And then access a method using this syntax: [instanceOfCalculationMethods aMethodInCalculationMethods];
For example in your case, try this:
In the FirstViewController.h file, before the #end:
CalculationMethods *_calculationMethods;
And in the viewDidLoad method:
_calculationMethods = [CalculationMethods alloc] init];
NSLog(#"%f",[_calculationMethods julianDateFinder: [_calculationMethods currentDayMonthAndYearFinder]]);
I think you're misunderstanding how Objective C works slightly.
Without adding details of the CalculationMethods.h header file I can't help you much, but that compiler warning is telling you that FirstViewController doesn't have the method currentDayMonthAndYearFinder.
The reason that this is the case is because you're calling performing the selector CurrentDayMonthAndYearFinder on self which in the context of your FirstViewController instance is in fact the instance of FirstViewController
You said it yourself that your method CurrentDayMonthAndYearFinder is on your CalculatorMethods class so I suggest you either create an instance of your CalculatorMethods class or call the class method named CurrentDayMonthAndYearFinder on your CalculatorMethods class.
The problem here is weather or not you've defined instance methods or class methods.
Do yourself a favour and update your question with the contents of CalculationMethods.h

AppDelegate class not found

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.
}

Resources