Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Dial a Phone Number/Website/SMS from a button, but get info from a Table View

greeneye82greeneye82 Posts: 21Registered Users
edited May 2012 in Tutorial Requests
I've been struggling with this for a few weeks now on my own, and now I am looking for help. I have a list of cities in a Table View. When the user selects a row (a city) they are pushed to the next screen which will have a button. When the user presses the button they will be calling a number associated with the city. It seems basic enough anyway.
I have the code to dial a number when the button is pressed. However I cannot seem to figure out how to simplify this process. I would think I would have to use a variable. My NSMutable array has the city name and phone number for each city in my table view controller implementation file (I am not using a plist). Is there a way I can do this without typing the whole UIApplication sharedApplication code for every city? I have about 75 of them in my table.
A YouTube video on this would be awesome!!!
I'm using Xcode 4.3.2, with storyboards and ARC.
Post edited by greeneye82 on

Replies

  • Duncan CDuncan C Posts: 9,114Tutorial Authors, Registered Users @ @ @ @ @ @ @
    edited May 2012
    greeneye82 wrote: »
    I've been struggling with this for a few weeks now on my own, and now I am looking for help. I have a list of cities in a Table View. When the user selects a row (a city) they are pushed to the next screen which will have a button. When the user presses the button they will be calling a number associated with the city. It seems basic enough anyway.
    I have the code to dial a number when the button is pressed. However I cannot seem to figure out how to simplify this process. I would think I would have to use a variable. My NSMutable array has the city name and phone number for each city in my table view controller implementation file (I am not using a plist). Is there a way I can do this without typing the whole UIApplication sharedApplication code for every city? I have about 75 of them in my table.
    A YouTube video on this would be awesome!!!
    I'm using Xcode 4.3.2, with storyboards and ARC.

    Ok. So you have a view controller that manages a table view, and when the user selects a cell, you push a second view controller that has a dial button on it.

    What you need to do is to either pass the phone number to the second view controller, or make the data source array available, and pass the index.

    If you want to pass the phone number, set up a phone number property in the second view controller. Then in the first view controller's prepareForSegue method, get the phone number for the selected cell and set it in the second view controller that you are transitioning to.

    In the second view controller, write an IBAction method that takes the phone number from the view controller's phone number property and uses it to invoke a "tel://" URL.

    That's should do it.
    Regards,
    Duncan C
    WareTo

    widehead.gif
    Animated GIF created with Face Dancer, available for free in the app store.

    I'm available for one-on-one help at CodeMentor
  • greeneye82greeneye82 Posts: 21Registered Users
    edited May 2012
    Duncan C wrote: »
    Ok. So you have a view controller that manages a table view, and when the user selects a cell, you push a second view controller that has a dial button on it.

    What you need to do is to either pass the phone number to the second view controller, or make the data source array available, and pass the index.

    If you want to pass the phone number, set up a phone number property in the second view controller. Then in the first view controller's prepareForSegue method, get the phone number for the selected cell and set it in the second view controller that you are transitioning to.

    In the second view controller, write an IBAction method that takes the phone number from the view controller's phone number property and uses it to invoke a "tel://" URL.

    That's should do it.


    I seem to be able to pass over the phone number to the second view, however I cannot seem to invoke the phone number into the UIApplication SharedApplication code. Can you give any examples of what that code may look like with "phone" being the property?
  • Duncan CDuncan C Posts: 9,114Tutorial Authors, Registered Users @ @ @ @ @ @ @
    edited May 2012
    greeneye82 wrote: »
    I seem to be able to pass over the phone number to the second view, however I cannot seem to invoke the phone number into the UIApplication SharedApplication code. Can you give any examples of what that code may look like with "phone" being the property?



    Take the telephone number and build it into an URL string of the form

    tel://phone_number

    like

    tel://2025551212
    (example US phone number for a domestic call; other country's number format may vary)

    Then create an NSURL objet with that URL string.

    Finally, invoke openURL:
    [[NSApplication sharedApplication] openURL: phoneURL];
    
    Regards,
    Duncan C
    WareTo

    widehead.gif
    Animated GIF created with Face Dancer, available for free in the app store.

    I'm available for one-on-one help at CodeMentor
  • greeneye82greeneye82 Posts: 21Registered Users
    edited May 2012
    This is what I have so far...

    TableView controller .m

    NSMutableArray *dept;

    -(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    if ([segue.identifier isEqualToString:@ShowInfo]) {
    InfoViewController *ivc = [segue destinationViewController];
    NSIndexPath *path = [self.tableView indexPathForSelectedRow];
    City *c = [dept objectAtIndex:[path row]];
    [ivc setCurrentCity:c];
    }
    }

    - (void)viewDidLoad

    {
    [super viewDidLoad];

    dept = [[NSMutableArray alloc] init];

    City *thedept = [[City alloc] init];
    [thedept setCityname:@New York];
    [thedept setPhone:@tel//5551234567];
    [dept addObject:thedept];

    thedept = [[City alloc] init];
    [thedept setCityname:@Springfield];
    [thedept setPhone:@tel://5558973265];
    [dept addObject:thedept];



    in my info view controller (the second view controller)
    .h

    @property (strong, nonatomic) City *currentCity;
    @property (strong, nonatomic) City *phone;

    @property (weak, nonatomic) IBOutlet UILabel *PhoneNumberLabel;

    - (IBAction)CallDept:(id)sender;

    @end

    in my info view controller (the second view controller)
    .m

    @synthesize currentCity;
    @synthesize PhoneNumberLabel;
    @synthesize phone;

    -(IBAction) CallDept:(id)sender {

    [[UIApplication sharedApplication]
    openURL:[NSURL URLWithString: phone];

    }


    I am able to pass over and display the phone number and title for the selected row, however when the user presses the CallDept button it does not pull that number from the table view. However, if I finish the UIApplication shared application code and place in a phone number in the second view .m then It will go to the phone, however i am stuck with only that number. Basically, my issue now is that my second view .m does not seem to pull over each phone number in order to dial it, and I was hoping to just do it with 'phone' being like a variable.
  • Duncan CDuncan C Posts: 9,114Tutorial Authors, Registered Users @ @ @ @ @ @ @
    edited May 2012
    greeneye82 wrote: »
    This is what I have so far...

    TableView controller .m

    NSMutableArray *dept;

    -(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    if ([segue.identifier isEqualToString:@ShowInfo]) {
    InfoViewController *ivc = [segue destinationViewController];
    NSIndexPath *path = [self.tableView indexPathForSelectedRow];
    City *c = [dept objectAtIndex:[path row]];
    [ivc setCurrentCity:c];
    }
    }

    - (void)viewDidLoad

    {
    [super viewDidLoad];

    dept = [[NSMutableArray alloc] init];

    City *thedept = [[City alloc] init];
    [thedept setCityname:@New York];
    [thedept setPhone:@tel//5551234567];
    [dept addObject:thedept];

    thedept = [[City alloc] init];
    [thedept setCityname:@Springfield];
    [thedept setPhone:@tel://5558973265];
    [dept addObject:thedept];



    in my info view controller (the second view controller)
    .h

    @property (strong, nonatomic) City *currentCity;
    @property (strong, nonatomic) City *phone;

    @property (weak, nonatomic) IBOutlet UILabel *PhoneNumberLabel;

    - (IBAction)CallDept:(id)sender;

    @end

    in my info view controller (the second view controller)
    .m

    @synthesize currentCity;
    @synthesize PhoneNumberLabel;
    @synthesize phone;

    -(IBAction) CallDept:(id)sender {

    [[UIApplication sharedApplication]
    openURL:[NSURL URLWithString: phone];

    }


    I am able to pass over and display the phone number and title for the selected row, however when the user presses the CallDept button it does not pull that number from the table view. However, if I finish the UIApplication shared application code and place in a phone number in the second view .m then It will go to the phone, however i am stuck with only that number. Basically, my issue now is that my second view .m does not seem to pull over each phone number in order to dial it, and I was hoping to just do it with 'phone' being like a variable.

    Please use code tags. It's very hard to read code with smilies in it.

    Several things. In your prepareForSegue, it looks like you are passing the City object for the selected row to the second view controller, but not setting up the phone number. That's fine, but then you should not expect the phone number to be in the phone property when you get to the second view controller.

    In your callDept method (don't make method names start with a capital letter. Only class names and protocol names should be capitalized) you should get the phone number string from the city property, and build a URL string out of it, with the prefix "tel://". You can't use openURL with just a number.


    -(IBAction) callDept:(id)sender 
    {
      NSString *callURLString = [NSString stringWithFormat: @"tel://%@", self.currentCity.phone];
      NSURL *callURL  = [NSURL URLWithString: callURLString];
        [[UIApplication sharedApplication] openURL: callURL];
    }
    
    Regards,
    Duncan C
    WareTo

    widehead.gif
    Animated GIF created with Face Dancer, available for free in the app store.

    I'm available for one-on-one help at CodeMentor
  • greeneye82greeneye82 Posts: 21Registered Users
    edited May 2012
    Duncan C wrote: »
    Please use code tags. It's very hard to read code with smilies in it.

    Several things. In your prepareForSegue, it looks like you are passing the City object for the selected row to the second view controller, but not setting up the phone number. That's fine, but then you should not expect the phone number to be in the phone property when you get to the second view controller.

    In your callDept method (don't make method names start with a capital letter. Only class names and protocol names should be capitalized) you should get the phone number string from the city property, and build a URL string out of it, with the prefix "tel://". You can't use openURL with just a number.


    -(IBAction) callDept:(id)sender 
    {
      NSString *callURLString = [NSString stringWithFormat: @"tel://%@", self.currentCity.phone];
      NSURL *callURL  = [NSURL URLWithString: callURLString];
        [[UIApplication sharedApplication] openURL: callURL];
    }
    


    Thank you for your assistance so far. I made the changes you suggested. Initially, there were no errors. However when I tested it in the simulator (changing to websites rather than phone numbers) it threw a signal SIGABRT error only when I press on the call button
    @autoreleasepool {
            return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
    

    I feel I am getting closer than I was. I believe I am passing my phone number data to my second view controller (info view) because I can get the correct number to display in a label I have set up there. When the user selects the city in the table view row it passes along the city name and phone number into the second view. So my issue still is that I can't get the button to dial that indicated phone number (or show the website in the simulator). Have any other ideas? Or do you know of any tutorial videos that may demonstrate the IBAction method in relation to table views?

    thanks for all your help.
  • greeneye82greeneye82 Posts: 21Registered Users
    edited May 2012
    Nevermind, I just got it! It works good now, with your code help. Apparently I did not properly have the callDept property set up.

    Thanks for all your help.
Sign In or Register to comment.