Segue 将字符串从 MainViewController 传递到 DetailViewController:
主视图控制器.m
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:@"DetailSegue"]) {
NSIndexPath *selectedRowIndex = [self.tableView indexPathForSelectedRow];
DetailViewController *detailViewController = [segue destinationViewController];
//Name:
detailViewController.detailName = [[sortedObjects objectAtIndex:selectedRowIndex.row] valueForKey:@"Name"];
//Attempt with number:
detailViewController.detailPopularity = [[sortedObjects objectAtIndex:selectedRowIndex.row] valueForKey:@"Popularity"];
}
}
DetailViewController.h
//Name:
@property (nonatomic, strong) IBOutlet UILabel *nameLabel;
@property (nonatomic, strong) NSString *detailName;
//Attempt with number:
@property (nonatomic, strong) IBOutlet UILabel *popularityLabel;
@property (nonatomic, strong) NSString *detailPopularity;
细节视图控制器.m
- (void)viewDidLoad
{
[super viewDidLoad];
//Name:
nameLabel.text = detailName;
//Attempt with number:
popularityLabel.text = detailPopularity;
}
在此示例中,应用程序崩溃是因为它无法在 UILabel 中显示 NSNumber。我需要看一个关于如何将 NSNumber 转换为 NSString 的示例,以便我可以在标签中显示它,我尝试了不同的方式,但我找不到正确的组合来修复错误..