【发布时间】:2011-05-06 04:27:39
【问题描述】:
我在看似非常简单的事情上遇到了很多麻烦。我无法从基于导航的 iOS 应用程序以编程方式更新 UILabel。我不想使用按钮,因为此标签旨在报告外部系统的状态,并且应该在启动时更新。如果我不需要,则无需让用户完成触摸按钮的额外步骤。
以下是我已采取的步骤的详尽列表。如果其中一些看起来不必要,我很抱歉,但根据我的经验,即使是最小的被遗忘的步骤也可能是问题的原因。
从 Xcode 中一个新的基于导航的应用程序中,我正在采取以下步骤:
- 将 UITableView 替换为通用 UIView 类
- 将文件所有者的视图出口重新连接到新的 UIView
- 在 UIView 的中心添加一个 UILabel,使文本居中,并保留默认文本。
- 保存并退出界面生成器
- RootViewController.h
<pre>#import<UIKit>@interface RootViewController : UIViewController { UILabel *myLabel; } @property (nonatomic, retain) IBOutlet UILabel *myLabel; @end - RootViewController.m
#import "RootViewController.h" @implementation RootViewController @synthesize myLabel; ...
- 从 RootViewController.m 中删除了 TableView 内容
- 将 IBOutlet myLabel 连接到 RootViewController.xib 中的标签
- 保存并退出界面生成器
- tempNavAppAppDelegate.m
...
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch.
// Add the navigation controller's view to the window and display. [self.window addSubview:navigationController.view]; [self.window makeKeyAndVisible];
RootViewController *rootViewCont = navigationController.visibleViewController; rootViewCont.myLabel.text = @"test"; NSLog(@"Label Text: %@", rootViewCont.myLabel.text);
return YES; } ... - 构建/运行
标签显示为“标签”而不是“测试”。并且日志报告:tempNavApp[94186:207] Label Text: (null)
我已经尝试了许多不同的方法来完成这项工作,但我们将不胜感激。
【问题讨论】:
标签: objective-c cocoa-touch ios