crazy_tiger_corp crazy_tiger_corp - 3 months ago 17
iOS Question

Sharing info between ViewControllers

I have a multiple view application and in

ViewController
I have added some code that I would like to be able to access from
ViewController2
. The purpose of the code being accessible in
ViewController2
is so I can have it run in there as well as in
ViewController
. I have already imported the
ViewController.h
file into
ViewController2
but I am unsure of how to share the data between controllers. The code is as follows:

ViewController2.h

#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
#import "ViewController.h"

@interface ViewController2 : UIViewController

@property(nonatomic, readonly) NSUInteger tapCount;
@property NSInteger numberOfLoops;
@property(readonly) NSTimeInterval deviceCurrentTime;
@property (strong, nonatomic) AVAudioPlayer *myPlayer;

- (NSTimeInterval)timeIntervalSinceDate:dateTimePicker;
- (BOOL)playAtTime:(NSTimeInterval)time;

- (IBAction)iconsBtn:(id)sender;

@end


ViewController2.m

#import "ViewController2.h"
#import <AVFoundation/AVFoundation.h>
#import "ViewController.h"

@interface ViewController2 ()
{
AVAudioPlayer *_myPlayer;
}
@end

@implementation ViewController2

- (void)viewDidLoad { //This is where I want to use the data
[super viewDidLoad];

}


ViewController.h

#import <UIKit/UIKit.h>
#import <AvFoundation/AVFoundation.h>


ViewController.m

#import "ViewController.h"


How should I do so? It is most important that I can use the one particular string in
ViewController.m


Thanks

EDIT

This is different from the other one because I was asking specifically how to do something where as the answers for the other one were more general and I was asking specifically how to share one object not multiple objects.

Answer

U can use segues for transferring data between viewcontrollers.

check this link for more details

First of all, u need to declare your dateTimeString as a property in your .h file (ViewController.h)

Then you can call the segue using

  [self performSegueWithIdentifier:@"your_segue_identifier_name" sender:self];

Then

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"your_segue_identifier_name"])
    {

    }
}