0

SEE EDIT AT BOTTOM

I am making a UtilityView application for the iPhone and I want to be able to change values from the FlipsideViewController. I have a problem that whenever I switch back to the MainView from the FlipsideView, the values of the text fields that I had changed in the FlipsideView are seemingly reset to 0. In the MainViewController.m, under the flipsideViewControllerDidFinish:

playToInt = [controller.playTo.text intValue];

computerMoveSpeed = [controller.aiSpeed.text intValue];

The text field 'playTo' coordinates with the integer 'playToInt' and the text field 'aiSpeed' coordinates with the integer 'computerMoveSpeed'. I think this should save the values, even when the ViewDidLoad runs again, but they both seem to be reset to 0.

if (playToInt != 10 || computerMoveSpeed != 3)   
{
  playBool = true;
} 
else 
{
  playBool = false;
}

I know that the boolean is working because I can change the integers' values with it, but they always default back to 0. Any help is appreciated. Thanks in advance.

NOTE: I have consulted the previous question 'Passing UISegmentedControl values from FlipSideViewController in an Utility application to the mainviewcontroller…' and that does not work for me... I am have too many possible values to use an index or a switch.

EDIT: I am trying something that I think is really close to working... I feel like it just has bad syntax or something... I have in the MainViewController.m:

computerMoveSpeed = [FlipsideViewController.aiSpeed.text intValue];

playToInt = [FlipsideViewController.playTo.text];

EDIT: I have something that is similar to the last one but I think it will have a better chance of working with some help...

in the FlipsideViewController.m I have

_playToNumber = [_playTo.text intValue];

and in the MainViewController.m

playToInt = [controller.playToNumber];

Any help is greatly appreciated, but as some additional info, on the string in the MainViewController it says it needs an identifier on the playToNumber variable, but it is declared as int... I don't know if there is an identifier for ints but if there is, please let me know!

EDIT: Is there any way to save the values in a usable format for the MainViewController using the prepareForSegue function in the FlipsideViewController?

EDIT: I figured out that it was just getting the initial values when it loaded the ViewDidLoad, so I started using a timer to retrieve the values. This seems to be working, I just need to know how to access these values from the MainViewController?

  • use NSUserDefault follow my ans http://stackoverflow.com/questions/13509999/i-have-nsstring-value-in-addviewcontroller-and-i-want-to-display-this-value-in-u/13619879#13619879 – Rajneesh071 Dec 15 '12 at 05:43
  • I don't need to set the defaults, I already did that... I need to be able to save the values that I change in the flipside and use them in the mainView –  Dec 15 '12 at 05:49
  • 1
    are you using pushViewControlller – Rajneesh071 Dec 15 '12 at 06:24
  • when view did load called then the all the view again allocating thats why ... – Rajneesh071 Dec 15 '12 at 06:28
  • you want value of playToInt as [controller.playTo.text intValue];..not 0, m i right – Rajneesh071 Dec 15 '12 at 06:29
  • yes, I want playToInt and computerMoveSpeed to != 0 unless specified in the flipside; i am not using pushViewController –  Dec 15 '12 at 17:40
  • make object of an string like @property(non,ret)nsstring *string; – Rajneesh071 Dec 17 '12 at 07:37
  • 1
    then paas your value to this string ..and in viewWillAppear put this string value in where you want – Rajneesh071 Dec 17 '12 at 07:37
  • When I try to pass the value to the NSInteger using 'NSInteger *number = [FlipsideViewController._playTo.text IntegerValue];' it tells me that "property '_playTo' is not found on any type of Flipside View Controller"... is there a better way to do this? @Rajneesh071 –  Dec 18 '12 at 14:11
  • first off all its 'NSInteger number=...' – Rajneesh071 Dec 19 '12 at 11:12
  • 1
    and in FlipsideViewController, make property of _playTo, like @property(retain,.)..and synthesize it then use – Rajneesh071 Dec 19 '12 at 11:13
  • hey dont get value from previous view, just send value from previous view, mean when you are getting your view from FlipsideViewController, then send value to number, @property(nonatomic)NSInteger *number; – Rajneesh071 Dec 19 '12 at 12:08
  • could you clarify a little? @Rajneesh071 –  Dec 19 '12 at 17:15
  • I just discovered that the flipsideViewController isn't even changing the values within itself... i used nslog and it gave me the default, 10, but it wouldn't change when i typed in it –  Dec 19 '12 at 17:51
  • never mind... i just realized that the viewdidload only runs once so i put a timer and it works now –  Dec 19 '12 at 18:27
  • I have in the `FlipsideViewController.m` : `_playToNumber = [_playTo.text intValue];` and in the `MainViewController.m` `playToInt = [controller.playToNumber];` I think I am really close, but it says 'expected identifier' on the second string of code –  Dec 19 '12 at 18:44
  • no... it is not solved, but I feel like i am close... @Rajneesh071 –  Dec 30 '12 at 05:51
  • I have a class that is connected to a timer when the `FlipsideViewController` is called and this has the values being updated, even when the `FlipsideViewController` is dismissed.... Is there a way to access this from the `MainViewController.m`? –  Dec 30 '12 at 06:07
  • can I use the - (void)run { } –  Dec 30 '12 at 06:30

2 Answers2

0

Use delegate to send values back to MainViewController. See this tutorial to learn how to use delegates.

Atif Azad
  • 1,294
  • 1
  • 16
  • 30
0

You have many way to do this task,

1. NSUserDefault

Follow ma answer Link

2. Using Getters, setters, property

Follow ma answer Link

EDIT

If you want int or NSInteger then no need for retain
just do

@property(nonatomic) NSInteger YourIntValue;

and

_playToNumber = [_playTo.text intValue];

MainViewController *MVC = [[MainViewController alloc] init];
MVC.playToInt=_playToNumber;
[self.navigationController pushViewController:MVC animated:YES];
Community
  • 1
  • 1
Rajneesh071
  • 30,846
  • 15
  • 61
  • 74
  • followed instructions, but I need it to be an `int` or `NSInteger` value, not a `NSString` and when I tried, it told me that the `copy` attribute had to be of object type... –  Dec 20 '12 at 23:03
  • it runs without errors, but how do i get it to be the same value as my `flipside` value? –  Dec 21 '12 at 19:44
  • how are you moving from the class flipside – Rajneesh071 Dec 22 '12 at 06:49
  • sorry for the long reply... I had no internet for a week, but I am using the default info button and done button (in the navigation bar) that are installed on the `view` by default. –  Dec 30 '12 at 05:51
  • when you are moving to your new view then paas value that time...and use in new view.. – Rajneesh071 Dec 30 '12 at 09:21
  • MainViewController *MVC = [[MainViewController alloc] init]; MVC.playToInt=_playToNumber; then push to new view – Rajneesh071 Dec 30 '12 at 09:21
  • how do you push it to the main view? does it do it automatically? –  Dec 31 '12 at 03:46