【发布时间】:2014-01-17 16:25:02
【问题描述】:
我正在尝试从另一个类更改 UILabel 的文本和位置。
我已经成功地从我的 SecondViewController 类中调用了我的 FirstViewController 类中的 -changeLabel 方法。这是我在两个课程中使用的代码:
SecondViewController:
#import "FirstViewController.h"
@interface SecondViewController ()
@property (nonatomic,strong) FirstViewController *firstViewController;
@end
@implementation SecondViewController
@synthesize firstViewController;
- (IBAction)button:(id)sender {
firstViewController = [[FirstViewController alloc] init];
[firstViewController changeLabel];
}
FirstViewController:
- (void)changeLabel {
NSLog(@"changeLabel is called!");
label.text = @"New text.";
label.frame = CGRectMake(10, 100, 150, 40);
NSLog(@"%@", label.text);
NSLog(@"%f", label.frame.size.width);
}
奇怪的是,按下调用方法的“按钮”后记录器看起来是这样的:
"2013-12-30 19:24:50.303 MyApp[655:70b] changeLabel is called!"
"2013-12-30 19:24:50.305 MyApp[655:70b] New text."
"2013-12-30 19:24:50.308 MyApp[655:70b] 0.000000"
所以标签文本似乎发生了变化,但它没有出现在屏幕上。并且标签宽度记录为 0.0.. 即使我只是将其设置为 150。
为什么会这样?我不能从另一个类更改框架变量吗?有没有其他方法可以做到这一点?
重要提示:
FirstViewController 是主视图控制器,而 SecondViewController 是侧菜单,类似于 facebook 应用程序:
我希望能够按下SecondViewController(侧边菜单)上的“按钮”并调用FirstViewController(主)中的一个方法来改变UILabel的位置(框架)。
编辑:
这是我创建 UILabel 的方式:
label = [[UILabel alloc] init];
label.frame = CGRectMake(0, 0, 150, 40);
label.text = @"Text."
[self.view addSubview:label];
【问题讨论】:
-
你在使用自动布局吗?
-
如何创建
label? -
很可能
FirstViewController的视图没有加载。 -
我所指的标签是以编程方式创建的,所以不,我没有使用自动布局。
-
@Peter:你用什么方法创建标签?
标签: ios iphone class methods uiviewcontroller