来自App Delegate的NSString在视图控制器(iOS)中重新调用时不保留值

时间:2011-11-20 06:21:54

标签: xcode delegates nsstring uilabel

我不能为我的生活,弄清楚为什么我不能在我的app委托中设置一个NSString,然后在另一个视图控制器中调用它。

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

我已经在app delegate中创建并合成了NSString为“phoneNumber。”

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

出于某种原因,它不会保护我设置的值。我试图从众多地方登出它并且它不起作用。当我调用calloutAccesoryControlTapped方法并尝试记录电话号码的值时(null)。当我为任何注释打开DetailsViewController时,它只返回一个特定的电话号码,无论我点击哪一个。所以,不用多说了,这是代码:

LocationsViewController .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的编号。我无法弄清楚为什么它显示这个数字以及为什么它不会改变这个值?有人有什么想法吗?感谢。

2 个答案:

答案 0 :(得分:0)

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

http://www.highoncoding.com/Articles/804_Introduction_to_MapKit_Framework_for_iPhone_Development.aspx

希望这有帮助

Ĵ

答案 1 :(得分:0)

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

(这是一个速记宏。" AppDelegate"这里的每个地方都应该用appdelegate类的名称替换。)

在viewcontroller中,使用[appd setPhoneNumber:@"705-123 456"]; //note how the first letter p becomes P when prefixed with 'set'.

更改对象

如果您仍然遇到问题,则应创建AppDelegate *appdel=appd;,在其中设置电话号码,在那里设置断点并检查phoneNumber和appdel是否正确无效。 (将鼠标悬停在变量名称上,将Print Description打印到控制台。)