Passing an float value to another ViewController - ios

I have 2 controllers and i would like to send a float from LevelViewController to ViewController. But it always sends it as 0 (zero). Here is my LevelViewController.m
ViewController *inGame = [[ViewController alloc] init];
enemySpeedShouldBe = 0.800f - 0.07f * levelSelected;
inGame.enemySpeed = enemySpeedShouldBe;
Where levelSelected and enemySpeedShouldBe are also floats.
NSLog(#"%f", levelSelected) gives me the correct value (1.0, 2.0 etc.), but when I send it to ViewController, it's equal to 0.
Here is my ViewController.h
#property (nonatomic) float enemySpeed;
I've read about this issue but I couldn't find any way to make it work.
--/--/--/--/--/--/--/--/--/--/--EDIT--/--/--/--/--/--/--/--/--/--
here is my enemy millisecond counter methods in "ViewController.m"
-(void)enemyStartCounter{
enemyMs += 0.001;
if(enemyMs > enemySpeed){ // i use 'enemySpeed' nowhere except here
[enemyTimer invalidate];
[fire setHidden:YES];
[self enemyFired];
}
}
-(void)enemyCounter{
enemyMs = 0;
enemyTimer = [NSTimer scheduledTimerWithTimeInterval:0.001/1.0 target:self selector:#selector(enemyStartCounter) userInfo:nil repeats:YES];
}
and my button touchup inside action method in "LevelViewController.m"
- (void)didTapLevel:(UIButton *)buttonn{
levelSelected = (float)buttonn.tag + 1.0f ;
ViewController *inGame = [[ViewController alloc] init];
enemySpeedShouldBe = 0.800f - (0.07f * levelSelected);
inGame.enemySpeed = enemySpeedShouldBe;
[self performSegueWithIdentifier:#"1player" sender:self];
}

At some point in your code you are creating this instance of ViewController, configuring it with a value for enemySpeed, and then probably throwing it away.
Somewhere else, another instance is being created. This instance isn't configured, but you're calling enemyStartCounter on it, so it logs a zero.
You need to ensure that you understand what instances of each class you have and how they're being used. Your view controller could be created from a Storyboard segue, in this case you need to get a reference to it by intercepting the segue using prepareForSegue:sender:.
This code creates 2 different instances:
- (void)didTapLevel:(UIButton *)buttonn{
ViewController *inGame = [[ViewController alloc] init]; // create first instance
inGame.enemySpeed = enemySpeedShouldBe;
[self performSegueWithIdentifier:#"1player" sender:self]; // create second instance
}
So what you should be doing is:
- (void)didTapLevel:(UIButton *)buttonn{
[self performSegueWithIdentifier:#"1player" sender:self]; // create single instance
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
ViewController *inGame = (ViewController *)[segue destinationViewController]; // get single instance
inGame.enemySpeed = enemySpeedShouldBe;
}

I prefer to create a NSNumber property to do this:
#property (strong, nonatomic) NSNumber *enemySpeed; // in your viewController
// passing the parameter
inGame.enemySpeed = [NSNumber numberWithFloat:(0.800f - 0.07f * levelSelected)];
Then if you pass the property between the controllers you can do this :
NSLog(#"%f", [enemySpeed floatValue]);

Related

Can't transition to Level 2 (using UINavigationController)

In my GameViewController class, I have this method displayWinScreen which gets the current level number and stores it in a public property in my WinViewController class called levelCompleted. It then runs this method called runPushAnimationWithController: which just pushes the WinViewController object onto the UINavigation stack.
- (void)displayWinScreen {
WinViewController *winViewController = [[WinViewController alloc] initWithNibName:#"WinViewController"
bundle:nil];
winViewController.levelCompleted = self.levelNumber;
[self runPushAnimationWithController:winViewController];
}
Then in WinViewController I set a button that, when pressed on the iPhone, calls this method:
-(IBAction)nextLevelSelection:(id)sender {
int num = [self.levelCompleted integerValue];
int newNum = num + 2;
self.levelCompleted = [NSNumber numberWithInteger:newNum];
GameViewController* nextLevelViewController = [[GameViewController alloc]
initWithNibName:#"GameViewController"
bundle:nil];
nextLevelViewController.levelNumber = self.levelCompleted;
[self runPushAnimationWithController:nextLevelViewController];
}
This method just increases that property value, and stores it in the GameViewController property levelNumber. When I put a breakpoint to see if that int number gets passed, everything checks out. If I originally played level 3, the number that eventually is nextLevelViewController.levelNumber is 4. Ignore the num + 2, the way I have it set up, the int value still will the increase the level by 1.
I use this same process of passing int properties for all my other UINavigationController methods and everything is ok. Except for this. Should I popping the controller or use another technique for view controller transitioning?
Here's the pop method I use to go back to the GameViewController from WinViewController.
-(void)runPopperAnimation:(UIViewController*)viewController {
CATransition *transition = [CATransition animation];
transition.duration = 0.30f;
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
transition.type = kCATransitionFade;
[self.navigationController.view.layer addAnimation:transition forKey:nil];
[self.navigationController popToViewController:viewController animated:NO];
}
how would I be able to pass that levelCompleted property from the
WinViewController back up to the GameViewController?
One way is to store it to a separate singleton class:
DataModel.h
#interface DataModel : NSObject
#property NSInteger *levelCompleted;
+ (id)sharedModel;
DataModel.m
#import "DataModel.h"
#implementation DataModel
#synthesize levelCompleted;
/* Return singleton model */
+ (id)sharedModel {
static DataModel *sharedModel = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedModel = [[self alloc] init];
});
return sharedModel;
}
- (id)init {
self = [super init];
if (self){
self.levelCompleted = 0;
return self;
}
You can then call this class from anywhere in your code and modify it's properties:
NSInteger levelCompleted = [DataModel sharedModel].levelCompleted;
To increment the value:
[DataModel sharedModel].levelCompleted+=1;
With this method, the levelCompleted will only persist over the lifecycle of the app execution. If you restart the device or close the app, the value is lost.
BUT
If you want to persist across restarts then you can save it to NSUserDefaults instead. This will persist of the lifetime of the app install:
NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
To Retrieve the value:
NSNumber *levelCompleted =[standardUserDefaults objectForKey:#"levelCompleted"];
To Save the value:
[stadardUserDefaults setObject:levelCompleted forKey:#"levelCompleted"];

Pass informaion back to mother view

I'd like to pass some object from one view (number 2) to view number 1. View 1 triggers view 2 and before that in "prepareForSeque" I'm passing "self" to the second view and store it in variable "delegate". After some time I'd like to pass back new created object to view 1 and I'm trying to achieve it but I got an error that method is not visible for this interface. How to pass created object to the mother view triggering method?
When I declare #property someObject and synthetize it, it works ok using delegate. Is there another option or am I forced to use delegate?
Code:
- (IBAction)saveAndClose:(id)sender {
KwejkModel *mod = [[KwejkModel alloc] init];
((ViewController *)self.delegate).model = mod;
}
It works ok, but is there another option triggering method not using the property? Like this:
[((ViewController *)self.delegate) someMethod];
but here is an error.
Here is my code:
VIEW 1
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"addItemSeque"]) {
ScrollViewViewController *destViewController = segue.destinationViewController;
destViewController.delegate = self;
}
}
-(void) addNewPosition:(KwejkModel *)newKwejk
{
//for testing only this
[modelArray count];
}
VIEW 2:
- (IBAction)saveAndClose:(id)sender {
KwejkModel *mod = [[KwejkModel alloc] init];
// ((ViewController *)self.delegate).model = mod;
//it crashes here with error:-[NSPlaceholderString initWithString:]: nil argument' but it sees method from VIEW 1
[self.delegate addNewPosition:mod];
}
Try this link: Passing Data between View Controllers
Take a look at the first answer under Passing Data Back.

Calling method from another class doesn't work properly

I am trying to change the text and the position of a UILabel from another class.
I have successfully managed to call my -changeLabel method, which is in my FirstViewController class, from my SecondViewController class. Here is the code I have used in my two classes:
SecondViewController:
#import "FirstViewController.h"
#interface SecondViewController ()
#property (nonatomic,strong) FirstViewController *firstViewController;
#end
#implementation SecondViewController
#synthesize firstViewController;
- (IBAction)button:(id)sender {
firstViewController = [[FirstViewController alloc] init];
[firstViewController changeLabel];
}
FirstViewController:
- (void)changeLabel {
NSLog(#"changeLabel is called!");
label.text = #"New text.";
label.frame = CGRectMake(10, 100, 150, 40);
NSLog(#"%#", label.text);
NSLog(#"%f", label.frame.size.width);
}
The weird thing is that the logger looks like this after pressing the "button" that calls the method:
"2013-12-30 19:24:50.303 MyApp[655:70b] changeLabel is called!"
"2013-12-30 19:24:50.305 MyApp[655:70b] New text."
"2013-12-30 19:24:50.308 MyApp[655:70b] 0.000000"
So it seems the label text change, but it doesn't show up on the screen. And the label width is logged as 0.0.. even though I just set it to 150.
Why is this happening? Am I not able to change frame variables from another class? Is there another way to do this?
IMPORTANT:
As the FirstViewController is the main view controller while the SecondViewController is a side menu, similar to the facebook app:
I want to be able to press a "button" on the SecondViewController(side menu) and call a method in the FirstViewController(main) that changes the position(frame) of a UILabel.
EDIT:
Here is how I created the UILabel:
label = [[UILabel alloc] init];
label.frame = CGRectMake(0, 0, 150, 40);
label.text = #"Text."
[self.view addSubview:label];
I think problem is this. You are calling method from new instance of FirstViewController.
Let assume
1. FirstViewController at stack[0].
2. SecondViewController at stack[1].
If you are navigating or moving from
FirstViewController->SecondViewController
In this case FirstViewController already in memory with some address 0x23ffff.
And in SecondViewController you are again creating new instance of FirstViewController which is point to another address '0x234jurhu`
- (IBAction)button:(id)sender {
firstViewController = [[FirstViewController alloc] init];
[firstViewController changeLabel];
}
Don't create new instance here.
You can use delegate or NSNotification concept for this.
How are you displaying FirstViewController?
Here is the issue:
firstViewController = [[FirstViewController alloc] init];
[firstViewController changeLabel];
You creating a new instance of FirstViewController and updating the label text. If your using these VC's in a navigation stack and you pop back to FirstViewController from SecondViewController, you won't see any label change because they are different instances of the class.
If your using FirstViewController as a childViewController of SecondViewController (with naming of them I don't think this what your doing), then in the - (IBAction)button:(id)sender method you don't need to instantiate a new instance of FirstViewController on each button press.
I have figured out a way to do this thanks to "#Gaurav Wadhwani" answer on this question: call method from other class (self issue).
I added this code in my FirstViewController.h:
+ (FirstViewController *)singletonInstance;
And then added this code in my FirstViewController.m
static FirstViewController *_singletonInstance = nil;
+(FirstViewController*)singletonInstance
{
#synchronized([FirstViewController class])
{
if (!_singletonInstance)
_singletonInstance = [[self alloc] init];
return _singletonInstance;
}
return nil;
}
+(id)alloc
{
#synchronized([FirstViewController class])
{
NSAssert(_singletonInstance == nil, #"Attempted to allocate a second instance of a singleton.");
_singletonInstance = [super alloc];
return _singletonInstance;
}
return nil;
}
Then I added this code in my SecondViewController to run the changeLabel method:
[[FirstViewController singletonInstance] changeLabel];
And that seems to work just fine so far. I hope it wont cause any other "problems" in the future, but right now it seems to be perfect.
Try doing this:
[label setNeedsDisplay];
After the last line in changeLabel.

Passing NSString from one class to another. (ECSlidingViewController?)

Firstly, I've already tried to search for solutions online but none works for me and I'm thinking since I'm using ECSlidingViewController to navigate around the app, I can't utilise the prepareForSegue method thus, my problem may need a different approach.
I have a class called viewInits which holds properties in the .h file that I want allow other classes to set and get it's values. In this case, the property is an NSString *navBarTitle.
In ClassA, I have a tableView:didSelectRowAtIndexPath: method, where I
Create an ViewInits class object - *viewInits.
I then set the setNavBarTitle: to the value of [self.MenuRowsArray objectAtIndex:indexPath.row].
In the next line, I did an NSLog to check and yes, viewInits.navBarTitle now holds the value I desire.
In ClassB's viewDidloadMethod, similarly, I created a ViewInits object - *viewInits and did an NSLog check for viewInits.navBarTitle. But it returns (null). What seems to be the problem here?
Here is the code for how I'm trying to pass the NSString. What am I doing wrong?
viewInit .h
#interface ViewInits : NSObject
#property (strong, nonatomic) NSString *navBarTitle;
#end
ClassA.m tableView:didSelectRowAtIndexPath: method:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *identifier = [self.MenuRowsArray objectAtIndex:indexPath.row];
UIViewController *newTopViewController = [self.storyboard instantiateViewControllerWithIdentifier:identifier];
// *---------- Assign identifier to NSString viewInits ----------*
ViewInits *viewInits = [[ViewInits alloc] init];
[viewInits setNavBarTitle:identifier];
NSLog(#"%#", viewInits.navBarTitle);
// *---------- Assign identifier to NSString viewInits ----------*
[self.slidingViewController anchorTopViewOffScreenTo:ECRight animations:nil onComplete:^
{
CGRect frame = self.slidingViewController.topViewController.view.frame;
self.slidingViewController.topViewController = newTopViewController;
self.slidingViewController.topViewController.view.frame = frame;
[self.slidingViewController resetTopView];
}];
}
ClassB.m
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
// *========== ECSlidingViewController ==========*
self.view.layer.shadowOpacity = 0.75f;
self.view.layer.shadowRadius = 10.0f;
self.view.layer.shadowColor = [UIColor blackColor].CGColor;
if (![self.slidingViewController.underLeftViewController isKindOfClass:[MenuViewController class]])
{
self.slidingViewController.underLeftViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"Menu"];
}
[self.view addGestureRecognizer:self.slidingViewController.panGesture];
// *========== ECSlidingViewController ==========*
ViewInits *viewInits = [[ViewInits alloc] init]; // Create ViewInit class object
self.navBar.topItem.title = viewInits.navBarTitle;
NSLog(#"%#", viewInits.navBarTitle); // <<--- This always ends up null. What's wrong?
}
Your help are much appreciated. Thank you.
If you want to use ViewInit as a common store of settings it should be a singleton so that all other instances in the app can get it. Currently you're creating a new instance each time you want to use it, so the new instance doesn't have any of your previous settings.
Aside, I know what the sliding view controller is, I ask about it because you may be using it incorrectly. If you have a view controller which is the current top view controller and it changes the top view controller (class A might be doing this, not sure) then the reference self.slidingViewController will stop working part way through your code.

Property getting set then reset in init

Here's the property declaration in my SlidingVC header, a subclass of UITableViewController:
#property (strong, nonatomic) NSString *user;
Here's my synthesize line:
#synthesize user = _user,sortedWordlist = _sortedWordlist, wordlist = _wordlist;
Here's my custom init:
- (id)initWithUser:(NSString*)theUser
{
self = [super init];
if (self) {
if ([theUser isEqualToString:#"user"]) {
_user = #"user";
}
else if ([theUser isEqualToString:#"opponent"]){
_user = #"opponent";
}
}
return self;
}
So what's happening is that as I step pver initWithUser:, I see that _user takes on the memory address of theUser in the variable debugger window. I step over return self and then to the closing } of the method and it's still set. However, Xcode returns to return self one more time and then if I step over that _user doesn't have a memory address next to it anymore, and it remains null for the methods that follow.
Why is it returning twice and then setting to null the second time?
Here's the method in my MainVC that instantiates the SlidingVC:
- (WSWordlistTableViewController *)setupWordlistTableViewController:(NSString*)user
{
WSWordlistTableViewController *wordlistTableViewController;
UIView *wordlistContainerView;
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"MainStoryboard"
bundle: nil];
if ([user isEqualToString:#"user"]){
if(!self.userWordlistTableViewController){
self.userWordlistTableViewController = [[WSWordlistTableViewController alloc] initWithUser:#"user"];
wordlistTableViewController = self.userWordlistTableViewController;
wordlistContainerView = self.userWordlistContainerView;
}
}
else if ([user isEqualToString:#"opponent"]) {
if(!self.opponentWordlistTableViewController){
self.opponentWordlistTableViewController = [[WSWordlistTableViewController alloc] initWithUser:#"opponent"];
wordlistTableViewController = self.opponentWordlistTableViewController;
wordlistContainerView = self.opponentWordlistContainerView;
}
}
wordlistTableViewController = [mainStoryboard instantiateViewControllerWithIdentifier:#"wordlistTableViewController"];
wordlistTableViewController.view.frame = wordlistContainerView.bounds;
wordlistTableViewController.view.autoresizingMask = wordlistContainerView.autoresizingMask;
[self addChildViewController:wordlistTableViewController];
[wordlistContainerView addSubview:wordlistTableViewController.view];
[wordlistContainerView bringSubviewToFront:wordlistTableViewController.wordlistTableView];
[wordlistTableViewController didMoveToParentViewController:self];
return wordlistTableViewController;
}
And the method that calls that, depending on which button is pressed:
- (IBAction)slideUserWordlistView:(id)sender {
if(!self.userWordlistTableViewController){
[self setupWordlistTableViewController:#"user"];
}
// (sliding drawer code here)
}
Or:
- (IBAction)slideOpponentWordlistView:(id)sender {
if(!self.opponentWordlistTableViewController){
[self setupWordlistTableViewController:#"opponent"];
}
// (sliding drawer code here)
}
What I'm doing is sliding out a view that contains my SlidingVC. I have two of them, one for each of two users. When each respective button is pressed I check if each respective SlidingVC exists, if not, instantiate then add to the the slidingViewContainer.

Resources