Passing an int to a new ViewController - iphone

Ive spent a month trying to pass my int to a new view. My int is called score and is connected to a label called scorelab. The user increases the score and once the they run out of time game switches views to game over screen and i would like the users score to be displayed on the game over screen in a label. The game play controller is called GamePlayViewController and the gave over controller is called GameDidEndViewController. I'm tired of trying to get this to work. I have looked at every tutorial possible but nothing seems to work for my case. Please help me i would appreciate it so so much, this is the very last part of game that I need to finish. Thank you in advance!
Sorry here is the code:
GamePlayViewController.h
#import "GameDidEndViewController.h"
int score;
IBOutlet UILabel *scorelab;
GamePlayViewController.m
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"GameOver"])
{
GameDidEndViewController *vc = [segue destinationViewController];
[vc setscore2: score];
}
}
GameDidEndViewController.h
#import "GamePlayViewController.h"
IBOutlet UILabel *scorelab2;
int score2;
}
#property (nonatomic , assign) int score2;
#property (nonatomic , assign) UILabel *scorelab2;
GameDidEndViewController.m
#synthesize score2 , scorelab2;
-(void)viewWillAppear:(BOOL)animated {
scorelab2.text = [NSString stringWithFormat: #"%d", score2];
[super viewWillAppear: animated];
}

What you do is create a property in GameDidEndViewController to hold the passed in score value.
#interface GameDidEndViewController : UIViewController {
....
}
#property(nonatomic,assign) int score;
#end
Make sure you #synthesize score in the #implementation(.m).
Now when you init and show the GameDidEndViewController you set the score, likely something like this.
GameDidEndViewController *end = .....// init the view.
end.score = score; // assuming you're doing this from GamePlayViewController where it has an int named `score`
Hope this helps !

Here is some tutorial for passing data between ViewControllers using Storyboard:
Storyboards Segue Tutorial: Pass Data Between View Controllers
Tutorial on How-To Pass Data Between Two View Controllers
or use something like this:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:...]) {
MyViewController *controller = (MyViewController *)segue.destinationViewController;
controller.score = score;
}
}
Hope this helps.

Related

Your Second iOS App: Edit one object in two different views

I am going through the tutorials provided by Apple and tried to improve "My Second iOS App", the app for bird sightings.
(There is a MasterView where all entered sightings are listed. If you click one, you are directed to a DetailView of the sighting. You can add a sighting and are asked to enter a name and location.)
I want to sepperate the views for entering the birds name and location.
So I have two views (one for entering the name and one for entering the location) and one object I want to store.
In the file BirdSighting.m I added the following methods
-(id)initWithNameOnly:(NSString *)name date:(NSDate *)date
{
self = [super init];
if (self) {
_name = name;
_date = date;
return self;
}
return nil;
}
and
-(id)setLocation:(NSString *)location
{
if (self) {
_location = location;
return self;
}
return nil;
}
In the AddSightingNameViewController.m I implemented the following code
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"ToLocation"])
{
if ([self.birdNameInput.text length])
{
BirdSighting *sighting;
NSDate *today = [NSDate date];
sighting = [[BirdSighting alloc] initWithNameOnly:self.birdNameInput.text date:today];
self.birdSighting = sighting;
}
}
}
The view for entering the name leads with a push segue to the location-view. There has'nt been changed much else.
Now how do I pass the object generated in the first view to the second? And how do I call the setLocation method on this specific object in AddSightingLocationViewController.m? Do I have to define different properties? And how do I finally display the object in the MasterView with the correct data after I entered the location?
As this code is not working yet, I don't even know if it is working, what I am trying to do. So please be gentle, if this is crappy code.
This is the method I have been using:
First you will need to add a property in your destination view controller, to hold the object you want to pass:
#property (strong, nonatomic) BirdSighting *newSighting;
Then change the prepareForSegue method in your first view controller to the following:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"ToLocation"])
{
if ([self.birdNameInput.text length])
{
BirdSighting *sighting;
NSDate *today = [NSDate date];
sighting = [[BirdSighting alloc] initWithNameOnly:self.birdNameInput.text date:today];
self.birdSighting = sighting;
// Get destination view
YourDestinationViewController *vc = (YourDestinationViewController *)segue.destinationViewController;
// Pass birdSighting object to your destination view controller
[vc setNewSighting:self.birdSighting];
}
}
}
I think I originally got this method from this question
It is also worth noting that the BirdSighting class has a location #property in it's .h file & you will notice the #synthesize line in the .m file.
The #synthesize directive automatically creates accessor methods for you:
#property (nonatomic, copy) NSString *location;
Has the following methods automatically generated (but not visible in the file):
- (NSString *)location;
- (void)setValue:(NSString *)location;
Therefore it was unnecessary for you to override the setter method for location in the BirdSighting.m file with:
-(id)setLocation:(NSString *)location
If you remove that method (note that it should return void not id) you should now be able to access the location variable in a BirdSighting object in the following way:
// In this example we are accessing a BirdSighting #property (hence the use of self.sighting)
// #property (strong, nonatomic) BirdSighting *sighting;
// Getter - returns (NSString *)location of the BirdSighting object
[self.sighting location];
// Setter - sets the location property of the BirdSighting object to 'newLocation'
[self.sighting setLocation:newLocation];
Hope this clears some things up for you!

