Преобразование UITextField в UILabel через контроллеры представления - PullRequest
1 голос
/ 10 февраля 2012

Я уверен, что этому есть простое объяснение.Я хочу передать данные (не хранить), но перенести текст, который пользователь вводит в текстовое поле и отобразить его как UILabel в другом ViewController.Я уже знаю, как преобразовать текст, введенный пользователем, в метку на том же viewcontroller.Я предполагаю, что моя проблема заключается в импорте.

.h:

@interface ViewController : UIViewController {
IBOutlet UITextField *firstPerson;
IBOutlet UITextField *secondPerson;
IBOutlet UIButton *calculateButton;
NSString *firstName;
NSString *secondName;
}
@property (nonatomic, retain) IBOutlet UITextField *firstPerson;
@property (nonatomic, retain) IBOutlet UITextField *secondPerson;
@property (nonatomic, retain) NSString *firstName;
@property (nonatomic, retain) NSString *secondName;
@property (nonatomic, retain) IBOutlet UIButton *calculateButton;
-(IBAction)calculate;
@end

.m:

-(IBAction)calculate {
//Linked to UIButton
//This is the first View Controller.
//    firstName = firstPerson.text;
//    secondName = secondPerson.text;
secondViewController = [[ShowStats alloc] init];
}

моего контроллера второго просмотра .m (ShowStats):

#import "ShowStats.h"
#import "ViewController.h"
- (void)viewDidLoad
{
firstName = firstPerson.text;
secondName = secondPerson.text;


[super viewDidLoad];
}

Большое спасибо! РЕДАКТИРОВАТЬ

ViewController.h

#import <UIKit/UIKit.h>
#import "ShowStats.h"

@interface ViewController : UIViewController {
IBOutlet UITextField *firstPerson;
IBOutlet UITextField *secondPerson;
IBOutlet UIButton *calculateButton;
//NSString *firstName;
// NSString *secondName;
}
@property (nonatomic, retain) IBOutlet UITextField *firstPerson;
@property (nonatomic, retain) IBOutlet UITextField *secondPerson;
//@property (nonatomic, retain) NSString *firstName;
//@property (nonatomic, retain) NSString *secondName;
@property (nonatomic, retain) IBOutlet UIButton *calculateButton;
-(IBAction)calculate;
@end

ViewController.m

#import "ViewController.h"
#import "ShowStats.h"

@implementation ViewController
@synthesize firstPerson, secondPerson;
//@synthesize firstName, secondName;
@synthesize calculateButton;
ShowStats *secondViewController;

-(IBAction)calculate {
secondViewController = [[ShowStats alloc] init];
secondViewController.firstName = firstPerson.text;
}

ShowStats.h

@interface ShowStats : UIViewController{

IBOutlet UILabel *nameStats;
}
@property (nonatomic, retain) IBOutlet UILabel *nameStats;
@property (nonatomic, retain) NSString *firstName;
@property (nonatomic, retain) NSString *secondName;
@end

ShowStats.m

- (void)viewDidLoad
{
nameStats.text = [NSString stringWithFormat:@"%@", firstName];    
//secondLabel.text = self.secondName;


[super viewDidLoad];
}

Ответы [ 3 ]

1 голос
/ 10 февраля 2012

Сделать эти свойства в ShowStats классе

@property (nonatomic, retain) NSString *firstName;
@property (nonatomic, retain) NSString *secondName;

и измените это на

-(IBAction)calculate {
     secondViewController = [[ShowStats alloc] init];
     secondViewController.firstName = firstPerson.text;
     secondViewController.secondName = secondPerson.text;
}

затем установите эти строки на UILablel в вашем viewDidLoad

0 голосов
/ 10 февраля 2012

Если это навигация, то в вашем SecondViewController вы можете вызвать:

ViewController *viewController = [self.navigationController.viewControllers objectAtIndex:0];
firstName = [[viewController firstPerson] text];
secondName = [[viewController secondPerson] text];

Или вы можете сделать следующее для приложения Single View:

AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
ViewController *viewController = [appDelegate viewController];
firstName = [[viewController firstPerson] text];
secondName = [[viewController secondPerson] text];

* РЕДАКТИРОВАТЬ *

Добавьте две метки в ваш файл .h (неатомный, сохраните) и синтезируйте в .m. Инициализируйте метки в viewDidLoad, затем установите их (при условии, что они называются label1 и label2):

[label1 setText:firstName];
[label2 setText:secondName];
0 голосов
/ 10 февраля 2012

В ShowStats.h добавить следующее:

@property(nonatomic, copy) NSString* firstName;
@property(nonatomic, copy) NSString* secondName;

В ShowStats.m добавить / обновить следующее:

@synthesize firstName, secondName;

//...

- (id) init {
    if (self = [super init]) {
        self.firstName = nil;
        self.secondName = nil;
        //...
    }

    return self;
}

- (void) dealloc {
    self.firstName = nil;
    self.secondName = nil;
    //...

    [super dealloc];
}

- (void)viewDidLoad {
    //or even better, do this in viewWillAppear instead
    firstLabel.text = self.firstName;
    secondLabel.text = self.secondName;
    //...


    [super viewDidLoad];
}

Наконец, в ViewController.m, внедрить calculate вот так:

-(IBAction)calculate {
    //Linked to UIButton
    //This is the first View Controller.
    //    firstName = firstPerson.text;
    //    secondName = secondPerson.text;
    secondViewController = [[ShowStats alloc] init];
    secondViewController.firstName = firstPerson.text;
    secondViewController.secondName = secondPerson.text;

    //display the view controller here

    [secondViewController autorelease];
}
...