无法将文本分配给 UILabel 或在模式加载视图控制器时传递 NSString 属性



我看了一堆帖子,但其中给出的建议没有奏效:UILabel 不更新(更改加载视图控制器的顺序)UILabel 刷新(线程依赖)等

我所做的只是从另一个视图控制器模态加载一个视图控制器

,并在模态加载之前设置视图控制器的一些属性。这是我创建和加载视图控制器的位置:

- (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 表现得很好(该代码由另一个堆栈溢出发布提供,但我丢失了链接,抱歉),因此在我看到视图之前完成了部分格式化,但日志显示我想用来填充标签文本的 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

我在这里错过了什么?我有一种感觉,我做事的顺序错误,但我看不到在哪里。感谢您的任何建议。

在分配/初始化视图控制器后,尝试像这样访问视图:[RVC 视图];以强制加载视图。否则,我认为您的视图在呈现之前不会加载,并且您将在访问视图中的内容时遇到问题。

有效的方法是从头开始,并使用新的 xib 在新类中再次实现完全相同的代码。我唯一能弄清楚的是 xib 文件一定有问题。但的,如果我知道那会是什么。感谢您的回复。

最新更新