通过视图控制器将 UITextField 转换为 UILabel

发布于 2025-01-04 09:38:34 字数 2717 浏览 2 评论 0原文

我确信对此有一个简单的解释。 我希望传输数据(而不是存储),但将用户输入的文本传输到文本字段中,并将其显示为另一个 ViewController 中的 UILabel。 我已经知道如何将用户输入的文本转换为同一视图控制器上的标签。 我想我的问题是导入。

.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];
}

I'm sure there is a simple explanation for this.
I am looking to transfer the data (not store) but transfer the text the user types into a text field and display it as a UILabel in another ViewController.
I already know how to convert text entered by the user into a label on the same viewcontroller.
I guess my problem is importing.

the .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

the .m:

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

my secondview controller .m (ShowStats):

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


[super viewDidLoad];
}

Many thanks!
EDIT

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];
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

情绪操控生活 2025-01-11 09:38:34

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;
}

然后将这些字符串设置为 viewDidLoad 中的 UILablel

Make these properties in ShowStats class

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

and change this to this

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

then set these strings to UILablel in your viewDidLoad

但可醉心 2025-01-11 09:38:34

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 中,实现 计算像这样:

-(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];
}

In ShowStats.h, add the following:

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

In ShowStats.m add/update the following:

@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];
}

Finally, in ViewController.m, implement calculate like this:

-(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];
}
忘年祭陌 2025-01-11 09:38:34

如果它是导航,那么您可以在 SecondViewController 中调用:

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

或者您可以对单视图应用程序执行以下操作:

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];

If it is a navigation then in your SecondViewController you can call:

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

Or you could do the following for a Single View app:

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

* EDIT *

Add two labels to your .h file (nonatomic, retain) & synthesize in .m . Initialize the labels in viewDidLoad then set them (assuming they are called label1 and label2):

[label1 setText:firstName];
[label2 setText:secondName];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文