0

I have a simple TableView, when the cell is selected I want that the content is passed to several fields in a detail view so that they can be changed and saved again in the table.

我的代码没有问题,没有错误,并且在调试时完美地设置了所有变量,但我的字段仍然为空。我尝试使用数组,尝试使用字符串,但无法治愈。

我正在使用来自 Java 的 Objective-C 迈出第一步,有人可以吗?

这是我的代码:

在我的 TableViewController.m 中:

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

static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (!cell){
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}

SNMGps *selectedCell = [arrayOfCell objectAtIndex:indexPath.row];
NSLog(@"%d - '%@ - '%@'- '%@'- '%@'", selectedCell.gpsID, selectedCell.name,selectedCell.userName,selectedCell.psw, selectedCell.notes);

SNMAddViewController *field = [[SNMAddViewController alloc]init];


//[field setGpsID1:selectedCell.gpsID];
[field setName1:selectedCell.name];
[field setUserName1:selectedCell.userName];
[field setPsw1:selectedCell.psw];
[field setNotes1:selectedCell.notes];

[field setFields];
}

NSLOG 显示完美的数据。

在我的 DetailViewController.h

#import <UIKit/UIKit.h>
#import "sqlite3.h"
#import "SNMGps.h"

@interface SNMAddViewController : UIViewController
@property(strong, nonatomic) NSString *name1;
@property(strong, nonatomic) NSString *userName1;
@property(strong, nonatomic) NSString *psw1;
@property(strong, nonatomic) NSString *notes1;
//@property(strong, nonatomic) NSString *gpsID1;




@property (strong, nonatomic) IBOutlet UITextField *nameText;
@property (strong, nonatomic) IBOutlet UITextField *userNameText;
@property (strong, nonatomic) IBOutlet UITextField *pswText;
@property (strong, nonatomic) IBOutlet UITextField *notesText;




- (IBAction)saveEntry:(id)sender;
- (void) setFields;
@end

在我的 DetailViewController.m

#import "SNMAddViewController.h"


@interface SNMAddViewController ()

@end

@implementation SNMAddViewController
//@synthesize nameText, userNameText, pswText, notesText, g;

NSMutableArray *arrayOfCell;
NSString *dbPathString;
sqlite3 *gpsDB;

@synthesize name1, userName1, psw1, notes1;
@synthesize nameText, userNameText, pswText, notesText;

- (void) setFields
{

nameText.text =  [NSString stringWithFormat:@"%@",name1];
userNameText.text = [NSString stringWithFormat:@"%@",userName1];
pswText.text = [NSString stringWithFormat:@"%@",psw1];
notesText.text = [NSString stringWithFormat:@"%@",notes1];
}

当我调试变量 name1、userName1 等时设置为正确的数据,但字段 nameText.text 仍然为零。

4

1 回答 1

0

而不是设置变量,只需在方法中传递参数,然后按如下方式分配它

所以在 DetailViewController.h 文件中更改您的 setFields 方法如下:

- (void) setFields:(NSString *)name :(NSString *)userName :(NSString *)psw :(NSString *)notes;

并在您的 DetailViewController.m 文件中更改方法如下

- (void) setFields:(NSString *)name1 :(NSString *)userName1 :(NSString *)psw1 :(NSString *)notes1
{

    nameText.text =  [NSString stringWithFormat:@"%@",name1];
    userNameText.text = [NSString stringWithFormat:@"%@",userName1];
    pswText.text = [NSString stringWithFormat:@"%@",psw1];
    notesText.text = [NSString stringWithFormat:@"%@",notes1];
}

并从 TableViewController.m 中调用它

[field setFields:selectedCell.name :selectedCell.userName :selectedCell.psw :selectedCell.notes];

试试这个,让我知道它是否有帮助。

NOTE:
Most important thing is that you never navigate to the SNMAddViewController.. so please navigate to it as per your need
于 2013-10-03T09:11:33.457 回答