在视图控制器和 Table 视图控制器之间传递数据
Passing Data between view Controller and a Table View Controller
我正在尝试将字符串值 '_passedDataDate'
从 viewController
传递到 TableView Controller
。在 TableViewController
中,我试图将它保存在一个数组中,即 '_dateArray'
。在尝试这样做时,首先插入一个 nil
对象,然后插入真实对象。如何避免添加这个零值?
下面是代码,
- (void)viewDidLoad {
[super viewDidLoad];
_dateArray = [[NSMutableArray alloc] init];
_dateString = _passedDataDate;
if(_dateString){
[_dateArray addObject:_passedDataDate];
NSLog(@"Added Passed Data");
NSLog(@"%ld",[_dateArray count]);
}
else{
NSLog(@"No object Added");
NSLog(@"%ld",[_dateArray count]);
}
}
输出如下:
没有添加对象
0
添加通过的数据
1
为什么打印 No object Added ?我不要它怎么办?
此外,我收到以下警告,
警告:尝试展示其视图不在 window 层次结构中!
不要在按钮操作中使用 Segue。
隐藏转场
[self performSegueWithIdentifier:@"pass" sender:nil];
然后试试这个:
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString: @"pass"])
{
TableViewController *tableView = [segue destinationViewController];
tableView.passedDataDate = @"HAI";tableView.passedDataDestination = @"Hellio";
}
}
我正在尝试将字符串值 '_passedDataDate'
从 viewController
传递到 TableView Controller
。在 TableViewController
中,我试图将它保存在一个数组中,即 '_dateArray'
。在尝试这样做时,首先插入一个 nil
对象,然后插入真实对象。如何避免添加这个零值?
下面是代码,
- (void)viewDidLoad {
[super viewDidLoad];
_dateArray = [[NSMutableArray alloc] init];
_dateString = _passedDataDate;
if(_dateString){
[_dateArray addObject:_passedDataDate];
NSLog(@"Added Passed Data");
NSLog(@"%ld",[_dateArray count]);
}
else{
NSLog(@"No object Added");
NSLog(@"%ld",[_dateArray count]);
}
}
输出如下:
没有添加对象 0 添加通过的数据 1
为什么打印 No object Added ?我不要它怎么办?
此外,我收到以下警告,
警告:尝试展示其视图不在 window 层次结构中!
不要在按钮操作中使用 Segue。
隐藏转场
[self performSegueWithIdentifier:@"pass" sender:nil];
然后试试这个:
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString: @"pass"])
{
TableViewController *tableView = [segue destinationViewController];
tableView.passedDataDate = @"HAI";tableView.passedDataDestination = @"Hellio";
}
}