简体   繁体   中英

Can't set the text on a UILabel controlled by a custom UIViewController

I have a mapkit application, where I've created a custom callout view. I can't seem to get the labels on the view to update though. Here's my setup:

I have a custom annotation class called VendorAnnotation with 5 string properties (phone and 4 address lines). The VendorAnnotation objects have their rightCalloutAccessoryView set to a UIButton. In the delegate method: `(void)mapView:(MKMapVIew *)mapView annotationView:(MKAnnotationView *)view calloutAccessroyControlTapped:(UIControl *)control{} I access the 5 strings via the annotations properties:
VendorAnnotation v = (VendorAnnotation*) view.annotation; Then I have: DetailViewController *detailVC = [[DetailViewController alloc] initWithDetails:v.phone add1:v.add1 add2:v.add2 add3:v.add3 add4:v.add4]; I know by stepping through, that at this point these string properties (v.*) have accurate values. The problem is, this initWithDetails method doesn't set the label texts. Here is my code for the DetailViewController class:

DetailViewController.h:

@interface DetailViewController : UIViewController
{
  IBOutlet UILabel *_phone;
  IBOutlet UILabel *_add1;
  IBOutlet UILabel *_add2;
  IBOutlet UILabel *_add3;
  IBOutlet UILabel *_add4;
}


- (id)initWithDetails:(NSString *)p add1:(NSString *)a add2:(NSString *)aa add3:(NSString *)aaa add4:(NSString *)aaaa;
@end

DetailViewController.m:

#import "DetailViewController.h"

@implementation DetailViewController


- (id)initWithDetails:(NSString *)p add1:(NSString *)a add2:(NSString *)aa add3:(NSString *)aaa add4:(NSString *)aaaa

{
  self = [super init];
  if (self) {
    [_phone setText:p];
    [_add1 setText:a];
    [_add2 setText:aa];
    [_add3 setText:aaa];
    [_add4 setText:aaaa];
  }
  return self;
}

I don't know why, but the labels' text property never gets set to the appropriate strings. Please help. I know it's something stupid, I'm new to objective C and iOS programming, but I don't see the problem. I should probably mention that I have connected the labels on the .xib in IB to the appropriate IBOutlet properties.

maybe because "p", "a", "aa"... aren't initiated..

Try it on - (void)viewDidLoad instead init.

Or instead of using:

detailVC = [[DetailViewController alloc] initWithDetails:v.phone add1:v.add1 add2:v.add2 add3:v.add3 add4:v.add4]; 

why don't you use the custom init method and then do something like this:

detailVC.YOUR_LABEL.text = @"Your TEXT";

or

detailVC.YOUR_LABEL.text = v.add1;

Your outlet i-vars are nil's inside your custom initializer. That's why values are not set.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM