gpt4 book ai didi

ios 以编程方式推送 Controller View ,无需 Storyboard或 xib

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:16:35 26 4
gpt4 key购买 nike

在 ViewControllerA 中,我现在有一个包含三行数据的表(三个消息)。单击一行,我试图将其中一行的文本(名称)推送到 ViewControllerB。我遇到的问题是我正在尝试以编程方式执行所有操作,而不是使用 Storyboard ID 或 Storyboard segues。这可能吗?这是我的。

ViewControllerA m.

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
Messages *cell = (Messages *)[tableView cellForRowAtIndexPath:indexPath];
NSString *from = [NSString stringWithFormat:@"%@", cell.from.text];
ViewControllerB *VCB = [[ViewControllerB alloc]init];
VCB.from = from;
[self.navigationController pushViewController:VCB animated:YES];
//using this^ creates a new view controller with a black screen but passes the variable
//[self performSegueWithIdentifier:@"pushMessage" sender:self];
using this^ shows my ViewControllerB perfectly but the value isn't set
}

ViewControllerB h.

@property (strong, nonatomic) NSString *from;

ViewControllerB m.

-(void)viewDidLoad{
NSLog(@"%@", from);
}

有没有办法在不在 VCA 中启动新的 viewcontrollerB 并获得正确的推送动画的情况下执行此操作?

最佳答案

我相信我明白你在问什么,你几乎走在了正确的轨道上。您说当您使用 [self performSegueWithIdentifier:@"pushMessage"sender:self]; 时它会起作用,但未设置该值。

因此,在您的 header 中声明一个 NSString“from”,并在前面的代码中删除 [self.navigationController pushViewController:CVC animated:YES];

//ViewControllerA.h

@property (copy, nonatomic) NSString *from;



//ViewControllerA.m

@synthesize from;

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
Messages *cell = (Messages *)[tableView cellForRowAtIndexPath:indexPath];
from = [NSString stringWithFormat:@"%@", cell.from.text];

[self performSegueWithIdentifier:@"pushMessage" sender:self];
}

然后你需要使用prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender

 //ViewControllerA.m

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{

if ([[segue identifier] isEqualToString:@"pushMessage"]) {

ViewControllerB *vcb = [segue destinationViewController];

[vcb setFrom:[self from]];
}

}

编辑

嗯,我觉得这是一种非常错误的做法。但是,这将满足您在问题中提出的所有条件。我已经检查过,这确实有效:

首先,转到您的 Storyboard,单击 View Controller B,单击 identity inspector 并更改 Storyboard ID。同时单击“使用 Storyboard ID”的复选标记。

Identity Inspector

在你的 ViewControllerA 中:

//ViewControllerA.h

- (void)callViewControllerWithCellString:(NSString *)str;

//ViewControllerA.m

- (void)callViewControllerWithCellString:(NSString *)str
{
NSString * storyboardName = @"Main";

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle: nil];
SecondViewController * vc = [storyboard instantiateViewControllerWithIdentifier:@"ViewControllerB"];
[vc setFrom:str];

[[self navigationController] pushViewController:vc animated:YES];

}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
Messages *cell = (Messages *)[tableView cellForRowAtIndexPath:indexPath];
NSString *from = [NSString stringWithFormat:@"%@", cell.from.text];

[self callViewControllerWithCellString:from];
}

关于ios 以编程方式推送 Controller View ,无需 Storyboard或 xib,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22795239/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com