gpt4 book ai didi

ios - 如何将另一个 ViewController 设置为 AlertView 的委托(delegate)

转载 作者:行者123 更新时间:2023-11-29 12:58:39 26 4
gpt4 key购买 nike

我想知道是否可以将 Alert View 委托(delegate)设置为其他 ViewController。原因是因为我打算根据用户按下的警报 View 按钮执行某个操作。

这是我正在尝试做的:-

1] 声明<UIAlertViewDelegate>在我需要实现 alertView 委托(delegate)方法的 View controller.h 文件中。

2]这是在 AppDelegate 中声明我的 alertView 的方式。

 - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
if ([[UIApplication sharedApplication] applicationState] == UIApplicationStateActive)
{
alertView = [[UIAlertView alloc]initWithTitle:@"New Message" message:@"Click switch to check out" delegate:rosterListController cancelButtonTitle:@"OK" otherButtonTitles:@"Switch ", nil];
[alertView show];
}
}

3] 现在我想实现 alertView 委托(delegate)方法 clickedButtonAtIndex在另一个 View Controller 中,但它不会被调用。

 - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0)
{
NSLog(@"Cancel button pressed");
}
}

最佳答案

您应该使 AppDelegate 成为 AlertView Delegate,因为它是呈现 Alter View 的 App Delegate。使用随机的不相关的 View Controller 是没有意义的。如果您无论如何都强烈希望这样做,请考虑您的设计和应用程序逻辑有问题。

或者,您可以创建一个新类,它成为委托(delegate)。此类的唯一目的是处理警报 View 事件,并可能在演示者和警报 View 之间传回数据和强制。您可以定义一个协议(protocol),它定义了演示者必须满足的最低 API,以便与委托(delegate)处理程序进行通信。

对于呈现此警报 View 的每个演示者,都有一个委托(delegate)对象的 ivar。当你呈现Alert View时,无论是哪个presenter,创建并初始化delegate对象并将其设置为delegate。当警报 View 将被关闭时,释放委托(delegate)对象。

请注意,委托(delegate)对象可能需要对演示者的引用,以便处理关闭事件。

关于ios - 如何将另一个 ViewController 设置为 AlertView 的委托(delegate),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20344524/

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