我想在应用程序进入前台时更改视图控制器中的标签....:
SalaryAppV4AppDelegate.h
@interface SalaryAppV4AppDelegate : NSObject <UIApplicationDelegate, UITabBarControllerDelegate> {
//----------------------------------------------
NSTimeInterval appEnteredBackground;
NSTimeInterval appEnteredForeground;
NSTimeInterval difference;
NSString *times;
//-----------------------------------------------
SalaryAppV4AppDelegate.m
- (void)applicationWillEnterForeground:(UIApplication *)application
{
//perform -(IBAction)DoThis
FirstViewController* controller = [FirstViewController alloc];
[controller DoThis];
//releasing
[dateFormatter release];
}
FirstViewController.m
-(IBAction) DoThis
{
appDelegate = [[[UIApplication sharedApplication] delegate] retain];
//This Doesn't Work :(
label.text = @"IT Has Worked";
//This Works
NSLog(@"%@", appDelegate.times);
}
//--------------------------------------------------------
我只想将label.text ti更改为任何内容,但在viewcontroller更改中没有任何内容......
答案 0 :(得分:0)
Interface Builder中是否挂了标签?
答案 1 :(得分:0)
首先确保标签不是零。第二,如果你的viewcontroller在导航堆栈中,那么你需要从导航堆栈中获取你的视图控制器。
这是从导航堆栈获取viewController的代码片段
HomeViewController *vController = nil;
NSArray *vControllers = [self.navigationController viewControllers];
for(UIViewController *v in vControllers) {
if([v isKindOfClass:[HomeViewController class]]) {
vController = (HomeViewController*)v;
break;
}
}
答案 2 :(得分:0)
RootViewController * controller = [[RootViewController alloc]initWithNibName:@"RootViewController" bundle:nil];
- (IBAction)DoThis {
appDelegate = [[[UIApplication sharedApplication] delegate] retain];
将此更改为与setText相似:
[label setText:@"IPhone/Ipad Chat Room"];
//This Works
NSLog(@"%@", appDelegate.times);
}
答案 3 :(得分:0)
在applicationWillEnterForeground:
方法中,您正在创建FirstViewController的新实例,然后在其上调用方法DoThis:
,这会更改标签文本。但是,这个控制器不是一个成员对象,你看到它不会更新的版本可能在某个地方的另一个代码文件中 - 或者它也在你的app代理中?
简而言之,虽然您确实更改了文本但它不是正确的对象,这就是为什么您没有看到任何明显的变化,我估计。
答案 4 :(得分:0)
使用App委托中的方法和对View Controller的实例调用
- (void)applicationWillEnterForeground:(UIApplication *)application {
[ViewController.label setText:@"DA TEXT"]; //if the VC is a property.
[[ViewController instance].label setText:@"DA TEXT"]; //if is not a property
}
如果不是属性,则必须在VC上设置实例类方法