我想在两个UITableViewControllers之间传递数据。在第一的UITableViewController我显示NewPackageViewController其中包含像一些细胞在下面的代码:如何在两个UITableViewControllers之间传递数据
- (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;
在:
- (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的后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];
}
之后,我实现PassStoreNameDelegate在NewPackageViewController(在我的第一个的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
我想创建一个类似于单选按钮的功能...... 每一个帮助将appriciated
Ragards
I'don”第二视图控制器不知道这个单选按钮功能应该是这样的:/我没有提供参考,因为在我看来,它们显然必须在那里...... – sonoDamiano
单选按钮的真实名称是什么。我的意思是官方名称。 – DilumN