![](/img/trans.png)
[英]How to pass data(dictionary) from Objective-C Controller into Swift `viewcontroller` while using bridging-header? Is there any other way?
[英]How to pass data values to other ViewController Objective-C using NSDictionary?
我正在看一本书中的教程,一切都很好,直到我不得不从tableView接收数据为止,我必须提到我修改了一些基本教程以适应我的需要,因为我使用的是来自API的JSON值。 当我使用self引用值并说变量为nil时,它崩溃。 还必须提及标签已添加为参考出口,并且customClass
也已创建并添加了View引用。 除此之外,还可以将Segue正确地添加到detailViewController
UserDetailViewController.h
@interface UserDetailViewController : UIViewController
@property (nonatomic, strong) NSString *labelName;
@property (nonatomic, strong) IBOutlet UILabel *detailLabelName;
@end
UserDetailViewController.m
这是在Table和detailView
之间进行转换的代码,但是有些值不属于我拥有的代码,例如recipeName
和recipeNames
,如下所示:
recipeName
在我的代码上labelName
recipeNames
在我的代码上字典[dictionary objectForKey:@"name"]
而且不知道如何将这部分放在一起。 :(
//这是我处理数据传输的地方
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"userDetailView"])
{
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
RecipeDetailViewController *destViewController = segue.destinationViewController;
destViewController.recipeName = [recipeNames objectAtIndex:indexPath.row];
}
}
根据我的理解,您想根据labelName为detailLbelName
设置文本。 这个值来自您的父视图控制器。
您必须执行以下操作:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UserDetailViewController *detailController = [[UserDetailViewController alloc]initWithNibName:@"UserDetailViewController" bundle:nil];
self.detailController.lableName = @"Your Text According to Cell"
[self.navigationController pushViewController:detailController animated:YES];
}
在您的UserDetailViewController
:
- (void)viewWillAppear
{
self.title = self.labelName;
[self.detailLabelName setText:self.labelName];
}
要从tableView接收数据,需要处理tableView委托,它确实使用select方法并将所有必需的数据传递给detailLabelName,然后刷新屏幕。
将viewDidLoad中的赋值行移动到viewWillAppear。
- (void)viewWillAppear
{
self.title = self.labelName;
_detailLabelName.text = _labelName; // this is the breakpoint
}
然后,无论您将字符串存储在表格视图中(我假设您是专门指单元格),然后可以在创建详细信息视图控制器之后但将其推入导航堆栈并崩溃之前,分配从表格视图单元格提取的值您的应用。
请尝试这个
在
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
您的表格视图中执行此操作。
UserDetailViewController *detailsView = [[UserDetailViewController alloc] initWithNibName:"Your nib name" bundle:[NSBundle mainBundle]];
UITableViewCell *reqDcell = [tableView cellForRowAtIndexPath:indexPath];
detailsView.labelName = //Text from reqDcell goes here. In particular the recipe name
[self.navigationController pushViewController:tableViewControls animated:YES];
希望这可以帮助。
**********编辑************
好的,为了使用字典传递值,您将需要一个字典数组来保存数据。
就您发布的链接而言,我已对其进行了修改,以供您理解以使用字典。
recipeNames = @[[NSDictionary dictionaryWithObject:@"Mushroom" forKey:@"Recipie Name"],
[NSDictionary dictionaryWithObject:@"Hamburger" forKey:@"Recipie Name"],
[NSDictionary dictionaryWithObject:@"Pasta" forKey:@"Recipie Name"],
[NSDictionary dictionaryWithObject:@"Pizza" forKey:@"Recipie Name"],
[NSDictionary dictionaryWithObject:@"Noodles" forKey:@"Recipie Name"]];
这里的字典将保存您拥有的json数据。
在- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
// Display recipe in the table cell
NSDictionary *dict = [recipeNames objectAtIndex:indexPath.row];
cell.nameLabel.text = [dict valueForKey:@"Recipie Name"];
和prepareForSegue函数。
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"showRecipeDetail"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
RecipeDetailViewController *destViewController = segue.destinationViewController;
NSDictionary *dict = [recipeNames objectAtIndex:indexPath.row];
destViewController.recipeName = [dict valueForKey:@"Recipie Name"];
}
}
我希望这是有帮助的
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.