设置 UITextField 文本

发布于 2024-10-26 19:02:36 字数 2524 浏览 1 评论 0原文

我的应用程序中有两个视图,由 RootViewControllerAccountInfoViewController 处理。

我的AccountInfoViewController看起来像这样:

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

@interface AccountInfoViewController : UIViewController {

    IBOutlet UITextField *acctName;
    IBOutlet UITextField *acctBalance;
    Account *acct;
}

@property (nonatomic, retain) UITextField *acctName;
@property (nonatomic, retain) UITextField *acctBalance;
@property (nonatomic, retain) Account *acct;

-(void) saveAccountInfo;

@end

在我的RootViewController中我正在这样做:

- (void)editAcctInfo:(Account *)acct {
    if (self.acctInfoViewController == nil) {
        AccountInfoViewController *a = [[AccountInfoViewController alloc]
                                        initWithNibName:@"AccountInfoViewController" 
                                        bundle:[NSBundle mainBundle]];
        self.acctInfoViewController = a;
        [a release];
    }

   if (acct != nil ) {
       self.acctInfoViewController.acct = acct;
   }

    //Hide Toolbar
    [toolbar removeFromSuperview];
    [self.navigationController pushViewController:self.acctInfoViewController 
                                         animated:YES];
}

这是我的AccountInfoViewController的viewDidLoad方法:

- (void)viewDidLoad
{
    UIBarButtonItem *saveButton = [[UIBarButtonItem alloc] initWithTitle:@"Save" 
                                                                   style:UIBarButtonItemStylePlain
                                                                  target:self 
                                                                  action:@selector(saveAccountInfo:)];
    acctBalance.text = acct.balance.accessibilityValue;
    acctName.text = acct.name.accessibilityValue;
    //acctName.text = @"Test Text";

    self.title =@"Edit Account";
    self.navigationItem.rightBarButtonItem = saveButton;
    [super viewDidLoad];
}

当我运行此命令时,我的视图的 UITextFields 中什么也没有得到。如果我用“测试文本”取消注释该行,在我看来这看起来很好,所以我知道我有一部分是正确的。 调试时可以看到acct下的值。当我实际上将鼠标悬停在代码中的单词 acct 上时,我得到所有值的“无效摘要”。

知道我做错了什么或者是否有更好的方法来实现这一点?我知道这一定是我在 AccountInfoViewController 中设置“acct”属性的问题。

这就是我调用上面 editAcctInfo 方法的方式:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    Account *account = [accountTableData objectAtIndex:indexPath.row];

    [self editAcctInfo:account];

}

I have two views in my application handled by my RootViewController and AccountInfoViewController.

My AccountInfoViewController looks like this:

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

@interface AccountInfoViewController : UIViewController {

    IBOutlet UITextField *acctName;
    IBOutlet UITextField *acctBalance;
    Account *acct;
}

@property (nonatomic, retain) UITextField *acctName;
@property (nonatomic, retain) UITextField *acctBalance;
@property (nonatomic, retain) Account *acct;

-(void) saveAccountInfo;

@end

In my RootViewController I am doing this:

- (void)editAcctInfo:(Account *)acct {
    if (self.acctInfoViewController == nil) {
        AccountInfoViewController *a = [[AccountInfoViewController alloc]
                                        initWithNibName:@"AccountInfoViewController" 
                                        bundle:[NSBundle mainBundle]];
        self.acctInfoViewController = a;
        [a release];
    }

   if (acct != nil ) {
       self.acctInfoViewController.acct = acct;
   }

    //Hide Toolbar
    [toolbar removeFromSuperview];
    [self.navigationController pushViewController:self.acctInfoViewController 
                                         animated:YES];
}

Here is my viewDidLoad method of my AccountInfoViewController:

- (void)viewDidLoad
{
    UIBarButtonItem *saveButton = [[UIBarButtonItem alloc] initWithTitle:@"Save" 
                                                                   style:UIBarButtonItemStylePlain
                                                                  target:self 
                                                                  action:@selector(saveAccountInfo:)];
    acctBalance.text = acct.balance.accessibilityValue;
    acctName.text = acct.name.accessibilityValue;
    //acctName.text = @"Test Text";

    self.title =@"Edit Account";
    self.navigationItem.rightBarButtonItem = saveButton;
    [super viewDidLoad];
}

When I run this, I get nothing in my UITextFields of my view. If I uncomment the line with the "Test Text", that appears fine in my view so I know I have part of it right.
When debugging, I can see the values under acct. When I actually hover over the word acct in the code, I get "Invalid Summary" for all the values.

Any idea what I'm doing wrong or if there is a better way to accomplish this? I know it must be a problem with how I am setting the "acct" property in my AccountInfoViewController.

This is how I call the above editAcctInfo method:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    Account *account = [accountTableData objectAtIndex:indexPath.row];

    [self editAcctInfo:account];

}

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

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

发布评论

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

评论(1

美人迟暮 2024-11-02 19:02:36

我看不到你在哪里分配和初始化你的帐户。您仅在所显示的代码中声明了它。它是否在您的 RootViewController 中分配?正在

if (acct != nil ) {
   self.acctInfoViewController.acct = acct;
}

接到电话吗?

I can't see where you alloc and init your account. You have only declared it in the code you're showing. Is it allocated in your RootViewController? is

if (acct != nil ) {
   self.acctInfoViewController.acct = acct;
}

getting called?

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