-3

So, I'm very new to iOS/Obj-C and Xcode and am trying to learn by building a simple tabbed view application which takes some user variables, moves the user to the next view and displays the variables.

Currently, I have the first view - where a user selects two dates. I have successfully logged those 2 dates to the console. I haven't quite understood the concept of moving between views yet.

What I would like help with is - inside the - (IBAction)submitDates function, moving the user to the next view and passing the variables across to that view too - and simply logging the variables to the console.

Note: The second view files (JPSecondViewController.m and JPSecondViewController.h have not been touched yet).

Any help/guidance much appreciated in advance!

My JPFirstViewController.m file

//  JPFirstViewController.m
//  Vacay
//

#import "JPFirstViewController.h"
#import "JPSecondViewController.h"

@interface JPFirstViewController ()

@end

@implementation JPFirstViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)submitDates {


    //Save the selected date variables
    NSDate *dateFromPicker = [_fromDate date];
    NSDate *endDateFromPicker = [_endDate date];
    NSLog(@"From date: %@ and end date: %@", dateFromPicker, endDateFromPicker);

    //Move user to second view controller

}

@end

my JPFirstViewController.m file

//  JPFirstViewController.h
//  Vacay
/

#import <UIKit/UIKit.h>
#import "JPSecondViewController.h"

@interface JPFirstViewController : UIViewController
@property (strong, nonatomic) IBOutlet UIDatePicker *endDate;
@property (strong, nonatomic) IBOutlet UIDatePicker *fromDate;
- (IBAction)submitDates;

@end
rmaddy
  • 314,917
  • 42
  • 532
  • 579
user2656127
  • 655
  • 3
  • 15
  • 31

2 Answers2

2

There are several methods for passing variables, here is one of the easiest.

- (IBAction)submitDates {
    //Save the selected date variables
    NSDate *dateFromPicker = [_fromDate date];
    NSDate *endDateFromPicker = [_endDate date];
    NSLog(@"From date: %@ and end date: %@", dateFromPicker, endDateFromPicker);

    //Create an instance of the second view controller
    // If you are using NIBs
    JPSecondViewController *secondViewController = [[JPSecondViewController alloc] init];

    //// If you are using storyboards, then you will need to know what the storyboard identifier is for JPSecondViewController. 
    //NSString *identifier = @"<Second Storyboard Identifier>";
    //JPSecondViewController *secondViewController = [self.storyboard instantiateViewControllerWithIdentifier:identifier];

    //Fill in all the data need for the second view controller
    secondViewController.fromDate = dateFromPicker;
    secondViewController.endDate = endDateFromPicker;

    //Show the second view controller
    // Option 1: Add the second view controller to a navigation controller
    [self.navigationController pushViewController:secondViewController animated:YES];

    //// Option 2: Show the second view controller as a modal view controller
    //[self presentViewController:secondViewController animated:YES completion:nil];
}

This creates the second view controller populates the values and presents the second view controller in one of two ways. If first view controller is embedded in a navigation controller, then you can just add the second view controller to the navigation stack. If the first view controller is not in a navigation controller, then present it as a modal.

Jeffery Thomas
  • 42,202
  • 8
  • 92
  • 117
  • Thanks, got a few errors with the variables (Property 'fromDate' not found on object of type 'JPSecondViewController *') but I should be able to crack it! – user2656127 Jul 13 '14 at 12:34
  • Also my second view is just black. I've added a log to the viewDidLoad and it confirms that it loads the second view. Any tips on that? Thanks JeffreY! – user2656127 Jul 13 '14 at 12:40
  • @user2656127 You need add properties to `JPSecondViewController` to hold the begin and end dates. – Jeffery Thomas Jul 13 '14 at 15:48
  • @user2656127 I updated my answer for storyboards in case you are using storyboards. – Jeffery Thomas Jul 13 '14 at 15:59
-5

There are various methods that you can use to pass variables between classes

  1. Singletons (which I prefer)
  2. NSUserDefaults
  • Great! If it's not too much trouble - would you mind helping me with the code to put me in the right direction regarding the switching of views? Then I will implement the two variables as Globals. – user2656127 Jul 13 '14 at 12:14
  • sure it is a pleasure to help, here is the light you are looking for, that I am already using in my apps too: http://stackoverflow.com/a/19604873/3551355 – Ahmet Hayrullahoglu Jul 13 '14 at 13:01
  • 1
    You forgot about Core Data. :) Seriously, take a look at Jeffery Thomas' answer. – anatoliy_v Jul 13 '14 at 13:46
  • @AhmetCevdet, you should also read, what trojanfoe wrote at the and of his answer. – vikingosegundo Jul 13 '14 at 16:34
  • @vikingosegundo okk who is trojanfoe?, so I can read what he wrote in his answer – Ahmet Hayrullahoglu Jul 13 '14 at 16:42
  • well, you linked his answer. – vikingosegundo Jul 13 '14 at 16:46
  • but to emphasize: using singletons to pass objects around is wrong for several reasons. and using NSUserDefaults for that is even worse. – vikingosegundo Jul 13 '14 at 16:48
  • I am not suggesting to pass objects, just to pass variables and also can you suggest the perfect solution to pass variables between classes, I really wonder how professional coders do – Ahmet Hayrullahoglu Jul 13 '14 at 16:54
  • would you mind to explain the difference of objects vs variables, especially if the OP needs to pass nsdates? and professional coders do it like it is shown in the language documentation. – vikingosegundo Jul 13 '14 at 16:57
  • yes you are right, from now on I will give a suggestion of "go look at language documentation".. – Ahmet Hayrullahoglu Jul 13 '14 at 18:04
  • @AhmetCevdet, you shouldn't take this personally, but your answer IS bad for the shown reasons. to answer with "suggest a perfect solution" shows you are easy to offend. maybe you should answer more carefully in fields you have more experience in. otherwise stackoverflow will give you hard times. – vikingosegundo Jul 15 '14 at 19:49
  • I am okay if someone criticise me because of my answers or judge my questions, but it is so rude when blaming someone by explicitly saying u copied someone's answer, I don't think it is the way how people communicate in stack overflow, whatever man just relax – Ahmet Hayrullahoglu Jul 15 '14 at 20:33
  • who said you copied an answer? you are just linking some you obviously didn't read to the end. you should relax, as you seem to ne hallucinate. – vikingosegundo Jul 16 '14 at 06:42