0

I am building an iOS app using storyboards.I have implemented google maps in my app using swift and rest of the code is in objective C.

My scenario:

1.As shown in the diagram, when I click on the label in front of location ( in First view controller ) a modal segue opens a second view controller containing map.

2.In the map view controllers, when the user moves the map with touch, I'm getting address value in a label. ( similar to uber app )

3.After this , as soon as user presses done button on this controller, it gets back to the first view controller using custom segue and with the value of the location.

enter image description here

The problem is that, iboulet of this label is hidden in the UI and doesn’t display its value. But when I print this value in console, I am able to display the value.

Below is my code. Help is much appreciated.

First View Controller code written in swift:

- (void)viewDidLoad {
    [super viewDidLoad];
    _Locationlabel.text=_location;
    NSLog(@"location=%@",_Locationlabel.text);
}

Second View Controller code:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
        if (segue.identifier == "locationDone") {
            // pass data to next view

            var svc = segue.destinationViewController as Host;

            svc.location = self.addressLabel.text;
            println("location\(svc.location)");
        }
    }

    @IBAction func doTap(x:UIButton) {

     self.dismissViewControllerAnimated(true, completion: {});//This is intended to dismiss the Info sceen.
        println("pressed")

    }
bobnoble
  • 5,754
  • 3
  • 23
  • 31
Hitesh
  • 41
  • 1
  • 2
  • 10

2 Answers2

1

You shouldn't use a custom segue to return to your first view controller. Segues always create a new instance of a view controller (unless you use an unwind segue). In your first view controller, add this method:

- (IBAction)backFromMapView:(UIStoryboardSegue *)segue
{
    NSLog(@"and we are back");
    MapViewController* mvc = (MapViewController *)segue.sourceViewController;
    NSLog(@"location %@", mvc.addressLabel.text);
}

Don't forget to #import <YourProjectName>-Swift.h so that Host.m will know about MapViewController.

Then you'd control drag from the Done button in your Map View Controller to the orange exit icon in the bar above the view controller in the Storyboard. In the pop up, you'd select backFromMapView and voila, you're done.

enter image description here

vacawama
  • 133,454
  • 26
  • 238
  • 261
  • Thanks vacawama for this detailed response. I would implement this and post an update. Thanks so much ! – Hitesh Dec 30 '14 at 17:42
  • Hi @vacawama, I was applying your method. I have imported #import "MapViewController.swift" in Host.m But when I apply - (IBAction) method, it is gives me an error at statement MapViewController* mvc = (MapViewController *)segue.sourceViewController; and error is Use of undeclared identifier 'MapViewController'; did you mean 'UIViewController'? Could you please help ?" – Hitesh Dec 30 '14 at 18:24
  • Is your project called Playo? If so, `#import Playo-Swift.h` – vacawama Dec 30 '14 at 19:09
  • Thanks a lot @vacawama, the problem got solved by the method you suggested. – Hitesh Dec 30 '14 at 19:12
0

Try storing the location as a NSString strong property of your Host view controller OR Post a notification to your host view from Maps And in the viewWillAppear of the Host, First- NSLog the NSString and Second- assign this string to the label as text

Amisha
  • 159
  • 1
  • 8
  • Hi Amisha, thanks for your quick response. 1. I have already used strong property for location:@property (nonatomic, strong) NSString *location; 2. I tried applying second method which you suggested but I did not get value of location in viewWillAppear but instead, getting the value in viewDidLoad – Hitesh Dec 30 '14 at 12:24
  • Could you please let me know, where am I going wrong ? – Hitesh Dec 30 '14 at 12:30