iphone – 如何在表格视图中选择后将数据传递到详细信息视图?

我有一个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
    }    
}

相关文章

在有效期内的苹果开发者账号(类型为个人或者公司账号)。还...
Appuploader官网--IOS ipa上传发布工具,证书制作工具跨平台...
苹果在9月13号凌晨(北京时间)发布 iOS 16,该系统的设备可...
计算机图形学--OpenGL递归实现光线追踪
Xcode 14打出来的包在低版本系统运行时会崩溃,报错信息是Li...