0

Okay, so I was reading here declaring global variables in iPhone project and I noticed the line with this code: [[[UIApplication sharedApplication] delegate] myNSString];.

Basically, I want a user to input something in a text field on the flipside view, then have it stored in a variable which is accessed on the main view. Ideally, the main view would be able to just read the text field from the flipside view, but this seems to be impossible (I've spent several hours each day for the past few days scouring the web and various books for an answer about how to do this and no one seems to be able to give a definitive answer). Therefore, I'm resorting to using a global variable to tackle this.

Will the code that I printed above somehow allow me to do this? I've been trying to adapt it for the past hour, but have come up with nothing except No known instance method for selector 'myNSString' and I'm not quite sure what that means in this case.

Can someone please help me out? I feel like I can keep trying different things but without some sort of help, I'm just shooting in the dark here. Thank you!

Community
  • 1
  • 1
user74756e61
  • 221
  • 2
  • 9

2 Answers2

1

You may want to think about using a singleton to hold your data if you're set on using a global variable. There's a good tutorial on singletons here: http://www.galloway.me.uk/tutorials/singleton-classes/ -basically it's a class that can be shared throughout the application and accessed/modified by different controllers. You'd be able to create a property on it, write to that property from the flip view, and then access that property from your main view.

#import "Singleton.h"

@implementation Singleton

@synthesize yourTextField;

#pragma mark Singleton Methods

+ (id)sharedManager {
    static Singleton *sharedMyManager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedMyManager = [[self alloc] init];
    });
    return sharedMyManager;
}

- (id)init {
    if (self = [super init]) {
        yourTextField = @"";
    }
    return self;
}

You could call it in code by importing its header file and:

Singleton *mySingleton = [Singleton sharedManager];

the mySingleton object will have the text field attached. It can be accessed by:

mySingleton.yourTextField;

.h file:

#import <Foundation/Foundation.h>

@interface Singleton : NSObject

@property (nonatomic, strong) NSString *yourTextField;

+ (id)sharedManager;

@end
GWhite
  • 426
  • 5
  • 13
  • Thank you! As per the tutorial, would I call `MyManager *sharedManager = [MyManager sharedManager];` on both the main view and flipside view? It seems like it's creating different instances of them in memory and it still isn't transferring the value, but it does at least compile and run. – user74756e61 Feb 27 '14 at 21:03
  • Yes you'd call it on both sides. Once you have the information you need you'd set the variable and it will persist until called later (from your other view). I've edited my answer to include the relevant code. – GWhite Feb 27 '14 at 21:05
  • It doesn't seem to be persisting. A quick NSLog check of the variable shows that each time my Main view loads, the NSString is empty. It's like it's creating it anew every time I change views. – user74756e61 Feb 27 '14 at 21:08
  • Please post the code you're using so I can help you. – GWhite Feb 27 '14 at 21:09
  • Nevermind, it actually did. I had something that overwrote it every time. Thank you SO much for your help! – user74756e61 Feb 27 '14 at 21:11
0

Singleton (remember about dispatch_once), static variables or NSUserDefaults. It really depends what you really need.

If you are using storyboards and just want to pass data between VC, then you can use "prepareForSegue" method (described here https://developer.apple.com/library/ios/documentation/uikit/reference/UIViewController_Class/Reference/Reference.html#//apple_ref/occ/instm/UIViewController/prepareForSegue:sender:).

Segue has "destinationController" property, so you can setup VC before showing it.

blackrain
  • 56
  • 2
  • Sorry, I should have mentioned that I've only been doing this for a few weeks now. This discussion went way over my head. If NSUserDefaults refers to settings, this is not what I wanted to use to accomplish this task. – user74756e61 Feb 27 '14 at 21:08
  • Do you use storyboards? – blackrain Feb 27 '14 at 21:12