-5

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.

crazy_tiger_corp
  • 271
  • 3
  • 22
  • 1
    This is not a duplicate as I explained! Please explain what makes you think it is @dandan78 – crazy_tiger_corp Sep 28 '15 at 10:20
  • 1
    This is not a duplicate as I explained! Please explain what makes you think it is @Abizern – crazy_tiger_corp Sep 28 '15 at 10:20
  • 1
    It doesn't matter what the object is or what you want to do with it; the issue is the same, and it's covered comprehensively in Apple docs, any decent iOS tutorial, and especially at the linked question. If you're having trouble _implementing_ what's shown there, then you can post a question [demonstrating the problem](http://stackoverflow.com/help/mcve) and asking for debugging help, but "I have a string/AVAudioPlayer and that question is asking about an array" does not change the fundamental nature of the problem. – jscs Sep 28 '15 at 19:18

2 Answers2

0

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"])
    {

    }
}
DHEERAJ
  • 1,478
  • 12
  • 32
-1

If you want to use a property (dateTimeString) from a presented view controller i would go with delegation.

boll
  • 57
  • 3
  • First of all, name your classes properly. ViewController and ViewController2 is not good obj-c coding conventions. Create a protocol that is ViewControllerDelegate with desired delegation methods, e.g. didSetDateString:(NSString *)dateTimeString. – boll Sep 28 '15 at 10:23
  • thank you but I will name my view controllers what happens to be best for me and that is a matter of opinion! Your rudeness was totally unwarranted! Btw I wasn't the one that marked you down. – crazy_tiger_corp Sep 28 '15 at 10:26
  • It was not my intention to be rude. – boll Sep 28 '15 at 10:27
  • sorry that was how the first bit abt bad programming came across. i appreciate you are trying to be helpful now and I thank you for that – crazy_tiger_corp Sep 28 '15 at 10:42