我有一个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 } }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。