0

私は単純な TableView を持っています。セルが選択されたときに、コンテンツを詳細ビューの複数のフィールドに渡して、それらを変更してテーブルに再度保存できるようにしたいと考えています。

私のコードには問題もエラーもありません。デバッグすると、すべての変数が完全に設定されますが、フィールドは空のままです。配列を試したり、文字列を試したりしましたが、治療法はありませんでした。

私は、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 は nil のままです。

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 に答える