Can't assign text to UILabel or pass NSString

2019-08-19 20:52发布

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 file:

@interface RespondViewController : UIViewController
@property (nonatomic) NSString *question;
@property (nonatomic) NSString *userQ;
@property (nonatomic) int qid;
@property (nonatomic) UserObject *user;
@end

.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];

}

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;

I get the following error message:

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.

2条回答
虎瘦雄心在
2楼-- · 2019-08-19 21:00

After alloc/init of the view controller, try accessing the view like this: [rvc view]; to force the view to load. Otherwise I think your view is not loaded until you present it, and you will have problems accessing things in the view.

查看更多
太酷不给撩
3楼-- · 2019-08-19 21:09

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.

查看更多
登录 后发表回答