gpt4 book ai didi

ios - UIAlertview 和 Segue 推送信息

转载 作者:行者123 更新时间:2023-11-29 03:06:10 25 4
gpt4 key购买 nike

所以用户需要从 UIAlertView 向 UITextField 输入一些东西。然后通过单击确定按钮,他将进入包含信息的下一个 View 。

但是我好像卡住了?

我有这个:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex == 1) {
UIStoryboardSegue *segue = [[UIStoryboardSegue alloc] init];

AddViewController *add = [segue destinationViewController];

UITextField *textfield = [alertView textFieldAtIndex: 0];

[add set_aantalDice:textfield.text.intValue];

[self performSegueWithIdentifier:@"addSegue" sender:self];

}else{
NSLog(@"Cancel");
}
}

但这行不通。我如何通过 segue 和 UIAlertView 获取信息槽?我已经尝试了 3 个小时了......

亲切的问候

最佳答案

虽然 Jonathan 对您做错的所有其他事情进行了详尽的解释,但问题要小得多。您的 UIAlertView 被关闭,当 segue 发生时,数据不再存在。您自己创建的 AddViewController 只是一个局部变量,根本不会在 segue 中使用,因此您设置的属性将被丢弃。

如果你想使用 segue,你需要按照 Apple 的方式来做。这意味着通过覆盖 prepareForSegue 方法来提供数据。

所以你需要做的是:

  1. 在当前 View Controller 中创建一个属性,该属性会将字符串存储在 UIAlertView 上的 UITextField 中。

    @property (nonatomic) NSInteger aantalDice;
  2. 使用以下代码将文本存储在 alertView:clickedButtonAtIndex: 中(应删除所有其他代码):

    UITextField *textfield = [alertView textFieldAtIndex:0];

    [self setAantalDice:textfield.text.intValue];

    [self performSegueWithIdentifier:@"addSegue" sender:self];
  3. 实现 prepareForSegue: 方法。

    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
    {
    if ([segue identifier] isEqualToString:@"addSegue"])
    {
    AddViewController * addViewController = [segue destinationViewController];

    [addViewController set_aantalDice:self.aantalDice];
    }
    }

数据现在应该在 AddViewController 中。

这是因为 performSegueWithIdentifier 方法会处理 UIViewController 创建本身,所以您不必自己实例化新的 View Controller 。

关于ios - UIAlertview 和 Segue 推送信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22745551/

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