将UILabel更改为appDelegate中声明的值?

时间:2011-05-01 12:36:34

标签: iphone objective-c cocoa-touch

我正在尝试从一个UIViewController向appDelegate发送一个值,以便另一个UIViewController可以使用该值并将UILabel.text设置为该值。

这是我到目前为止所用的, UIViewController设置标签文本只是将文本设置为“0.00000”。此外,下面的文件不是全部​​,只是相关的代码。

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

}

1 个答案:

答案 0 :(得分:1)

这就是为什么如果没有必要,你不应该使用idvoid*

您可以在此行中的recordedNewTime中保存NSString:

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

以后你将%f与NSString一起使用,这将无效。

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

尝试使用newResult.text = mainDelegate.recordedNewTime;

请将id recordedNewTime更改为NSString *recordedTimeString;或类似内容。

btw id *foo是指向对象指针的指针。通常是id foo;