I want create global variable for coordinates UIView how correct syntax to do this?
3 Answers
Instead of using two floats, I would recommend storing coordinates in a container made exclusively for this: CGPoint
.
To use it globally, you could add it to a singleton (as in @user3182143 answer), or expose it from your class.
In your .m
you can define it as a constant (outside both the @interface
and @implementation
) as follows:
const CGPoint kMyCoordinate = {.x = 100, .y = 200};
In order for other classes to be able to use it, you need to expose it in the .h
as follows:
extern const CGPoint kMyCoordinate;
While you normally create CGPoints
with CGPointMake(x,y)
in this particular case we have to use the shorthand because otherwise Xcode complains that the "initializer element is not a compile-time constant".

- 613
- 7
- 10
-
I think your answer is most convenient. I thought of using CGPoint . – Andrei Trotsko Aug 01 '16 at 14:42
-
in my " .h" i add @property(nonatomic, assign) CGPoint* lastScrollOffset; and in my " .m" i must call him and assigned to the current position my View how i cat do it? – Andrei Trotsko Aug 01 '16 at 14:44
-
If you want to use properties instead of class constants, then define the property in the `.h`: `@property (nonatomic) CGPoint myCoordinate;` And in `.m`: `self.myCoordinate = CGPointMake(100, 200);` – Paulo Fierro Aug 01 '16 at 14:46
-
I logically impossible. From the top there VIEV size of which depends on the content and it may change its height. after it goes tableView which NSLayoutConstraint to topLayout view depends on the size of which is above. And I need to catch during scrolling contentOffset.y how much it has changed and to transfer it to my global variable that would sravnimat with its initial coordinate – Andrei Trotsko Aug 01 '16 at 15:18
Probably the right thing to use here would be a class-level property. See here for more information.

- 1
- 1

- 34
- 2
First you need too create NSObject Class
Give Class name GlobalShareClass
GlobalShareClass.h
#import <Foundation/Foundation.h>
@interface GlobalShareClass : NSObject
{
}
@property (nonatomic) float xvalue
@property (nonatomic) float yvalue
+ (GlobalShareClass *)sharedInstance;
@end
GlobalShareClas.m
#import "GlobalShareClass.h"
static GlobalShareClass *_shareInstane;
@implementation GlobalShareClass
@synthesize xvalue;
@synthesize yvalue;
+ (GlobalShareClass *)sharedInstance
{
if (_shareInstane == nil)
{
_shareInstane = [[GlobalShareClass alloc] init];
}
return _shareInstane;
}
ViewController.h
#import <UIKit/UIKit.h>
#import "GlobalShareClass.h"
@interface ViewController : UIViewController
{
GlobalShareClass *globalShare;
}
@end;
ViewController.m
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
globalShare = [GlobalShareClass sharedInstance];
globalShare.xvalue = 50.0;
globalShare.xvalue = 100.0;
}

- 9,459
- 3
- 32
- 39