How to import an UILabel from one UIViewcontroller to another - xcode

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];
}

Related

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

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.

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.

Range or index out of bounds (App is crashing)

I have an app with ads banner (inner-active) in every view controller (4 views) and i keep getting this error after 2-3-4 minutes that my app is running:
'NSRangeException', reason: '*** -[__NSCFString substringToIndex:]: Range or index out of bounds'
My app is crashing & i can't find the solution for that, but i know for sure that is something with my AD banner issue cau's when i comment the ad banner code everything works great. i can assume that it happens when new request is loading on the banner.
This is the code i'm using for the AD banner:
.h file:
#property (nonatomic, retain) UIView *adBanner;
.m file:
Did synthesize & import for what i need and after that:
- (void)viewWillAppear:(BOOL)animated
{
CGRect frame = CGRectMake(0, 430, 320, 50);
self.adBanner = [[UIView alloc] initWithFrame:frame];
[self.view addSubview:self.adBanner];
// Display ad
if (![InneractiveAd DisplayAd:#"iOS_Test" withType:IaAdType_Banner withRoot:adBanner withReload:60 withParams:optionalParams])
{
adBanner.hidden = YES;
}
}
This is my AppDelegate (don't know why but maybe it something with that too?):
.h file:
#import <UIKit/UIKit.h>
#class RootViewController;
#interface AppDelegate : UIResponder <UIApplicationDelegate>
#property (strong, nonatomic) UIWindow *window;
#property (nonatomic, strong) RootViewController *rootViewController;
#end
.m file:
#import "AppDelegate.h"
#import "RootViewController.h"
#implementation AppDelegate
#synthesize window = _window;
#synthesize rootViewController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.rootViewController = [[RootViewController alloc] initWithNibName:nil bundle:NULL];
[self.window addSubview:self.rootViewController.view];
[self.window makeKeyAndVisible];
return YES;
}
I realy don't understand what it could be and how it related to NSCFString :\
Thanks.
Not sure if you got an answer to this but I was getting it with this line of code when there was no internet connection.
NSString *value = [serverOutput substringWithRange:NSMakeRange(2, [serverOutput length] - 2 * 2)];
I had to protect it with a check to the internet connection and an if then statement

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;
}

Why won't this simple view switching project work?

I'm in the process of learning how to write apps in Xcode. I'm using a book to guide me. Unfortunately the book is written with guides to Xcode 3 and I'm using Xcode 4.
Now so far there haven't been any problems, but this project doesn't work, and I simply don't get it, because it seems to make pretty good sense.
The project's goal is to use a view controller to switch between three views.
Could anyone please take a look and see what I did wrong?
Here is the entire project: http://www.2shared.com/file/CKO6ACzg/MultipleViews.html
PS: I know that as it is now the views will be stacked on top off each other and that the view isn't being cleared when you click a new button.
MultipleViewsViewController.h should be:
#import <UIKit/UIKit.h>
#class FirstViewController;
#class SecondViewController;
#class ThirdViewController;
#interface MultipleViewsViewController : UIViewController {
IBOutlet FirstViewController *firstViewController;
IBOutlet SecondViewController *secondViewController;
IBOutlet ThirdViewController *thirdViewController;
}
//#property (nonatomic, retain) FirstViewController *firstViewController;
//#property (nonatomic, retain) SecondViewController *secondViewController;
//#property (nonatomic, retain) ThirdViewController *thirdViewController;
-(IBAction)loadFirstView:(id)sender;
-(IBAction)loadSecondView:(id)sender;
-(IBAction)loadThirdView:(id)sender;
#end
MultipleViewsViewController.m should be:
#import "MultipleViewsViewController.h"
#import "FirstViewController.h"
#import "SecondViewController.h"
#import "ThirdViewController.h"
#implementation MultipleViewsViewController
//#synthesize firstViewController;
//#synthesize secondViewController;
//#synthesize thirdViewController;
-(IBAction)loadFirstView:(id)sender{
[secondViewController.view removeFromSuperview];
[thirdViewController.view removeFromSuperview];
[self.view insertSubview:firstViewController.view atIndex:0];
}
-(IBAction)loadSecondView:(id)sender{
[firstViewController.view removeFromSuperview];
[thirdViewController.view removeFromSuperview];
[self.view insertSubview:secondViewController.view atIndex:0];
}
-(IBAction)loadThirdView:(id)sender{
[firstViewController.view removeFromSuperview];
[secondViewController.view removeFromSuperview];
[self.view insertSubview:thirdViewController.view atIndex:0];
}
-(void)dealloc{
[firstViewController release];
[secondViewController release];
[thirdViewController release];
[super dealloc];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
firstViewController = [[FirstViewController alloc] init];
secondViewController = [[SecondViewController alloc] init];
thirdViewController = [[ThirdViewController alloc] init];
[self loadFirstView:nil];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
Connect your buttons (which you haven't done in your project, which also might be the issue) and you're done.

Resources