Xcode 在视图控制器(iOS)中调用时,应用程序委托的NSString不保留值

Xcode 在视图控制器(iOS)中调用时,应用程序委托的NSString不保留值,xcode,delegates,nsstring,uilabel,Xcode,Delegates,Nsstring,Uilabel,我一辈子都无法弄明白为什么我不能在我的应用程序委托中设置NSString,然后在不同的视图控制器中调用它 我所做的是在MKMapView上设置注释。我使用calloutAccessoryControlTapped方法将视图控制器推送到“DetailsViewController”。在DetailsViewController中,我将注释中的标题和副标题显示为页面上的UILabels(基本上是标题和地址)。由于在注释中只能设置两个值(标题和副标题),并且我还希望在DetailsViewContro

我一辈子都无法弄明白为什么我不能在我的应用程序委托中设置NSString,然后在不同的视图控制器中调用它

我所做的是在MKMapView上设置注释。我使用calloutAccessoryControlTapped方法将视图控制器推送到“DetailsViewController”。在DetailsViewController中,我将注释中的标题和副标题显示为页面上的UILabels(基本上是标题和地址)。由于在注释中只能设置两个值(标题和副标题),并且我还希望在DetailsViewController中显示位置的电话号码,因此我决定在应用程序委托中将电话号码设置为NSString。然后,我会在DetailsViewController中将该值作为UILabel调用

我已经在应用程序委托中创建并合成了NSString作为“phoneNumber”

然后我将app delegates.h文件导入到我正在使用的实现文件中

由于某些原因,它无法确保我设置的值。我试着从很多地方注销它,但它不起作用。当我调用calloaccessorycontroltapped方法并尝试记录电话号码的值时,它是(null)。当我为任何注释打开DetailsViewController时,它只返回一个特定的电话号码,无论我单击哪个。因此,不用多说,下面是代码:

位置VIEWCONTROLLER.m:

//Miller's Neighborhood Market #9
MillersLocations *ann09 = [[MillersLocations alloc]init];
ann09.title = @"Neighborhood Market #9";
ann09.subtitle = @"1700 Todds Lane, Hampton, VA 23666";
ann09.coordinate = CLLocationCoordinate2DMake(37.0395580, -76.4096930);
[mapView addAnnotation:ann09];
[ann09 release];
delegate.phoneNumber = @"(757) 826-7608";

//Miller's Neighborhood Market #10
MillersLocations *ann10 = [[MillersLocations alloc]init];
ann10.title = @"Neighborhood Market #10";
ann10.subtitle = @"2722 Airline Blvd., Portsmouth, VA 23701";
ann10.coordinate = CLLocationCoordinate2DMake(36.8110840, -76.3644950);
[mapView addAnnotation:ann10];
[ann10 release];
delegate.phoneNumber = @"(757) 488-0223";

//Miller's Neighborhood Market #16
MillersLocations *ann16 = [[MillersLocations alloc]init];
ann16.title = @"Neighborhood Market #16";
ann16.subtitle = @"3801 Indian River Rd., Chesapeake, VA 23325";
ann16.coordinate = CLLocationCoordinate2DMake(36.822247, -76.233070);
[mapView addAnnotation:ann16];
[ann16 release];
delegate.phoneNumber = @"(757) 420-1866";

//Miller's Neighborhood Market #21
MillersLocations *ann21 = [[MillersLocations alloc]init];
ann21.title = @"Neighborhood Market #21";
ann21.subtitle = @"2129 W. Mercury Blvd., Hampton, VA 23666";
ann21.coordinate = CLLocationCoordinate2DMake(37.0385289, -76.4004810);
[mapView addAnnotation:ann21];
[ann21 release];
delegate.phoneNumber = @"(757) 826-5237";

//Miller's Neighborhood Market #23
MillersLocations *ann23 = [[MillersLocations alloc]init];
ann23.title = @"Neighborhood Market #23";
ann23.subtitle = @"13797 Warwick Blvd., Newport News, VA 23602";
ann23.coordinate = CLLocationCoordinate2DMake(37.124062, -76.531425);
[mapView addAnnotation:ann23];
[ann23 release];
delegate.phoneNumber = @"(757) 874-5806";
我不确定每次设置这些值后是否应该释放这些值。好的,下面是我如何在DetailsViewController.m中调用它们:

-(IBAction)callPhone:(id)sender {
    Miller_Tab_BarAppDelegate *delegate = (Miller_Tab_BarAppDelegate *)[[UIApplication sharedApplication]delegate];
    delegate.phoneNumber=phoneLabel.text;
    NSLog(@"%@", delegate.phoneNumber);
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat: @"tel:%@", phoneLabel.text]]];
}

每次注销时(无论我使用哪个注释推送视图控制器),它都会显示位置23的编号。我不明白为什么它会显示那个数字,为什么它不会改变值?有人知道吗?谢谢。

访问此页面,他使用多个注释并使用for循环放置其中5个注释

希望这有帮助


J

在AppDelegate.h文件中,定义appd(AppDelegate*)[[UIApplication sharedApplication]delegate]

(这是一个速记宏。此处的“AppDelegate”应替换为AppDelegate类的名称。)

在viewcontroller中,使用
[appd setPhoneNumber:@“705-123 456”]更改对象//注意第一个字母p在前缀为“set”时如何变成p。


如果仍然有问题,您应该创建
AppDelegate*appdel=appd
在设置电话号码的地方,在那里设置断点,并检查phoneNumber和appdel是否正确初始化。(将鼠标悬停在变量名称上,将说明打印到控制台。)

此评论与您的问题无关,但您肯定应该使用plist来存储您的联系信息数组,而不是将其硬编码到应用程序中。我不确定我是否理解您在这里尝试的操作。您多次设置单个值,您希望发生什么?很好,谢谢。很明显,我仍然在开始阅读一些关于plists的文章,也许是时候让我投入进去了……不,我正试图通过点击注释按钮设置一次并显示它。也许我的头有点乱,我在向后思考。这与我在上面所说的不同。我在放置管脚时没有遇到问题,它控制了当您单击注释中的callout附件按钮时调用的Details View控制器的内容。