How to send data from UItextfields of one viewcontroller to new view controller? [duplicate]

I am a bit stuck on trying to pass data from two UITextfields on one viewcontroller to another viewcontroller. Basically I got the items below:
ViewController.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController {
IBOutlet UITextfield *Textfield1;
IBOutlet UITextfield *Textfield2;
}
-(IBAction)GoNextView:(id)sender;
#end
ViewController.m
#interface ViewController ()
#end
#implementation ViewController
(IBAction)GoNextView:(id)sender {
SecondView *second = [[SecondView alloc] initWithNibName:nil bundle:nil];
[self presentViewController:second animated:YES completion:NULL];
}
- (void)viewDidLoad {
[super viewDidLoad];
}
SecondView.h
#import <UIKit/UIKit.h>
#interface SecondView : UIViewController {
IBOutlet UILabel *ShowTextfield1;
IBOutlet UILabel *ShowTextfield2;
}
-(IBAction)GoBack:(id)sender;
#end
SecondView.m
#interface SecondView ()
#end
#implementation SecondView
- (IBAction)GoBack:(id)sender {
[self dismissViewControllerAnimated:YES completion:NULL];
}
What I want is, that when the button GoNextView is pressed it should pass the data in the UITextfield to the SecondView. And the go to the next page. The data should now be displayed in the two Labels on the SecondView. I've been struggling with this for many hours now and its killing me. I know I have to use some NSString and #property(nonatomic, retain) NSString *asdas. But I simply can't make it work. So based on the above code how do I pass data from the two UITextfield to the two labels on the next view?
Instead of navigating to the second view through a function, use Storyboard Segues. Create a storybaord application. To Create a segue you need a navigation view controller. You can do that by the following steps.
Select the first view controller and go to Editor -> Embed In -> Navigation Controller.
Then Ctrl+Click on the "GoNextView" button, and drag and drop in the SecondView.
You will get three options: push, model, custom. Click push then a segue will be created between these two views.
Click that segue and click the "Show the Attribute Inspector" on the right side property window. Now name it as "connector" or which ever name you prefer.
Now, in SecondView.h create two strings,
#property (nonatomic, retain) NSString *str1;
#property (nonatomic, retain) NSString *str2;
and synthesize them in SecondView.m.
Lets name your UITextFields as "text1" and "text2" which are in your ViewController. Go to the ViewController.h and add #import "SecondView.h"
In ViewController.m do the following,
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"connector"])
{
ViewController *VC = [segue destinationViewController];
VC.str1 = text1.text;
VC.str2 = text2.text;
}
}
Now you can display these strings in your SecondView. In Secondview.m,
label.text = str1;
and it will be displayed.
Use NSUserDefaults or the normal synthesising common interface variables. For example,
NSUserDefaults
From sender's view controller,
NSString *text=textField1.text;
NSUserDefaults *prefs=[NSUserDefaults standardUserDefaults];
[prefs setObject:text forKey:#"text1value"];
And you can get the value from any view controller as,
NSUserDefaults *prefs=[NSUserDefaults standardUserDefaults];
NSString *received string= [prefs stringForKey:#"text1value"];
Normal assigners,
I believe you must be using performsegue to navigate view controllers. There is a delegate method called "prepareforsegue" and you can assign the textfield values into the interface variables. For example,
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"seguename"]) {
destViewController *destViewController = segue.destinationViewController;
destViewController.name = #"some text";
}
}
In the receiver's view controller, getter variable is actually defined as,
In .h file, #property(nonatomic, strong) NSString *name;
In .m file, Don't forget to synthesis the variable after implementation
#implementation destViewController
#synthesize name;
Hope it helps!

iPhone: How do I share information between my UITabBarViewController and the individual Tabs?

