0

This is my situation. I'm using NSTimer, which counts 15 seconds but I have also button, which I use to stop this timer earlier. What's more this button takes me to the next tableViewController. The second parameter is date - current time in Center European. If I click a button I need to move to the next TableViewController, and put this two parameters to the TableViewController. This is my code:

**FirstViewController:**

    import UIKit

    class FirstViewController: UIViewController {

        var timer = NSTimer()
        @IBOutlet weak var labelCounter: UILabel!


            override func viewDidLoad() {
                super.viewDidLoad()
                timer = NSTimer.scheduledTimerWithTimeInterval(1,target:self, selector: Selector("update"),userInfo: nil, repeats :true)
            }

            func update(){

                    timer.invalidate()
             }

            override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
        if (segue.identifier == "segueTest") {
            var transfer = segue!.destinationViewController as SecondViewController
            transfer.toPass = labelCounter.text
            transfer.toPass2 = "\(NSDate())"
        }

    }

            @IBAction func buttonPressed(button: UIButton){

                if labelCounter.text > "0" {

                   timer.invalidate()

                }


SecondViewController:
import UIKit

class ThirdViewController: UIViewController {


    @IBOutlet weak var label1: UILabel!
    @IBOutlet weak var label2: UILabel!

    var toPass: String!
    var toPass2: String!


    override func viewDidLoad() {
        super.viewDidLoad()
    label1.text = toPass2
    label2.text = toPass
        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

This code work fine :) In storyboard i had to connect firstViewController with SecondViewController. I did that by connected my button with the secondViewController and setting the segue identifier to "segueTest"

Tannis
  • 47
  • 1
  • 11
  • 1
    possible duplicate of [Passing Data between View Controllers](http://stackoverflow.com/questions/5210535/passing-data-between-view-controllers) – Grzegorz Krukowski Oct 28 '14 at 09:45
  • You can use `self.performSegueWithIdentifier("mySegue",sender:self)` to invoke the segue from `buttonPressed`. The rest is explained here- possible duplicate of [Pass variables from one ViewController to another in Swift](http://stackoverflow.com/questions/24044108/pass-variables-from-one-viewcontroller-to-another-in-swift) – Paulw11 Oct 28 '14 at 09:48
  • I solved this problem, I edit my code above. – Tannis Oct 28 '14 at 15:07

4 Answers4

2

I don't know it's useful or not for you

BViewControl.h

    @interface Showdeals : UIViewController
    {
        NSString *TotalJSonString;
        NSString *PassingString;
        NSMutablearray *setarray;
   }
   @property (strong,nonatomic)NSString *TotalJSonString;
   @property (strong,nonatomic)NSString *PassingString;

BViewControl.m

- (void)viewDidLoad
{
 [setarray addObject:TotalJSonString];
 [setarray addObject:PassingString];
}

finally you setarray pass to UITableView

AViewControl.m in button action

BViewControl *show=[[BViewControl alloc]initWithNibName:nil bundle:nil];
    [show setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
    show.TotalJSonString=string1;
    show.PassingString=string2;

    [self.navigationController popToRootViewControllerAnimated:TRUE];
    [self presentViewController:show animated:YES completion:NULL ];
Pavan Alapati
  • 317
  • 1
  • 5
  • 17
0

Create a property in secondview controller. and assign values from current view controller to secondview controller property using class object.

hardik hadwani
  • 556
  • 6
  • 24
0

Override prepareForSegue

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
    // Get the new view controller using segue.destinationViewController.
    // Pass the selected object to the new view controller.

    //CHECK SEGUE IDENTIFIER HERE IF YOU HAVE MULTIPLE SEGUES
    (segue.destinationViewController as MyViewController).property = value // you can repeat this for any number of properties in your destination view controller
}
Rajeev Bhatia
  • 2,974
  • 1
  • 21
  • 29
  • I thinking about this method of passing data, so if i do this how can i connected two views in interface builder? – Tannis Oct 28 '14 at 10:22
  • On what action are you displaying the view controller? If it is on the click of a button or selection of a tableview, connect a segue from that UI element to the view controller. http://www.raywenderlich.com/5191/beginning-storyboards-in-ios-5-part-2 – Rajeev Bhatia Oct 28 '14 at 11:04
0

You should use prepareForSegue indeed. Here is the code:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{

if ([segue.destinationViewController respondsToSelector:@selector(setMyData:)]) {
    [segue.destinationViewController performSelector:@selector(setMyData:) 
                                          withObject:myData];
    } 
}

In the destination ViewController, you should have

@property (nonatomic, strong) MyData *myData;

If you want to pass more than one value, you can make an object, with those two values, and then pass an object as data.

Or, you can make an array, where you would put your data, or dictionary, and pass that as your data.

so, MyData can be NSString, NSArray, NSDictionary, or whatever you need :)

EDIT:

There is another thing you can do, and it doesn't include segues at all. You could make static variables, that exist only throughout your app life cycle.

What I always make in all of my apps, is a Constants class.

here is some code:

Constants.h

@interface Constants : NSObject

+ (Constants*)shared;
@property NSString* constantA;
@property int constantB;
@property NsMutableArray array;

@end

Constants.m

@implementation Constants

static Constants *constants = nil;


+ (Constants*)shared {

if (nil == constants) {

    if (constants == nil) {
        constants = [[[self class]alloc] init];
     }
    // Remember to initialize an array (this is just an example, if you need an array. if you need only one int and string for example, skip this)
    [Constants shared].array = [[NSMutableArray alloc]init];
  }

 return constants;
 }

Now, let's say you have 2 ViewControllers, and you want to read some variables from the first one, when you segue to another one.

in the first ViewController, simply do this:

[Constants shared].constantA = yourValue1;
[Constants shared].constantB = yourValue2;

Then, when you get to the secont ViewController, read your values from Constants class like this:

int        firstVal = [Constants shared].constantA;
NSString* secondVal = [Constants shared].constantB;

Remember to #import "Constants.h" in both ViewControllers.

This is fairly easy to do in Swift as well. I don't know it's semantic yet, but I'm sure you can figure it out :)

EDIT 2

If one of the variables has to be the time, and it depends on the time when user presses the button, you can do the following (In this example, you will need a time when user pressed the button in this format: @"yyyy-MM-dd HH:mm:ss"

-(IBAction) onButtonClick
{
 NSDateFormatter *DateFormatter=[[NSDateFormatter alloc] init];
[DateFormatter setDateFormat:@"yyyy-MM-dd%20HH:mm:ss"];
NSString dateTime  = [DateFormatter stringFromDate:[NSDate date]];

 [Constants shared].time = dateTime;

 // and after you do it, you can segue to another ViewController, like you do
 let view = self.storyboard?.instantiateViewControllerWithIdentifier("segue") as TableViewController
self.navigationController?.pushViewController(view, animated: true)

}
SteBra
  • 4,188
  • 6
  • 37
  • 68