将数据从Dynamic tableView传递到Static TableView

时间:2013-11-30 14:04:52

标签: iphone objective-c uitableview uistoryboardsegue

您好,我试着澄清一下我的问题。我有两个TableView,一个是静态的,另一个是动态的。 static = RootVC和Dynamic = FirstVC。在FirstVC中,我有我想要选择的数据,保存并将保存的数据传递给RootVC中的UILabel。 1)当我运行我的应用程序时,数据被选中,但它没有被保存或传递给我的rooVC。我正在使用代表,并建议不要使用“委托”,而是使用“块”。但我仍然面临同样的问题。这是我的代码:

in rootVC.h
#import <UIKit/UIKit.h>

@interface RootViewController : UITableViewController
{

    NSString *getRepeatLabel;
}
@property (strong, nonatomic) IBOutlet UILabel *repeatLabel;
@property (strong, nonatomic) IBOutlet UILabel *repeatDetail;
@property (nonatomic,strong) NSString *getRepeatLabel;
@end

in my rootVC.m

#import "RootViewController.h"

@interface RootViewController ()

@end

@implementation RootViewController
- (void)viewDidLoad
{
    [super viewDidLoad];

    _repeatLabel.text = @"Repeat";
    _repeatDetail.text = getRepeatLabel;


}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

}


- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    UIViewController *destinationController = segue.destinationViewController;
    if( [destinationController isKindOfClass:[FirstViewController class]] )
    {
        [(FirstViewController *)destinationController setCompletionBlock:^(NSString *getRepeatLabel;)
         {

             // do something here with your string // maybe you must reload your table // it depends on where your returning data needs to display    <--------Not sure what to do here
             // NSDateFormatter*dateFormatter = [[NSDateFormatter alloc]init];
             //    NSArray*days = [dateFormatter shortWeekdaySymbols];  <------Here I would like when data is selected to show days in short symbol
             NSLog (@"The selected day/s is %@", getRepeatLabel); <---nothing displaying on console




         }];
    }
}

@end

in FirstVC.h

#import <UIKit/UIKit.h>
#import "RootViewController.h"
typedef void(^WeekdayCompletionBlock)(NSString *dayName);
@interface FirstViewController : UITableViewController
{
    NSString *dayName;
}


@property (nonatomic, strong) WeekdayCompletionBlock completionBlock;
@property (nonatomic,strong) NSString *dayName;
- (IBAction)save:(id)sender;
@end

in FirstVC.m

#import "FirstViewController.h"
#import "RootViewController.h"
@interface FirstViewController ()
@end

@implementation FirstViewController
@synthesize completionBlock;
@synthesize dayName;


- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    // Initialize table data
    completionBlock = [NSArray arrayWithObjects:@"Sunday", @"Monday", @"Tuesday", @"Wednesday", @"Thursday", @"Friday", @"Saturday", nil];


}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations

    return YES;
}


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


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"RepeatCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier];


    }


    cell.textLabel.text = [completionBlock objectAtIndex:indexPath.row];

    return cell;
}




// Called after the user changes the selection.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSLog (@"The selected day/s is %@", [completionBlock objectAtIndex:indexPath.row]);


   _getRepeatLabel = completionBlock; //<-----------string from RootVC gives error "undeclared _getRepeatLabel"
}
- (IBAction)save:(id)sender
{
    NSUserDefaults *myNewWeekString = [NSUserDefaults standardUserDefaults];
    [myNewWeekString setObject:completionBlock forKey:@"%@"];
    [myNewWeekString synchronize];
    self.completionBlock(myNewDayOfWeekString) <------error myNewDayOfWeekString undeclared and if i declare it here it complains about incompatibility


}
@end

1 个答案:

答案 0 :(得分:0)

你的代码有点不对劲。我觉得你真的不懂块。 您希望传递的不仅仅是字符串,因此最好的方法是通过数组。将块定义更改为接受数组而不是字符串:

typedef void(^WeekdayCompletionBlock)(NSArray *dayName);

将FirstVC.h中的财产声明更改为:

@property (nonatomic, copy) NSArray *completionBlock; //This is your array, you use it as data source, It's not a block
//Add your block property
// This is your block property you will use it to pass the data between view controllers
@property (copy) WeekdayCompletionBlock returnBlock;
//Add property to keep your selected days
@property (nonatomic, strong) NSMutableArray *returnArray;

将此行添加到viewDidLoad方法:

self.returnArray = [[NSMutableArray alloc] init];

将FirstVC.m中的didSelectRowAtIndexPath方法更改为:

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

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if (cell.accessoryType == UITableViewCellAccessoryCheckmark)
    {
        cell.accessoryType = UITableViewCellAccessoryNone;
        //remove data from array
        [self.returnArray removeObject:[completionBlock objectAtIndex:indexPath.row]];
    }
    else
    {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
        //add data to array
        [self.returnArray addObject:[completionBlock objectAtIndex:indexPath.row]];
    }

   [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

在save:方法调用块中并将值传递给rootVC替换行:

self.completionBlock(myNewDayOfWeekString);

使用:

    if (self.returnBlock)
    {
        self.returnBlock(self.returnArray);
    }
    [self.navigationController popViewControllerAnimated:YES];

要做的最后一个更改是在rootVC.m文件中设置ablok。替换行:

[(FirstViewController *)destinationController setCompletionBlock:

with(nsstring需要用nsarray替换 - 你传递带有所有选定数据的数组)

[(FirstViewController *)destinationController setCompletionBlock:^(NSArray *getRepeatLabel)

您设置了阻止而不是NSArray。

我不知道你在这里做什么:

[myNewWeekString setObject:completionBlock forKey:@"%@"];

您正在使用%@作为密钥。它应该是文本,例如@“MY_KEY_FOR_ACCESING_DAYSOFWEEK”。您正在保存completionBlock,如果您想保存选定日期,请将其替换为self.returnArray。

希望得到这个帮助。