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

发布于 2024-12-16 21:56:39 字数 3032 浏览 4 评论 0原文

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

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

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

然后我将应用程序委托 .h 文件导入到我正在使用的实现文件中。

由于某种原因,它无法保证我设置的值。我尝试从多个地点将其注销,但没有成功。当我调用 calloutAccesoryControlTapped 方法并尝试记录电话号码的值时,它是(空)。当我打开任何注释的 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 的编号。我不明白为什么它显示该数字以及为什么它不会改变该值?有人有什么想法吗?谢谢。

I cannot for the life of me, figure out why I can't set an NSString located in my app delegate and then recall it in a different view controller.

What I've done is set up annotations on a MKMapView. I used the calloutAccessoryControlTapped method to push the View Controller to "DetailsViewController." In DetailsViewController, I display the title and subtitle from the annotation as UILabels on the page (it's basically a title and an address). Since there are only two values you can set in the annotation (title and subtitle) and I also want to display the phone number of the locations in the DetailsViewController, I decided to set the phone number as an NSString in the app delegate. Then, I would recall that value in DetailsViewController as a UILabel.

I've created and synthesized the NSString in the app delegate as "phoneNumber."

Then I imported the app delegates .h file into implementation file I'm working with.

For some reason, it won't secure the value that I set to it though. I've tried to log it out from numerous spots and it doesn't work. When I call the calloutAccesoryControlTapped method and try to log the value of the phone number it's (null). When I open the DetailsViewController for any annotation it only returns one specific phone number, no matter which one I click. So, without further ado, here's the code:

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";

I'm not sure if I'm supposed to be releasing these values after every time I set them. Ok and then here is how I try to call them in the 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]]];
}

Every single time it logs out (no matter which annotation I use to push the view controller) it always displays the number for location #23. I can't figure out why it's displaying that number AND why it won't change the value? Anybody have any idea? Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

韶华倾负 2024-12-23 21:56:40

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

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

希望这有帮助

J

visit this page, he uses multiple annotations and uses a for loop to place 5 of them

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

Hope this helps

J

暖心男生 2024-12-23 21:56:40

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

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

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

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

In your AppDelegate.h file, #define appd (AppDelegate *)[[UIApplication sharedApplication] delegate]

(This is a shorthand macro. "AppDelegate" everywhere here should be replaced with the name of your appdelegate class.)

In your viewcontroller, change the object with [appd setPhoneNumber:@"705-123 456"]; //note how the first letter p becomes P when prefixed with 'set'.

If you still have trouble, you should create AppDelegate *appdel=appd; where you set the phone number, set a breakpoint there and check that phoneNumber and appdel is initalized properly. (Hover over variable name, Print Description to console.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文