I have a TableViewController that segues into a TabBarViewController.
I know how to pass my object via a segue, but not by a relationship like the TabBarViewController and it's tab share.
How can I do this? From the TabView is there a way to access the TabBarViewControllers member variables?
Update:
This is how I've solved the problem so far, but I'm not crazy about using the AppDelegate to do this...
Add the Following to WhateverYouNamedYourAppDelegate.h
#class myObjectIWantToPass;
#property (strong, nonatomic) myObjectIWantToPass *object;
Then add the following to the View Class file you have your data in that you want to pass on. I'm going to assume you know how to set up your object already in this file if your planning on passing it to another view.
WhateverYouNamedYourAppDelegate *appDelegate =
(WhateverYouNamedYourAppDelegate *)[[UIApplication sharedApplication] delegate];
appDelegate.object = object;
Then you do some similar work to retrieve the object back from the appDelegate in your destination View Class.
WhateverYouNamedYourAppDelegate *appDelegate = (WhateverYouNamedYourAppDelegate *)[[UIApplication sharedApplication] delegate];
object = appDelegate.object;
You can make singleton classes, so that all of the controllers can access those variables in the Singleton. See Code below
SingletonClass.h
#interface SingletonClass : NSObject {
NSString *someString;
}
#property(nonatomic, retain)NSString *someString;
+(id)shared;
#end
SingletonClass.m
#import "SingletonClass.h"
static SingletonClass *aShared;
#implementation LibShared
#synthesize someString;
+(id)shared
{
if (aShared == nil) {
aShared = [[self alloc] init];
}
return aShared;
}
-(id)init
{
if (self = [super init]) {
}
}
-(void)dealloc
{
[someString releease];
[super dealloc];
}
In the tabbar you can set the variable on SingletonClass:
[[SingletonClass shared] setSomeString:#"Value_Set"];
On the tableViewController, you can get the property of the someString variable on the SingletonClass:
NSString *string = [[SingletonClass shared] someString];
There's no need for a singleton pattern here. Instead, you can send the data-object forwards in - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender just as you do normally, you just need to find the correct viewController in the UITabBarController's viewControllers property.
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"MyTabBarSegue]) {
UITabBarController *tabBarController = segue.destinationViewController;
// Either set the index here, if you know for sure which viewController is which, or
// Enumerate the viewControllers for isKindOfClass:[MYCustomViewController class] to be robust and change-proof
MYCustomViewController *myVC = [[tabBarController viewControllers] objectAtIndex:0];
myVC.dataObject = self.dataObject;
}
}

Accessing variable values from one view in another

