加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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
    }    
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读