2011-08-18 116 views
1

我一直在努力几天得到此代码的工作,但我不知道我在做什么错了。每当应用程序从睡眠中醒来,或者用户关闭应用程序并再次打开应用程序(无需关闭多任务应用程序),我想要更改标签值。 在我的applicationDidBecomeActive中,我正在运行一个计数器,我想在任何视图控制器当前打开时显示该计数器。需要帮助与applicationDidBecomeActive

代码:

- (void)applicationDidBecomeActive:(UIApplication *)application { 
counter = counter + 1; 

W1G1 *view1 = [[[W1G1 alloc] initWithNibName:@"W1G1" bundle:nil] retain]; 
[view1 setlabel]; 

}

在我的ViewController W1G1,我有以下代码: 代码:

- (void) setlabel { 
NSString *string = [NSString stringWithFormat:@"%d", counter]; 
vocabword.text = string; 

}

我在进口W1G1我appdelegate,但代码不运行:(请帮助!

谢谢

+0

什么是 “不跑” 呢?你有错误吗? – jtbandes

+0

标签不变。我通过调试器运行它,并且代码块正在工作,计数器和字符串根据需要进行更新,但标签不会更改。但是,当我从我的viewController检查计数器的值时,它显示更新的值。但标签根本没有更新。 – Prajoth

+0

如果W1G1是UIViewController子类或UIView子类。如果前者,变量名称view1是一个不寻常的选择。您发布的内容不清楚您的标签如何添加到视图层次结构中。根本没有在视图层次结构中更新的标签? – Obliquely

回答

2

1)当你说'代码不运行'你的意思是?也就是说,如果你把NSLogs放在applicationDidBecomeActive中:并且在setLabel中它是否显示代码正在运行?

2)我会怀疑代码正在运行。但是你的代码不会“在那个时候在任何视图控制器打开时显示计数器”。您的代码会创建一个新视图(视图1),但该视图不会显示。它不作为子视图添加到任何东西。你的代码也会泄漏。您创建了一个W1G1对象,但它永远不会被释放,并且会丢弃对它的任何引用。

为了实现你想要的,你可以在应用程序的窗口中添加一个子视图。根据您的应用程序代理是如何设置的,像下面应该做的伎俩:

counter++; 
    W1G1 *viewController1 = [[W1G1 alloc] initWithNibName:@"W1G1" bundle:nil]; 
    [viewController1 setlabel: counter]; 
    [[self window] addSubview: [viewController1 view]] 
    // you'll want to save a reference to the viewController somehow so you can release it at a later date 
在W1G1

然后

- (void) setlabel: (int) counter; 
{ 
    NSString *string = [NSString stringWithFormat:@"%d", counter]; 
    vocabword.text = string; 
} 

有,当然,很多其他的方法,你可以迈出这问题。而且您需要一些策略来移除您在某个阶段添加的W1G1视图,否则您只会添加更多视图。

更新:你问(在评论中)如何跟踪你的viewController整个应用程序的生命周期...一种方法是跟踪它在你的appDelegate中。在头部碰到这样的:

@class W1G1; 
@interface MyAppDelegate : : NSObject <UIApplicationDelegate> 

{ 
    // other decelerations 
    int counter; 
    W1G1 * _myW1G1 
} 

@property (nonatomic, retain) W1G1* theW1G1 

在.m文件包括

@synthesize theW1G1 = _myW1G1; 

大概在应用中:didFinishLaunchingWithOptions:创建的viewController,设置属性引用它,并添加其视图视图层次结构。

W1G1* theViewController = [[W1G! alloc] initWithNibName: @"W1G1" bundle: nil]; 
[[self window] addSubview: [theViewController view]]; 
[self setTheW1G1: theViewController]; 
[theViewController release]; 

然后,当你想从应用程序的委托使用[自theW1G1],例如再次访问的viewController

[[self W1G1] setlabel: counter]; 
+0

请参阅PengOne的回答。你可以传递setLabel(可能是最好的)或者让W1G1向应用程序代理请求计数器值。我已经修改了我的答案。 – Obliquely

+0

谢谢!当我说它不运行时,标签不会改变。我通过调试器运行它,并且代码块正在工作,计数器和字符串根据需要进行更新,但标签不会更改。但是,当我从我的viewController检查计数器的值时,它显示更新的值。但标签根本没有更新。 我尝试了你提到的子视图方法,并且在从不同的Objective-C类型传递'addSubview:'的参数1时,抛出了一个警告,指出“不兼容的Objective-C类型'结构体W1G1 *',期望'结构体UIView *' ”。 – Prajoth

+0

确定W1G1是一个viewController。你选择的变量名称表明这是一个观点。修正上面的答案。但是你需要考虑你是否真的想每次都创建一个新的viewController(和视图)(并且在某个阶段之后清理),或者如果你想在应用程序的整个生命周期内跟踪viewController。 – Obliquely

3

在AppDelegate中。M档,那就是你有

- (void)applicationDidBecomeActive:(UIApplication *)application { 
    counter = counter + 1; 

    W1G1 *view1 = [[[W1G1 alloc] initWithNibName:@"W1G1" bundle:nil] retain]; 
    [view1 setlabel]; 
} 

变量counter递增仅限于AppDelegate。换句话说,你的视图控制器不知道它已经增加了。

我建议您使用NSUserDefaults来存储counter的值,以便您可以轻松地在这些视图控制器之间传递它。或者,您可以允许输入方法setLabel,例如,

- (void) setlabel:(int)counter { 
    NSString *string = [NSString stringWithFormat:@"%d", counter]; 
    vocabword.text = string; 
} 

然后在AppDelegate中,你会想做的事:

- (void)applicationDidBecomeActive:(UIApplication *)application { 
    counter = counter + 1; 

    W1G1 *view1 = [[[W1G1 alloc] initWithNibName:@"W1G1" bundle:nil] retain]; 
    [view1 setlabel:counter]; // <-- now you're using counter 

    [self.window addSubview:view1]; 
}