我看了很多帖子,但是这些帖子中的建议没有奏效: UILabel not updating(更改加载视图控制器的顺序) UILabel Refresh(线程依赖) 等
我正在做的是从另一个视频控制器模态加载一个视图控制器,并在模态加载之前设置视图控制器的一些属性。这是我创建和加载视图控制器的地方:
- (void)swipeUp:(UISwipeGestureRecognizer *) gr
{
RespondViewController *rvc = [[RespondViewController alloc] init];
rvc.user = self.user;
rvc.question = self.question.text;
rvc.userQ = self.questionUser.text;
[self.navigationController presentViewController:rvc animated:YES completion:NULL];
NSLog(@"question & user is %@ and %@", self.question.text, self.questionUser.text);
}
.h文件:
@interface RespondViewController : UIViewController
@property (nonatomic) NSString *question;
@property (nonatomic) NSString *userQ;
@property (nonatomic) int qid;
@property (nonatomic) UserObject *user;
@end
.m属性声明+函数,我尝试更新标签:
@interface RespondViewController () <UIGestureRecognizerDelegate, UITextViewDelegate>
@property (weak, nonatomic) IBOutlet UILabel *userLabel;
@property (weak, nonatomic) IBOutlet UILabel *qLabel;
@property (weak, nonatomic) IBOutlet UITextView *respondText;
@property (nonatomic, strong) NSURLSession *session;
@end
- (void)viewDidLoad {
[super viewDidLoad];
_respondText.layer.cornerRadius = 5;
[_respondText.layer setBorderColor:[[[UIColor grayColor] colorWithAlphaComponent:0.5] CGColor]];
[_respondText.layer setBorderWidth:2.0];
_respondText.clipsToBounds = YES;
// self.userLabel.text = self.userQ;
// self.qLabel.text = self.question;
NSLog(@"self.userQ is %@", self.userQ);
NSLog(@"self.question is %@", self.question);
NSLog(@"self.userQ is %@", self.userQ);
UITapGestureRecognizer* tapBackground = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissKeyboard:)];
[tapBackground setNumberOfTapsRequired:1];
[self.view addGestureRecognizer:tapBackground];
NSLog(@"finished view did load");
[self.view setNeedsDisplay];
}
UITextView很好地出现(该代码是另一个stackoverflow发布的礼貌,但是我已经丢失了链接,抱歉)所以在我看到视图之前完成了部分格式化,但是日志显示了我想用来填充标签文本的NSString属性的值为null或标签数据的一些奇怪的合并(参见***已加星标的错误消息):
2014-11-14 17:19:19.021 Ratings[1136:227062] ran init
***2014-11-14 17:19:19.133 Ratings[1136:227062] self.userQ is <UILabel: 0x175f0f10; frame = (102 78; 81 21); text = 'user name'; opaque = NO; autoresize = RM+BM; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x175f0fd0>>
***2014-11-14 17:19:19.134 Ratings[1136:227062] self.question is (null)
***2014-11-14 17:19:19.134 Ratings[1136:227062] self.userQ is <UILabel: 0x175f0f10; frame = (102 78; 81 21); text = 'user name'; opaque = NO; autoresize = RM+BM; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x175f0fd0>>
2014-11-14 17:19:19.135 Ratings[1136:227062] finished view did load
2014-11-14 17:19:19.136 Ratings[1136:227062] question & user is Heya hey and ivan
在这个排列中,我有以下注释,因为否则应用程序崩溃:
// self.userLabel.text = self.userQ;
// self.qLabel.text = self.question;
我收到以下错误消息:
2014-11-14 17:30:14.034 Ratings[1150:229094] -[UILabel copyWithZone:]: unrecognized selector sent to instance 0x146f67a0
我在这里缺少什么?我有一种感觉,我在错误的顺序做事,但我不知道在哪里。感谢您的任何建议。
答案 0 :(得分:0)
在视图控制器的alloc / init之后,尝试访问这样的视图: [rvc view]; 强制视图加载。 否则,我认为您的视图在呈现之前不会加载,并且您将无法访问视图中的内容。
答案 1 :(得分:0)
从头开始并在新类中使用新的xib再次实现完全相同的代码是有效的。我唯一能想到的是xib文件肯定有问题。但是,如果我知道那将是什么,我会感到害怕。谢谢你的回复。