IBOutlet does not display its value in UI

故事扮演 提交于 2020-01-11 13:29:09

问题


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.

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

    }

回答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.




回答2:


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



来源:https://stackoverflow.com/questions/27705114/iboutlet-does-not-display-its-value-in-ui

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!