4

私は2つのTableViewControllerを持っています。ユーザーが UIBarButtonItem をタップすると、UITextField、runnameField からのテキストが SaveViewController に保存されます。runnameField のテキストを取り、それを配列 runsArray に入れる必要があります。RecordVC では、tableView のデータは runsArray から取得する必要があります。誰かがこれで私を助けてくれますか? 私は長い間グーグルで検索してきましたが、答えが見つかりません。

SaveViewController.h:

#import <UIKit/UIKit.h>
#import "RecordsViewController.h" //Next ViewController

@interface SaveViewController : UITableViewController <UITableViewDataSource,UITableViewDelegate> {
__weak IBOutlet UITextField *runnameField;
RecordsViewController *RecordsVCData;
}
@property (weak, nonatomic) IBOutlet UITextField *runnameField;
@property (weak, nonatomic) IBOutlet UITextView *runnotesTextView;
@property (weak, nonatomic) IBOutlet UIBarButtonItem *saveBarItem;
@property (nonatomic, retain) RecordsViewController *RecordsVCData;

- (IBAction)saverun:(UIBarButtonItem *)sender;
- (IBAction)goAwayKeyboard:(id)sender;



@end

SaveViewController.m:

- (IBAction)saverun:(UIBarButtonItem *)sender {
RecordsViewController *RecordsVC = [[RecordsViewController alloc] initWithNibName:nil bundle:nil];
RecordsVC.tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
self.RecordsVCData = RecordsVC;
[RecordsVC.runsArray addObject:[NSString stringWithFormat:@"%@", runnameField.text]];
[self presentModalViewController:RecordsVC animated:YES];
}

- (IBAction)goAwayKeyboard:(id)sender {
    [sender resignFirstResponder];
}

RecordsViewController.h:

#import <UIKit/UIKit.h>
@interface RecordsViewController : UITableViewController <UITableViewDataSource, UITableViewDelegate> {      
NSMutableArray *runsArray;
}
@property (weak, nonatomic) NSString *runname;
@property (strong, nonatomic) NSString *runnotes;
@property (weak, nonatomic) UITableViewCell *cell;
@property (retain,nonatomic) NSMutableArray *runsArray;
@end

RecordsViewController.m:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:     (NSIndexPath *)indexPath {

// Identifier for retrieving reusable cells.
static NSString *cellIdentifier = @"MyCellIdentifier";

// Attempt to request the reusable cell.

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault     reuseIdentifier:cellIdentifier];
// No cell available - create one
if(cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                  reuseIdentifier:cellIdentifier];
[cell.textLabel setText:[NSString stringWithFormat:@"%@",[runsArray objectAtIndex:indexPath.row]]];

}

// Set the text of the cell to the runnamestring.
[cell.textLabel setText:[NSString stringWithFormat:@"%@",[runsArray objectAtIndex:indexPath.row]]];
return cell;

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [runsArray count];
}

どんな助けでも大歓迎です。

ありがとう、

ドゥルヴ

4

1 に答える 1

1

RecordsVC.runsArray を更新している時点で、データはありませんが、テーブルビューは既に初期化されてロードされていると思います。

この状況で私が最近行ったことは、通知を使用することです。

RecordsViewController では、init メソッドで通知を登録します。

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {

        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(dataLoaded:) name:@"runsArrayLoaded" object:nil];

        // Custom initialization
    }
    return self;
}

また、RecordsViewController では、通知を受信したときに呼び出されるメソッドが必要になります。この場合、それは dataLoaded です:

-(void) dataLoaded:(NSNotification *)notif {
    if (notif.userInfo){
        runsArray = [notif.userInfo objectForKey:@"newdata"];
        [self.tableview reloadData];
    }
}

このすべての魔法を機能させる部分は、SaveViewController クラス内の saverun メソッドにあります。

- (IBAction)saverun:(UIBarButtonItem *)sender {
    RecordsViewController *RecordsVC = [[RecordsViewController alloc] initWithNibName:nil bundle:nil];
    RecordsVC.tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    [runsArray addObject:[NSString stringWithFormat:@"%@", runnameField.text]];
    [self presentModalViewController:RecordsVC animated:YES];

    // notify the RecordsViewController that new data is available
    NSMutableDictionary* userInfo = [[NSMutableDictionary alloc] initWithCapacity:1];
    [userInfo setObject:runsArray forKey:@"newdata"];
    [[NSNotificationCenter defaultCenter] postNotificationName:@runsArrayLoaded" object:nil userInfo:(NSDictionary*)userInfo];
}

最後に、RecordsViewController クラスの通知監視をクリーンアップして削除する必要があります。viewWillDisappear に、次の行を追加します。

[[NSNotifcation defaultCenter] removeObserver:self name:@"runsArrayLoaded" object:nil];
于 2012-11-21T13:41:21.793 に答える