iphone – 如何在表格视图中选择后将数据传递到详细信息视图?
发布时间:2020-12-14 18:13:47 所属栏目:百科 来源:网络整理
导读:我有一个UITableViewController,其中包含从名为userList的NSMutableArray填充的数据.选择特定用户后,它将转到详细视图,并更新NavBar中的标题,但它不会传递数据以更新UIView中的UILabel. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath
我有一个UITableViewController,其中包含从名为userList的NSMutableArray填充的数据.选择特定用户后,它将转到详细视图,并更新NavBar中的标题,但它不会传递数据以更新UIView中的UILabel.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [tableView deselectRowAtIndexPath:indexPath animated:NO]; //Attempt at a singleton to pass the data DetailView *details = [[DetailView alloc] init]; details.userNameLbl = [userList objectAtIndex:indexPath.row]; DetailView *detailVC = [[DetailView alloc] initWithNibName:nil bundle:nil]; //This line doesn't pass the data detailVC.userNameLbl.text = [userList objectAtIndex:indexPath.row]; //This line does update the title in the NavBar detailVC.navigationItem.title = [userList objectAtIndex:indexPath.row]; [self.navigationController pushViewController:detailVC animated:YES]; [details release]; [detailVC release]; } 我应该尝试将数据从表视图推送到详细视图,还是让详细视图尝试从表视图中提取数据? DetailView.h实际上是在IB中连接的以下行. IBOutlet UILabel *userNameLbl 解决方法
在从nib文件加载视图之前,不会实例化userNamelbl.这不会在初始化时立即发生,但会在调用viewDidLoad时发生.
因此,您应该在DetailView中声明一个属性来存储您的标题,然后将该值分配给viewDidLoad方法中的userNamelbl.text. 例如,在你的表viewController中: DetailView *detailVC = [[DetailView alloc] initWithNibName:nil bundle:nil]; detailVC.userName = [userList objectAtIndex: indexPath.row]; 并在您的详细视图中控制: - (void) viewDidLoad { [super viewDidLoad]; self.userNameLbl.text = self.userName; } viewController的navigationItem属性是在初始化viewController时创建的,因此您可以立即分配给navigationItem.title. SWIFT代码 let detailVC = DetailView(nibName: nil,bundle: nil) detailVC.userName = userList.objectAtIndex(indexPath.row) as? NSString 和 class DetailView: UIViewController { @IBOutlet var userNameLbl: UILabel var userName:NSString? override func viewDidLoad() { super.viewDidLoad() self.userNameLbl.text = self.userName } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- 【Cocos2d入门教程三】HelloWorld之一目了然
- ruby-on-rails – omniauth-facebook cannnot获取电子邮件地
- SQlite数据库的C编程接口(四) 绑定参数(Bound Parameter
- react-native – 使用GraphRequestManager的promise
- ruby-on-rails – rspec`with`必须至少有一个参数.升级到3.
- C++ 将文件数据一次性加载进内存实例代码
- 详解Oracle自定义异常示例
- cocos2d-x 3.3 之黑白棋设计 NO.6 移植Android
- xml约束和DTD校验
- Ruby to Actionscript3字节码