[英]How to pass data from 2nd viewcontroller to tableviewcontroller using delegates
I have 2 viewcontroller
s - one is a tableview
and the other is a normal viewcontroller
. 我有2个
viewcontroller
s-一个是tableview
,另一个是普通的viewcontroller
。 I want pass data from the second viewcontroller
to a tableview
controller by using delegates. 我想通过使用委托将数据从第二个
viewcontroller
传递到tableview
控制器。 I have created a delegate and delegatemethod
in viewcontroller
and implemented delegatemethod
in the tableview
controller. 我已经在
viewcontroller
创建了一个委托和delegatemethod
,并在tableview
控制器中实现了delegatemethod
。 The problem is that I am getting data to the array but tableview
is not reloading. 问题是我正在将数据获取到数组,但
tableview
没有重新加载。 Why? 为什么?
Can anyone help with this problem? 任何人都可以帮助解决这个问题吗? Thanks in advance.
提前致谢。
#import "TableViewController.h"
@interface TableViewController ()<name>{
NSMutableArray *data;
}
@end
@implementation TableViewController
- (void)viewDidLoad {
[super viewDidLoad];
data = [NSMutableArray array];
[self.tableView reloadData];
}
- (IBAction)callingSecondView:(id)sender {
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
ViewController *var = [storyboard instantiateViewControllerWithIdentifier:@"vc"];
var.delegate = self;
[self.navigationController pushViewController:var animated:YES];
}
-(void)getdata:(NSString *)name{
[data addObject:name];
[self.tableView reloadData];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [data count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
if (cell != nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
}
cell.textLabel.text = [data objectAtIndex:indexPath.row];
return cell;
}
And I am creating delegate a object and protocol in ViewController.h
我正在
ViewController.h
创建委托对象和协议
#import <UIKit/UIKit.h>
@protocol name <NSObject>
-(void)getdata : (NSString *)name;
@end
@interface ViewController : UIViewController
@property (strong, nonatomic) IBOutlet UITextField *txt;
- (IBAction)done:(id)sender;
@property(nonatomic,retain) id<name> delegate;
ViewController.m
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize delegate;
- (void)viewDidLoad {
[super viewDidLoad];
}
- (IBAction)done:(id)sender {
[delegate getdata:self.txt.text];
[self.navigationController popToRootViewControllerAnimated:YES];
}
@end
I think the first VC don't reloadData because it's not the visible VC. 我认为第一个VC不要reloadData,因为它不是可见的VC。 Try reloadData when the VC willAppear.
当VC出现时,尝试reloadData。
-(void) viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self.tableView reloadData];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.