我有这样的情况,我需要在第一个视图控制器中单击一个按钮时发送文本字段值给第二个视图控制器,并在表格视图中显示,但在第二个视图控制器中没有显示任何内容
viewcontroller.h
@interface ViewController : UIViewController
@property (strong, nonatomic) IBOutlet UITextField *textfiled;
@end
viewcontroller.m
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
NextViewController*nxt=segue.destinationViewController;
nxt.sr=_textfiled.text;
}
NextviewController.h
@interface NextViewController : UIViewController
@property (strong,nonatomic)NSMutableArray*arr;
@property (strong, nonatomic) IBOutlet UITableView *table;
@property(strong,nonatomic) NSString*sr;
Nextviewcontroller.m
@interface NextViewController ()<UITableViewDataSource>
//@property(strong,nonatomic)NSMutableArray*finalarr;
@end
@implementation NextViewController
- (void)viewDidLoad {
[super viewDidLoad];
[_arr addObject:_sr];
[_table reloadData];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return _arr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell*cell=[tableView dequeueReusableCellWithIdentifier:@"cell"];
cell.textLabel.text=[_arr objectAtIndex:indexPath.row];
return cell;
}
发布于 2016-11-07 06:34:07
您可以使用nsuserdefaults在web中像cookie或session一样传递
发布于 2016-11-07 06:44:56
//The below should not be used in prepare segue.use in any button action.
if(!secondVC) {
secondVC = [UIStoryboard storyboardWithName:@"Main"
bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:@"SecondVCIdetifier"];
}
secondVC.tempString = tf.text;
[self presentViewController:secondVC animated:YES completion:nil];
发布于 2016-11-07 09:38:58
首先看一下[_arr addObject:_sr];
。查看_sr值是否为空。如果是,你的问题是在之前,如果不是,你的问题是在之后。
从那时起,您将能够准确地指出问题所在。从快速阅读来看,我会说你要么根本没有得到字符串数据,要么你没有使用segue,因此prepareforsegue没有被调用。
学习使用断点。如果你不知道怎么做,那就去查一查,这非常容易,而且它占了你工作的70%。
只要有了这个建议,你就能解决你的问题,还有更多的问题。
https://stackoverflow.com/questions/40459080
复制相似问题