以模态加载视图控制器时,无法将文本分配给 UILabel 或传递 NSString 属性 [英] Can't assign text to UILabel or pass NSString property when modally loading a view controller
问题描述
我看了一堆帖子,但这些帖子中给出的建议没有奏效:UILabel 未更新(更改加载视图控制器的顺序)UILabel 刷新(线程依赖)等
I've looked at a bunch of posts, but the advice given in these has not worked: UILabel not updating (changing order of loading view controller) UILabel Refresh (threading dependence) etc
我所做的只是从另一个视图控制器模态加载一个视图控制器,然后在模态加载设置视图控制器的某些属性之前.这是我创建和加载视图控制器的地方:
All I am doing is modally loading one view controller from another and before the modal load setting some properties of the view controller. Here's where I create and load the view controller:
- (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 属性声明 + 我尝试更新标签的函数:
.m properties declared + function where I try to update labels:
@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 属性为空或标签数据的一些奇怪的合并(请参阅 *** 加星标的错误消息):
The UITextView comes out nicely (that code is courtesy of another stackoverflow posting, but I've lost the link, sorry) so that part of the formatting is done before I see the view, but the logs show that the values for the NSString properties I would like to use to fill the text of the labels are null or some weird amalgamation of label data (see *** starred error messages):
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
在这个排列中,我注释掉了以下内容,否则应用程序会崩溃:
In this permutation I have the following commented out because otherwise the app crashes:
// 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
我在这里错过了什么?我有一种感觉,我正在以错误的顺序做事,但我不知道在哪里.感谢您的任何建议.
What am I missing here? I have a feeling I'm doing things in the wrong order, but I don't see where. Thanks for any suggestions.
推荐答案
有效的方法是从头开始,并在具有新 xib 的新类中再次实现完全相同的代码.我唯一能想到的是,xib 文件肯定有问题.但如果我知道那会是什么,那就该死了.感谢您的回复.
What worked was starting over from scratch and implementing the exact same code again in a new class with a new xib. The only thing I can figure is there must have been something wrong with the xib file. but darned if I know what that would have been. Thanks for the replies.
这篇关于以模态加载视图控制器时,无法将文本分配给 UILabel 或传递 NSString 属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!