简体   繁体   中英

Trouble Calling A Phone Number Method From An App Delegate String Variable

I'm trying to pass a string variable from the App Delegate into the a method to call that number as a phone number. I declared the string in the app delegate and then I set the string in another view controller.

This is in the DetailViewController.m. Note: I have NSLogged it in this implementation file and it logs correctly, so the value is being carried over successfully, I suppose there is just something wrong with this method. Maybe I'm unclear on some of the syntax that's possible for this method:

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

The error I get is "Too many arguments to method call, expected 1, have 2." I'm not sure what that's referencing.


I also tried this:

First, setting the value of phoneNumber to a UILabel like so:

TheAppDelegate *delegate = (TheAppDelegate *)[[UIApplication sharedApplication] delegate];
    phoneLabel.text = delegate.phoneNumber;

And then trying it like this:

-(IBAction)callPhone:(id)sender {
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel:%@", phoneLabel.text]];
}

This gives me the same error "Too many arguments to method call, expected 1, have 2." Again, not exactly sure what that's referencing.

Now, I know I know, singletons are what I should be using but I still haven't wrapped my head around them. I'm reading up on them and I figure this is an easy way to go about right now while I'm testing the app and learning the singleton code. Hopefully there's a quick fix! Thanks for the help.

[NSURL URLWithString] takes only a complete NSString, not a format + arguments.

So what you really want is:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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