buzzkip
buzzkip

Reputation: 613

Change UILabel to the value declared in an appDelegate?

I'm trying to send a value to the appDelegate from one UIViewController, so that another UIViewController can use the value and set a UILabel.text to that value.

Here's what I have so far,the UIViewController setting the label text just sets the text to "0.00000". Also, the files below aren't the whole thing, just the relevant code.

appDelegate.h

@interface AppDelegate : NSObject <UIApplicationDelegate> {
id *recordedNewTime;
}
@property (nonatomic) id *recordedNewTime;
@end

appDelegate.m

@implementation AppDelegate
@synthesize recordedNewTime;
@end

HomeView.m

#import "Reflex_Test_ProAppDelegate.h"
- (void)viewDidLoad
{        
    [super viewDidLoad];
    startTimer = [[NSDate alloc]init];
}

-(IBAction)stopTime
{
    // STOP TIME AND ASSIGN TIME LAPSE RESULT TO newTime
    stopTimer = [[NSDate alloc]init];
    void *newTime;
    newTime = [NSString stringWithFormat:@"Time : %f", [stopTimer timeIntervalSinceDate:startTimer]];

    // SEND newTime DATA TO APP DELEGATE FOR OTHER VIEWS TO USE
    AppDelegate *mainDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    mainDelegate.recordedNewTime = newTime;

    // SHOW NEXT VIEW
    PostSecondView *postsecondView = [[PostSecondView alloc] initWithNibName:nil bundle:nil];
    [self presentModalViewController:postSecondView animated:NO];

}

SecondView.m

#import "Reflex_Test_ProAppDelegate.h"
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.

    // ACCESS APP DELEGATE
    AppDelegate *mainDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];

    // CHANGE UILABEL TEXT ON THIS VIEW TO THE RESULT SAVED ON recordedNewTime
    newResult.text = [NSString stringWithFormat:@"Time : %f", mainDelegate.recordedNewTime]; 

}

Upvotes: 0

Views: 652

Answers (1)

Matthias Bauch
Matthias Bauch

Reputation: 90117

That's why you shouldn't use id and void* if not necessary.

You save a NSString in recordedNewTime in this line:

newTime = [NSString stringWithFormat:@"Time : %f", [stopTimer timeIntervalSinceDate:startTimer]];
mainDelegate.recordedNewTime = newTime;

and later you use %f with that NSString, that won't work.

newResult.text = [NSString stringWithFormat:@"Time : %f", mainDelegate.recordedNewTime];

Try to use newResult.text = mainDelegate.recordedNewTime;

And please change id recordedNewTime to NSString *recordedTimeString; or something similar.

And btw id *foo is a pointer to a pointer to an object. Usually it is id foo;

Upvotes: 1

Related Questions