how do i get one user input value form one ViewController, to appear in another? (extern/global variables?) - xcode

i have a set up 2 sets of Objective-C classes (no xib file)
AdultTicketCalculatorViewController
//.h file
#import <UIKit/UIKit.h>
#interface AdultTicketCalculatorViewController : UIViewController {
IBOutlet UITextField *Quantity;
IBOutlet UILabel *Pricelabel;
IBOutlet UILabel *TotalLabel;
}
-(IBAction)calculate;
-(IBAction)clear;
#end
//.m
#import "AdultTicketCalculatorViewController.h"
#interface AdultTicketCalculatorViewController ()
#end
#implementation AdultTicketCalculatorViewController
-(IBAction)calculate {
float x = ([Quantity.text floatValue]);
float c = x*15;
TotalLabel.text = [[NSString alloc] initWithFormat:#"$%.2f", c];
}
-(IBAction)clear {
Quantity.text =#"";
Pricelabel.text =#"$15.00";
TotalLabel.text =#"";
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[Quantity resignFirstResponder];
}
AdultPayNowViewController
//.h
#import <UIKit/UIKit.h>
#interface AdultPayNowViewController : UIViewController {
IBOutlet UILabel *Quantity ,*Price ,*QuantitySpace ,*TotalSum ,*TotalSumSpace;
IBOutlet UIButton *ConfirmPurchase;
}
#end
//.m
(i havent implemented anything yet)
i want to take some user input and calculated values form AdultTicketCalculator and display them within AdultPayNowViewController in the following manner :
UITextField 'Quantity' (AdultTicketCalculatorViewController) to appear in UILabel 'QuantitySumSpace (AdultPayNowViewController)
UILabel 'TotalLabel' (AdultTicketCalculatorViewController) to appear in UILabel 'TotalSumSpace'

