S
S
SpaceInvader2014-08-17 18:15:42
Objective-C
SpaceInvader, 2014-08-17 18:15:42

How to access a variable from another class?

How to access 'pew' variable from another class (ViewController)?
// ImageTouched.h

#import <UIKit/UIKit.h>

@interface ImageTouched : UIImageView    
@property (nonatomic, strong) NSString *pew;    
@end

// ImageTouched.m
#import "ImageTouched.h"

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    NSLog(@"Image Touched");
    self.pew = @"pewpewpewew";  
}

// myViewController.m
#import "ImageTouched.h"

- (void)viewDidLoad
{
    [super viewDidLoad];    
    ImageTouched *instance = [[ImageTouched alloc] init];        
    NSLog(@"Pew value is %@", instance.pew);

}
log:
Pew value is (null)
Image Touched

I think the problem is that the 'pew' variable is initialized after clicking on the picture. At this point myViewController is already loaded. We need a method that would handle events after the controller is loaded.

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
D
Denis Morozov, 2014-08-17
@morozovdenis


I think the problem is that the 'pew' variable is initialized after clicking on the picture. At this point myViewController is already loaded. We need a method that would handle events after the controller is loaded.
what what? I don’t understand the meaning, describe in more detail
such code will initialize pew before NSLog is executed
#import "ImageTouched.h"

-(void)awakeFromNib
{
    self.pew = @"pewpewpewew";  
}

R
Rorg, 2014-08-17
@Rorg

I think the problem is that the 'pew' variable is initialized after clicking on the picture. At this point myViewController is already loaded. We need a method that would handle events after the controller is loaded.

Basically you are right. At the time of the creation of the view and the call to the viewDidLoad function, the event (clicking on the image) has not yet occurred, so the pew variable has not been initialized. Taking your question as a whole (How to access a variable from another class?) You are doing everything right, but in this situation the event that initializes the pew variable happens later than the view is created.
Please describe in more detail what you need.

A
agee, 2014-08-18
@agee

In the viewcontroller's code, write

- (void)viewDidLoad {
    [super viewDidLoad];    
    ImageTouched *instance = [[ImageTouched alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
    [self.view addSubview:instance];
    // Подписываемся на уведомление
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(imageDidTouch:) name:@"ImageTouchedImageDidTouch" object:nil];
}

Add a method to the view controller code
// Метод, который обрабатывает уведомление
- (void)imageDidTouch:(NSNotification *)notification {
    if ([notification.object isKindOfClass:[ImageTouched Class]])
        NSLog(@"Pew value is %@", ((ImageTouched *)notification.object).pew);
}

And already in touchesBegan call this notification
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    NSLog(@"Image Touched");
    self.pew = @"pewpewpewew";  
    // Отправить уведомление, отправляя самого себя в качестве параметра object
    [[NSNotificationCenter defaultCenter] postNotificationName:@"ImageTouchedImageDidTouch" object:self];
}

For reading:
About properties https://developer.apple.com/library/ios/documentat...
About notifications: https://developer.apple.com/library/mac/documentat...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question