【问题标题】:Converting UITextField to UILabel through view Controllers通过视图控制器将 UITextField 转换为 UILabel
【发布时间】:2012-03-02 15:09:23
【问题描述】:

我相信对此有一个简单的解释。 我希望传输数据(而不是存储),但将用户键入的文本传输到文本字段中,并将其显示为另一个 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];
}

我的 secondview 控制器 .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];
}

【问题讨论】:

    标签: objective-c uiviewcontroller uitextfield uilabel


    【解决方案1】:

    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

    【讨论】:

    • 检查您所有的 Outlets 是否连接正确
    【解决方案2】:

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

    【讨论】:

    • 谢谢,我没有收到任何错误。但它仍然不会显示标签中的文本。不过我会继续玩它。
    • @ZachBamberger - 你如何显示第二个视图控制器?您可以发布您为该部分提供的代码吗?
    • nameStats.text = [NSString stringWithFormat:@"%@", self.firstName];
    • 什么意思?用户推送计算后,我将其设为“模态”。
    • 所以您在calculate 返回后的某个时间点调用[self presentModalViewController:secondViewController animated:YES]
    【解决方案3】:

    如果是导航,则可以在您的 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];
    

    【讨论】:

    • 谢谢,我没有收到任何错误。但它仍然不会显示标签中的文本。不过我会继续玩它。
    • 试过了 nameStats.text = [NSString stringWithFormat:@"%@", firstName];它们都不起作用,setText 标签显示为空。使用 stringWithFormat 它显示为 null。
    • 你设置所有者为IB中的标签了吗?
    • 您能否编辑您的初始帖子以显示整个 ShowStats.h 以及 .m 中所有带有标签或名字/第二名的内容被修改的地方?
    • 好的,已编辑...希望对您有所帮助
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-08
    • 2015-07-24
    相关资源
    最近更新 更多