you can achieve this one by using two different ways.
1.save the data whatever you want in AdultTicketCalculatorViewController this class in NSUserDefaults and retrieve the data in AdultPayNowViewController.
like this :
whenever pushing your view place this code or in
- (void)viewDidDisappear:(BOOL)animated{
//place here
}
in AdultTicketCalculatorViewController
NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
[defaults setObject: TotalLabel.text forKey:#"TotalLabel"];
[defaults setObject: Quantity.text forKey:#"Quantity"];
[defaults synchronize];
and re assign the textfield value in viewWillAppear method.
AdultPayNowViewController:
in viewWillAppear method place this following code.
NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
TotalSumSpace.text= [defaults valueForKey:#"TotalLabel"];
QuantitySumSpace.text= [defaults valueForKey:#"Quantity"];
2.Synthesize the string objects in AdultPayNowViewController and assign the values when you push the screen from AdultTicketCalculatorViewController.

Related

How to import an UILabel from one UIViewcontroller to another

I've got a problem with xcode.
I'm a noob with object-c and xcode so... please help.
I have 2 Viewcontrollers: ViewController (with .m/.h) and HighScores (with .m/.h).
In HighScores I have put a label called first. And in ViewController I have a UITextField called *textField. I want the text in textField to be in the label when I enter the text AND when the score of the game already played is grater than the text already existing in the label ('first').
So,
This is how my HighScore.h looks like:
#import <UIKit/UIKit.h>
#interface HighScores: UIViewController {
IBOutlet UILabel *first;
}
#end
and this is ViewController.m:
#import "ViewController.h"
#import "HighScore.h"
...
NSString *myString = [HighScores.first];
if (score.text > myString) {
NSString *string = [textField text];
[HighScores.first setText:string]
but xcode says there's an error when I type "first" after the dot '.'... How can I make it if I want xCode to recognize the "first" label from HighScore UIViewController in VewController UiViewController?
Thanks!
In your code "first" is a UILabel , it will be generated when view of highScores get loaded .
Because it is a IBOUtlet.
Secondly you are trying yo access with class name .
Make a instance of HighScore class first and than try to acess label "first".
#import <UIKit/UIKit.h>
#interface HighScores: UIViewController
#property (nonatomic , strong)UILabel *firstLabel ;
#end
#implementation HighScores
- (id)initWithNibName:(NSString *)nibName bundle:(NSBundle *)nibBundle
{
self.firstLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 50)];
[self.view addSubview self.firstlabel];
}
#end
than in ViewController.m
HighScore * highscoreObject = [[HighScore alloc]init];
NSString *mystring = [highscoreObject.firstLabel text];
if (score.text > mystring) {
[highscoreObject.firstLabel setText:score.text];
{
Lets use notification , if you are confused in here:
In this case you can also use IBoutlet.
We will throw an notification with the string to be set and read the notification in HighScores and set the label with the string send.
In ViewController.m
if (score.text > myString) {
NSString *string = [textField text];
[[NSNotificationCenter defaultCenter] postNotificationName:#"update" object:string];
}
#interface HighScores: UIViewController
#property (nonatomic , strong) IBOutlet UILabel *firstLabel ;
#end
and in HighScores.m
#implementation HighScores
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] removeObserver:self];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(changetext:) name:#"update" object:nil];
}
- (void) changetext:(NSNotification *)notification {
NSLog(#"Received");
self.firstLabel.text = [notification object];
}

blank screening after going back

I've recently created a app with Xcode. When 2 images collide, it sends me to a new screen (which is good). However, when i push the button to return me back to the original view, it goes blank. Can you tell me why? Any feed back is appreciated.
Here is code from the First .h file:
import
#interface ViewController : UIViewController {
IBOutlet UIImageView *image1;
IBOutlet UIImageView *image2;
}
#property (nonatomic, retain) UIImageView *image1;
#property (nonatomic, retain) UIImageView *image2;
-(void)collision;
#end
And from the .m:
import "ViewController.h"
import "newview.h"
#interface ViewController ()
#end
#implementation ViewController
#synthesize image1, image2;
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *mytouch = [[event allTouches] anyObject];
image1.center = [mytouch locationInView:self.view];
[self collision];
}
-(void)collision {
if (CGRectIntersectsRect(image1.frame, image2.frame)) {
newview *second = [[newview alloc] initWithNibName:nil bundle:nil];
[self presentModalViewController:second animated:YES];
}
}
here is code from the second .h (The one with the go back button):
(IBAction)retry:(id)sender;
and from the .m:
import "newview.h"
import "ViewController.h"
#interface newview ()
#end
#implementation newview
(IBAction)retry:(id)sender {
ViewController *second = [[ViewController alloc] initWithNibName:nil bundle:nil];
[self presentModalViewController:second animated:YES];
}
The blank screen is another modal view from (IBAction)retry:(id)sender.
You should use [self dismissModalViewControllerAnimated:YES]; in (IBAction)retry:(id)sender for going back to previous screen instead of presenting another modal view.

how to set the property of the ViewController before presenting it as presentModalViewController?

I present the view controller like this:
FBViewController *fbViewController =[[FBViewController alloc] initWithNibName:#"FBViewController" bundle:nil];
fbViewController.label.text=#"hello"; // I set the value of the property label which is the outlet
[self presentModalViewController:fbViewController animated:YES];
FBViewController.h:
#import <UIKit/UIKit.h>
#interface FBViewController : UIViewController
#property (weak, nonatomic) IBOutlet UILabel *label;
#end
FBViewController.m:
...
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(#"%#", self.label.text); // Here instead of "hello" i get the value which was in nib file.
}
...
The question is how to set the value of the label?
You can pass the NSString retrieve text to assign label modalview controller:
FBViewController *fbViewController =[[FBViewController alloc] initWithNibName:#"FBViewController" bundle:nil];
fbViewController.labeltext=#"Your Text";
[self presentModalViewController:fbViewController animated:YES];
FBViewController.h
#interface FBViewController : UIViewController {
NSString *labeltext;
}
#property (nonatomic, retain) NSString *labeltext;
and use view to load method in FBViewController.m
- (void)viewDidLoad {
label1.text=labeltext;
}

Accessing NSTextField from its delegate notification…

I'm subclassing NSTextField
MultiTextField.h
#import <AppKit/AppKit.h>
#interface MultiTextField : NSTextField {
id storedObject;
}
#property (nonatomic, retain) id storedObject;
#end
MultiTextField.m
#import "MultiTextField.h"
#implementation MultiTextField
#synthesize storedObject;
#end
to store a pointer to an object, which I want to "rename".
I made this textfield editable and have a delegate which listens to controlTextDidChange: and works fine:
- (void)controlTextDidChange:(NSNotification *)aNotification {
NSTextView *textView = [[aNotification userInfo] objectForKey:#"NSFieldEditor"];
NSString *theString = [[textView textStorage] string];
if([theString length] > 0 ) {
MyObject *theObject = ???; // I need access to the MultiTextField.storedObject!
[theObject setName:theString];
}
}
the only problem is that I can't access the storedObject (see comment in the if-block).
So how do I access that storedObject?
Try this:
MyObject *theObject = [[aNotification object] storedObject];

Multiple Views for UITabBarController

I'm trying to build an app where I have a TabBarController with 4 entries.
When I select the first entry, a view with a UITableView shows up.
This TableView is filled with several entries.
What I would like to do is:
When an entry out of that UITableView gets selected, another view should show up; a detailview.
.m
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == 0) {
if(self.secondView == nil) {
SecondViewController *secondViewController = [[SecondViewController alloc] initWithNibName:#"SecondView" bundle:[NSBundle mainBundle]];
self.secondView = secondViewController;
[secondViewController release];
}
// Setup the animation
[self.navigationController pushViewController:self.secondView animated:YES];
}
}
.h
#import <UIKit/UIKit.h>
#import "SecondViewController.h"
#interface FirstViewController : UIViewController {
SecondViewController *secondView;
NSMutableArray *myData;
}
#property (nonatomic, retain) SecondViewController *secondView;
#property (nonatomic, copy, readwrite) NSMutableArray* myData;
#end
This is what I have so far.
Unfortunately.. the code runs, bit the second view does not show up.
Is your first view controller wrapped in a UINavigationController? When you set up your UITabBarController, you should add UINavigationControllers rather than your UIViewController subclasses, e.g.:
FirstViewController *viewControl1 = [[FirstViewController alloc] init];
UINavigationController *navControl1 = [[UINavigationController alloc] initWithRootViewController:viewControl1];
UITabBarController *tabControl = [[UITabBarController alloc] init];
tabControl.viewControllers = [NSArray arrayWithObjects:navControl1, <etc>, nil];
//release everything except tabControl
Also, based on your code, you don't need to keep your secondViewController as an ivar, since UINavigationController automatically retains its view controllers (and retaining it when you're not displaying it will use up unnecessary memory).

Resources