I'm having some trouble understanding how variable values are passed from one view to another. I have a UITextField in the firstview that the user enters a number into. When the user taps a button, that number is multiplied by 2 and the result is displayed on a UILabel in the second view. This is what I have thus far
FirstViewController.h
#interface FirstViewController : UIViewController{
UITextField *numberTextField;
NSNumber *aNumber;
}
#property (nonatomic, retain) IBOutlet UITextField *numberTextField;
#property (nonatomic) NSNumber *aNumber;
-(IBAction)calculate;
#end
FirstViewController.m
#implementation FirstViewController
#synthesize numberTextField, aNumber;
-(double)doubleNumber{
double number = [numberTextField.text doubleValue] * 2;
return number;
}
-(IBAction)calculate{
self.aNumber = [NSNumber numberWithDouble:[self doubleNumber]];
}
//more default code continues below
SecondViewController.h
#import "FirstViewController.h"
#interface SecondViewController : FirstViewController{
UILabel *numberLabel;
}
#property (nonatomic, retain) IBOutlet UILabel *numberLabel;
#end
SecondViewController.m
#implementation SecondViewController
#synthesize numberLabel;
- (void)viewDidLoad
{
[super viewDidLoad];
numberLabel.text = [NSString stringWithFormat:#"%#",aNumber];
}
Best and Easy Way to store value globally
set you object with your keyword
[[NSUserDefaults standardUserDefaults] setObject:#"Ajay" forKey:#"name"];
than get that object any where in you project
NSString *name = [[NSUserDefaults standardUserDefaults] objectForKey:#"name"];
You should accomplish what you want by using a segue. Create a segue in your storyboard and then call it in your firstviewcontroller with - (void)performSegueWithIdentifier:(NSString *)identifier sender:(id)sender. Then to pass data, import your secondviewcontroller.h file with a property for the value you want to pass and setup - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender.
In that method you can pass things by using [segue.destinationViewController ###call the setter for the property in your secondViewController.h file###];
If this isn't clear, or you need more help just let me know.
I'm assuming that the FirstViewController is instantiating the SecondViewController. If that is so, then you just pass aNumber to the SecondViewController using an additional property:
// add an additional property to the SecondViewController
#property (nonatomic, retain) NSNumber *aNumber;
When you instantiate the SecondViewController inside the FirstViewController, you just pass that value to the SecondViewController before you load it:
// inside FirstViewController
SecondViewController *secondViewController = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
secondViewController.aNumber = aNumber;
// inside SecondViewController
- (void)viewDidLoad
{
[super viewDidLoad];
numberLabel.text = self.aNumber;
}
Mmm... Pay attention to not confuse views and viewControllers!!!
A viewController can manage more than a view. For example in your code you have a UITextField, a UILabel and probably a UIButton. These are all views that are managed by two viewsController (FirstViewController and SecondViewController).
As long as you have so few views to work with you can use just one viewController and pass the value you want to your UILabel directly:
- (void)calculateAndPassValue
{
aNumber = [NSNumber numberWithDouble:[self doubleNumber]];
numberLabel.text = [NSString stringWithFormat:#"%#",aNumber];
}
Otherwise, if your goal is passing variable values from one viewController to another. Well... There are many ways you can obtain that, for example:
Creating an ivar.
Using a singleton.
Saving your data in NSUserDefault.
Creating a database on disk.
First and second cases are good if you need to manage your data while your app is running. Third and fourth if you want to memorize your data for future use and retrieve them at next start up.
Try to search keys like ivar, singleton, NSUserDefault and you'll find many discussions and lines of sample code.

iPhone - passing data from child to parent in navigation stack

Dear fellow programmers,
What is the best way to pass information from a child in a navigation stack up one or more levels? I have searched online and can't seem to find the best way to begin implementing this.
Specifically this is my model: I have a grouped table view with several rows. When a user clicks on a row I take them to an MVC .ie click on 'select date' row takes them to a MVC with a datepicker setup (the parent passes data to the child in the process). Once this date picker (or other piece of data) has been selected, how do I pass the data back? Heres some code of the setup if it helps:
/* following is inside didSelectRowAtIndexPath of parent in navigation stack */
if (row == 1) {
DateViewController *datevc = [[DateViewController alloc] init];
datevc.selectedDate = [self dateToEnter];
[self.navigationController pushViewController:datevc animated:YES;
[datevc release];
}
DateViewController.h
#import <UIKit/UIKit.h>
#interface EnterProcedureDateViewController : UIViewController {
IBOutlet UIDatePicker *datePicker;
NSDate *selectedDate;
}
#property (nonatomic, retain) IBOutlet UIDatePicker *datePicker;
#property (nonatomic, retain) NSDate *selectedDate;
-(IBAction)selectButtonPressed:(id)sender;
#end
DateViewController.m
#import "DateViewController.h"
#implementation EnterProcedureDateViewController
#synthesize datePicker;
#synthesize selectedDate;
-(IBAction)selectButtonPressed:(id)sender {
/* how do I pass the date pickers date back to the ivar dateToEnter of parent?? */
[self.navigationController popViewControllerAnimated:YES];
}
-(void)viewWillAppear:(BOOL)animated {
if (selectedDate == nil) {
datePicker.date = [NSDate date];
} else {
datePicker.date = selectedDate;
}
[super viewWillAppear:animated];
}
...
#end
Thanks in advance as always
Andy
UINavigationController has a property, viewControllers, which returns an NSArray of all the view controllers in the stack.
The "parent" view controller (i.e. the one below your currently visible one) can be obtained by:
TableViewController *parentViewController = (TableViewController*)[self.navigationController.viewControllers objectAtIndex:self.navigationController.viewControllers.count - 2];
You can then call any methods or set properties as required.
In call backs, one of the best and recommended ways to do it is Delegates.
In your case, you will need to create a DataViewControllerDelegate and put here all the methods that Delegates of this class can call. Now, the parent should implement this delegate and register itself as the delegate when initializing it.
It's better with an example, take a look at Jonathan's answer on making custom delegates.
Something like this?
I can't see all of your code structure, so I've made some assumptions about inheritance and class structure.
Get's the view of the parent view controller, casts it to the appropriate subclass of UIView and passes a message to it.
[((MayNeedCastToSpecificClass*)[self.view parentViewController].view) setMyDate:date];
in your parentviewcontroller add the property
#property (nonatomic, retain) NSDate *selectedDate;
in childViewController after selecting the date
parentViewController *parentViewController =[self.navigationController.viewControllers objectAtIndex:self.navigationController.viewControllers.count - 2];
parentViewController.selectedDate = self.selectedDate;
[self.navigationController popViewControllerAnimated:YES];
then you can access the property from parentviewcontroller