如何在两个UITableViewController之间传递数据

时间:2013-11-24 16:08:43

标签: ios iphone uitableview delegates radio-button

我想在两个UITableViewControllers之间传递数据。在第一个UITableViewController中,我显示了NewPackageViewController,其中包含一些像下面代码中的单元格:

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


static NSString *CellIdentifier = @"Cell";
UITableViewCell *aCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (indexPath.section ==0)
    {
        UITextField* textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 0, 310, 44)];
        [textField setPlaceholder:@"Set package number here"];
        [textField setEnablesReturnKeyAutomatically: YES];

        [textField setReturnKeyType:UIReturnKeyDone];
        [textField addTarget:self action:@selector(dismissKeyBoard:) forControlEvents:UIControlEventEditingDidEndOnExit];
        [textField setKeyboardType:UIKeyboardTypeNumbersAndPunctuation];
        [aCell addSubview:textField];
    }
        else if(indexPath.section ==1){
            aCell  =[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
            [aCell setSelectionStyle:UITableViewCellSelectionStyleNone];
            [aCell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
            aCell.textLabel.text = creationDate;
            aCell.detailTextLabel.text = @"Choose";
            aCell.imageView.image =  [UIImage imageNamed:@"Date.png"] ;



    }
    else if(indexPath.section ==2)
    {
          [aCell setSelectionStyle:UITableViewCellSelectionStyleNone];
        aCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
        aCell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        aCell.textLabel.text = self.storeName;


        aCell.imageView.image = [UIImage imageNamed:@"Buildings2020.png"];

    aCell.detailTextLabel.text = @"Choose";

    }
    else if(indexPath.section ==4){
        aCell =[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        aCell.textLabel.text = @"Add package item";
        [aCell.textLabel setTextColor:[UIColor orangeColor]];
        aCell.imageView.image = [UIImage imageNamed:@"New.png"];
        [aCell setSelectionStyle:UITableViewCellSelectionStyleNone];
        aCell.backgroundColor = [UIColor clearColor];
    }
    else if(indexPath.section ==5){
        UITextView *textView = [[UITextView alloc]initWithFrame:CGRectMake(10, 0, 300, 100)];
        [textView setEditable:YES];
        [textView setFont:[UIFont fontWithName:@"Verdana" size:14]];
        [textView setReturnKeyType:UIReturnKeyDone];

        textView.delegate = self;

         [aCell setSelectionStyle:UITableViewCellSelectionStyleNone];
        [aCell addSubview:textView];

    }



return aCell;

}

单击部分== 2我的应用程序正确显示第二个UITableView:

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

switch (indexPath.section)
{
    case ...:
break;
........
   case 2:
UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
UIViewController *vc = [storyBoard
instantiateViewControllerWithIdentifier:@"SelectStoreView"];
UINavigationController *navController = [[UINavigationController
alloc]initWithRootViewController:vc];
[self.navigationController presentViewController:navController animated:YES
completion:nil];
break; }
default:
break;

在SelectStoreViewController中,我实现了检查标记

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return arrayOfStores.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath
*)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier ];
if([self.checkedIndexPath isEqual:indexPath])
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
cell.textLabel.text= [arrayOfStores objectAtIndex:indexPath.row];
cell.imageView.image = [UIImage imageNamed:@"Buildings2020.png"];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath
*)indexPath
{
if(self.checkedIndexPath)
{
UITableViewCell* uncheckCell = [tableView
                                    cellForRowAtIndexPath:self.checkedIndexPath];
    uncheckCell.accessoryType = UITableViewCellAccessoryNone;
}
if([self.checkedIndexPath isEqual:indexPath])
{
    self.checkedIndexPath = nil;
}
else
{
    UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
    self.checkedIndexPath = indexPath;
}
}

在SelectStoreViewController中,我使用如下方法创建了委托:

SelectStoreViewController.h

@class SelectStoreViewController;
@protocol PassStoreNameDelegate <NSObject>
-(void)setStoreName:(SelectStoreViewController*)controller
didFinishEnteringStoreName:(NSString*)enteredStoreName;
@end
@interface SelectStoreViewController :
UITableViewController<UITableViewDataSource,UITableViewDelegate>{
}
@property (retain,nonatomic) NSIndexPath *checkedIndexPath;
@property (strong,nonatomic) NSMutableArray *arrayOfStores ;
@property (nonatomic,weak) id<PassStoreNameDelegate>delegate;
@end

SelectStoreViewController.m

- (void)viewDidLoad{
[super viewDidLoad];
arrayOfStores = [[NSMutableArray alloc]init];
[self fetchStoresFromDatabase];
self.title = @"Select store from list";
UIBarButtonItem *backButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"Back"
style:UIBarButtonItemStylePlain target:self
action:@selector(didSelectBackBarButtonItem:)];
self.navigationItem.leftBarButtonItem = backButtonItem;
}
-(void)didSelectBackBarButtonItem:(id)sender{
[self.delegate setStoreName:self didFinishEnteringStoreName:@"store name"];
[self dismissViewControllerAnimated:YES completion:nil];
}

之后我在NewPackageViewController中实现了PassStoreNameDelegate(在我的第一个UITableViewController中) NewPackageViewController.h

interface NewPackageViewController :
UITableViewController<UITextViewDelegate,PassStoreNameDelegate,CreationDateViewDelegate>
{
SelectStoreViewController *selectStoreNameViewController;
NSString *storeName;
}
@property (nonatomic, retain) SelectStoreViewController*selectStoreNameViewController;
@property (retain,nonatomic) NSString* storeName;
@end

NewPackageViewController.m

-(void)setStoreName:(SelectStoreViewController *)controllerdidFinishEnteringStoreName
(NSString *)enteredStoreName{
self.storeName = enteredStoreName;
[self.tableView reloadData];
}

尝试执行此任务时,我使用了以下链接:Passing Data between View Controllers

我想创建类似于单选按钮的功能...... 每一个帮助都会得到满足

Ragards

1 个答案:

答案 0 :(得分:0)

首先你应该在.h文件中创建单选按钮功能。然后在第一个视图控制器中实现您的代码。

你唯一想做的就是第二视图控制器.h文件导入“secondviewController.h”

然后你肯定可以在secondviewController.m文件中使用那个单选按钮功能