从 2 个不同的源加载相同的 UIViewController 但结果不同

发布于 2024-12-29 05:23:15 字数 2087 浏览 0 评论 0原文

在我的应用程序中,我有两种不同的方法来访问同一个 UIViewController,其中一种方法是通过使用故事板创建的 2 个父 ViewController,这种方法可以完美地工作,只需通过每个 segue 转换传递和 id 即可。

另一种方式(这是我需要帮助的方式)直接从 RootViewController 上按下的按钮开始,它也只是将 id 传递给一个名为 Event 的自定义对象。每个转换的代码如下:

从 segue:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{
    NSIndexPath *path = [self.tableView indexPathForSelectedRow];
    Event *event = [nEventList objectAtIndex:path.row];
    EventInfo *eventInfo = segue.destinationViewController; //Create new ViewController
    eventInfo.eventID = event.id;
}

从按钮:

- (void)FeaturedPressedAction:(id)sender
{
    UIButton *button = (UIButton *)sender;
    int tag = button.tag;
    EventInfo *eventInfo = [[EventInfo alloc] init]; //Create new ViewController
    eventInfo.eventID = @"1"; //There is an event with id '1'
    [self.navigationController pushViewController:eventInfo animated:YES];
}

以及设置 ViewController 的代码:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.

    UIImage *image = [UIImage imageNamed:@"title.png"];
    self.navigationItem.titleView = [[UIImageView alloc] initWithImage:image];

    SingletonClass* myapp = [SingletonClass sharedInstance];
    events = [myapp getEvents];

    // Loop to filter out the events that Do not have this ID
    Event *event;
    BOOL found = false;
    for (int i = 0; i < [events count]; i++) {
        if(!found){
            event = [events objectAtIndex:i];
            if([event.id isEqualToString: eventID]){
                found = true;
                NSLog(@"Found the event with title %@", event.title);
            }
        }
    }

    self.eventTitle.text = event.title;
    self.eventLocation.text = event.location;
    self.eventDate.text = event.date;
    self.eventTime.text = @"22:00";
    self.eventDescription.text = event.details; 
}

它查找事件正如我所看到的,NSLog 留在那里,很好,只是按下按钮时没有任何视图加载。我哪里出错了?

In my application I have 2 separate ways to get to the same UIViewController, on way is to go through 2 parent ViewControllers created using the storyboard and this way works perfectly just passing and id through each segue transition.

The other way (Which is what I need help with) goes directly from a button pressed on the RootViewController, it also just passes the id to a customly defined object called Event. The code for each transition is below:

From the segue:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{
    NSIndexPath *path = [self.tableView indexPathForSelectedRow];
    Event *event = [nEventList objectAtIndex:path.row];
    EventInfo *eventInfo = segue.destinationViewController; //Create new ViewController
    eventInfo.eventID = event.id;
}

From the button:

- (void)FeaturedPressedAction:(id)sender
{
    UIButton *button = (UIButton *)sender;
    int tag = button.tag;
    EventInfo *eventInfo = [[EventInfo alloc] init]; //Create new ViewController
    eventInfo.eventID = @"1"; //There is an event with id '1'
    [self.navigationController pushViewController:eventInfo animated:YES];
}

And the code to set up the ViewController:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.

    UIImage *image = [UIImage imageNamed:@"title.png"];
    self.navigationItem.titleView = [[UIImageView alloc] initWithImage:image];

    SingletonClass* myapp = [SingletonClass sharedInstance];
    events = [myapp getEvents];

    // Loop to filter out the events that Do not have this ID
    Event *event;
    BOOL found = false;
    for (int i = 0; i < [events count]; i++) {
        if(!found){
            event = [events objectAtIndex:i];
            if([event.id isEqualToString: eventID]){
                found = true;
                NSLog(@"Found the event with title %@", event.title);
            }
        }
    }

    self.eventTitle.text = event.title;
    self.eventLocation.text = event.location;
    self.eventDate.text = event.date;
    self.eventTime.text = @"22:00";
    self.eventDescription.text = event.details; 
}

It finds the event fine as I can see with the NSLog's left in there, just none of the view loads when coming from the button press. Where am I going wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

憧憬巴黎街头的黎明 2025-01-05 05:23:15

您应该从情节提要中实例化它,而不是分配您的 EventInfo*

因此,在您的 FeaturedPressedAction 中,将初始化行替换为:

EventInfo *eventInfo = [self.storyboard instantiateViewControllerWithIdentifier:@"..."];

并确保您在 Interface Builder 中使用相同的标识符来标识 ViewController。将 ... 替换为有意义的东西。

Rather than alloc your EventInfo*, you should instantiate it from your storyboard instead.

So in your FeaturedPressedAction replace the initialisation line with:

EventInfo *eventInfo = [self.storyboard instantiateViewControllerWithIdentifier:@"..."];

And make sure you identify the ViewController in Interface Builder with the same identifier. Replace ... with something meaningful.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文