无法在由自定义 UIViewController 控制的 UILabel 上设置文本
我有一个 Mapkit 应用程序,我在其中创建了一个自定义标注视图。不过,我似乎无法更新视图上的标签。这是我的设置:
我有一个名为 VendorAnnotation 的自定义注释类,具有 5 个字符串属性(电话和 4 个地址行)。 VendorAnnotation 对象将其 rightCalloutAccessoryView 设置为 UIButton。在委托方法中: `(void)mapView:(MKMapVIew *)mapView commentView:(MKAnnotationView *)view calloutAccessroyControlTapped:(UIControl *)control{}
我通过注释属性访问 5 个字符串:
VendorAnnotation v = (VendorAnnotation*) view.annotation; 然后我有: DetailViewController *detailVC = [[DetailViewController alloc] initWithDetails:v.phone add1:v.add1 add2:v.add2 add3:v.add3 add4:v.add4]; 通过逐步执行,我知道此时这些字符串属性 (v.*) 具有准确的值。 问题是,这个 initWithDetails 方法没有设置标签文本。这是我的 DetailViewController 类的代码:
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;
}
我不知道为什么,但标签的文本属性永远不会设置为适当的字符串。请帮忙。我知道这很愚蠢,我是 Objective C 和 iOS 编程的新手,但我没有看到问题所在。我可能应该提到,我已将 IB 中 .xib 上的标签连接到相应的 IBOutlet 属性。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
也许是因为“p”,“a”,“aa”...没有启动。
尝试在
- (void)viewDidLoad
上而不是 init 上尝试。或者而不是使用:
为什么不使用自定义 init 方法,然后执行如下操作:
或
maybe because "p", "a", "aa"... aren't initiated..
Try it on
- (void)viewDidLoad
instead init.Or instead of using:
why don't you use the custom init method and then do something like this:
or
您的插座 i-vars 在您的自定义初始化程序中为零。这就是为什么没有设置值的原因。
Your outlet i-vars are nil's inside your custom initializer. That's why values are not set.