Federico Ciabatta
Federico Ciabatta

Reputation: 25

Label Result in another Viewcontroller

Good Morning Guys,

I have a problem. I have an application that do a calculate. If i insert the Label in a same ViewController i watch all.

How do I insert the same Label in another View?

This is the view where there is the calculate.

H.

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@property (strong, nonatomic) IBOutlet UITextField *textField1;
@property (strong, nonatomic) IBOutlet UITextField *textField2;

@property (strong, nonatomic) IBOutlet UIButton *result;

@property (strong, nonatomic) IBOutlet UIDatePicker *data;


@end

M.

//
//  ViewController.m
//  IEcigarette
//
//  Created by Federico on 24/11/12.
//  Copyright (c) 2012 Federico. All rights reserved.
//

#import "ViewController.h"
#import "ResultViewController.h"



@interface ViewController ()
@property (strong, nonatomic) NSString * myString;

@property (strong, nonatomic) UILabel * myLabel;
@end

@implementation ViewController

-(IBAction)calculate:(id)sender{



    NSDate *past = _data.date ;
    NSDate *now = [NSDate date];

    NSCalendar *gregorianCalendar = [[NSCalendar alloc]
                                     initWithCalendarIdentifier:NSGregorianCalendar];
    NSUInteger unitFlags = NSDayCalendarUnit;
    NSDateComponents *components = [gregorianCalendar components:unitFlags
                                                        fromDate:past
                                                          toDate:now
                                                        options:0];
    int z = [components day];
    int a = ([_textField1.text intValue]);
    int b = a*([_textField2.text intValue]);

    int r = b * z / 20;

    _myLabel.text = [[NSString alloc] initWithFormat:@"%d", r];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (BOOL)textFieldShouldReturn:(UILabel *)myLabel {

    [myLabel resignFirstResponder];

    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
        [self performSegueWithIdentifier:@"detail" sender:self];
    }

    return YES;
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"detail"]) {

        ResultViewController* destVC = (ResultViewController*)segue.destinationViewController;

        destVC.myString = self.myLabel.text;
        destVC.navigationItem.title = self.myLabel.text;

    }
}

@end

Thank you

Upvotes: 0

Views: 149

Answers (2)

Edwin Iskandar
Edwin Iskandar

Reputation: 4119

You could keep a reference to it in a shared and call:

[yourLabel removeFromSuperview];
[yourViewController.view addSubview:yourLabel];

for every view controller you want to display it on.

However if what you're sharing is pretty much functioning as a UILabel, I would recommend using separate UILabel instances for each view controller and just sharing the .text value across views. You could pass the value in prepareForSegue, save it to NSUserDefaults, set it as a property of some shared object/singleton.

Upvotes: 0

SARANGA
SARANGA

Reputation: 172

why you are trying to insert the same label to the another view also. just pass the label.text (string) to another view controller's string. then use that string and create a new label with this string in the viewdidload method . you can also pass the value using nsuserdefaults.

Upvotes: 2

Related Questions