【问题标题】:Getting Data from an Array in a Previous View Controller, Put it in Another TableViewController从前一个 View Controller 中的数组中获取数据,将其放入另一个 TableViewController
【发布时间】:2012-11-11 18:12:32
【问题描述】:

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

非常感谢任何帮助。

谢谢,

德鲁夫

【问题讨论】:

    标签: nsmutablearray uitableview xcode4.3


    【解决方案1】:

    在您更新 RecordsVC.runsArray 时,我想您的 tableview 已经初始化并加载,尽管没有数据。

    我最近在这种情况下所做的就是使用通知。

    在您的 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 类中的通知观察。在您的视图WillDisappear 中,添加以下行:

    [[NSNotifcation defaultCenter] removeObserver:self name:@"runsArrayLoaded" object:nil];
    

    【讨论】:

      猜你喜欢
      • 2018-05-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-19
      • 2015-07-15
      相关资源
      最近更新 更多