简体   繁体   English

无法在由自定义UIViewController控制的UILabel上设置文本

[英]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. 我有一个mapkit应用程序,在其中创建了一个自定义标注视图。 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). 我有一个名为VendorAnnotation的自定义注释类,具有5个字符串属性(电话和4条地址线)。 The VendorAnnotation objects have their rightCalloutAccessoryView set to a UIButton. VendorAnnotation对象的rightCalloutAccessoryView设置为UIButton。 In the delegate method: `(void)mapView:(MKMapVIew *)mapView annotationView:(MKAnnotationView *)view calloutAccessroyControlTapped:(UIControl *)control{} I access the 5 strings via the annotations properties: 在委托方法中: `(void)mapView:(MKMapVIew *)mapView annotationView:(MKAnnotationView *)view calloutAccessroyControlTapped:(UIControl *)control{}我通过注解属性访问5个字符串:
VendorAnnotation v = (VendorAnnotation*) view.annotation; 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]; 然后我有: 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. 通过逐步了解,目前这些字符串属性(v。*)具有准确的值。 The problem is, this initWithDetails method doesn't set the label texts. 问题是,此initWithDetails方法未设置标签文本。 Here is my code for the DetailViewController class: 这是我的DetailViewController类的代码:

DetailViewController.h: 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: 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. 我不知道为什么,但是标签的text属性永远不会设置为适当的字符串。 Please help. 请帮忙。 I know it's something stupid, I'm new to objective C and iOS programming, but I don't see the problem. 我知道这很愚蠢,我是不熟悉目标C和iOS编程的,但是我没有看到问题。 I should probably mention that I have connected the labels on the .xib in IB to the appropriate IBOutlet properties. 我可能应该提到我已将IB中.xib上的标签连接到适当的IBOutlet属性。

maybe because "p", "a", "aa"... aren't initiated.. 可能是因为未启动“ p”,“ a”,“ aa”...。

Try it on - (void)viewDidLoad instead init. 尝试- (void)viewDidLoad代替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: 为什么不使用自定义init方法,然后执行以下操作:

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

or 要么

detailVC.YOUR_LABEL.text = v.add1;

Your outlet i-vars are nil's inside your custom initializer. 您的出口i-vars在自定义初始化程序中为nil。 That's why values are not set. 这就是为什么未设置值的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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