选择太复杂无法使用 UIAlertView 时如何正确获取用户的选择

     2023-03-13     275

关键词:

【中文标题】选择太复杂无法使用 UIAlertView 时如何正确获取用户的选择【英文标题】:How to get the user's choice properly when the choice is too complex to use UIAlertView 【发布时间】:2012-07-13 14:42:18 【问题描述】:

我已经为这个问题苦苦挣扎了一段时间,所以任何帮助都将不胜感激。

情况如下:我的应用程序有一个名为InitialViewControllerUIViewController 子类。此视图控制器有一个 UIButton,当按下该按钮时,它会创建一个名为 MyEngineNSObject 子类。像这样的:

@interface InitialViewController : UIViewController <MyEngineDelegate>
...
@end

@implementation InitialViewController
...
-(IBAction)pressedButton:(id)sender 
    MyEngine *engine = [[MyEngine alloc] init];
    [engine start];

start 中,我以模态方式呈现一个 ViewController (ConflictViewController) 以获取用户的选择:

@interface MyEngine : NSObject <ConflictViewControllerDelegate>
...
-(void) start;
@end

@implementation MyEngine
...
-(void) start 
        ConflictViewcontroller *cvc = [[ConflictViewController alloc] initWithNibName:@"ConflictViewController" bundle:nil];
        cvc.modalPresentationStyle = UIModalPresentationFormSheet;
        cvc.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
        cvc.delegate = self;
        UIWindow *window = [(MyAppDelegate *) [[UIApplication sharedApplication] delegate] window];
        [[window rootViewController] presentModalViewController:cvc animated:YES];

@end

ConflictViewController 真的很简单。它只是等待用户决定,当用户按下按钮时,它会将消息发送到delegate,然后自行关闭。

-(IBAction)didSelectConflict:(id)sender 
    UISegmentedControl *seg = (UISegmentedControl*) sender;
    [self.delegate didResolveConflictChoice:seg.selectedSegmentIndex];
    [self dismissModalViewControllerAnimated:YES];

我检查了每个连接,所有代表都正常工作。 出了什么问题: 当 MyEngine 在其实现 didSelectConflict: 中收到用户的选择时,它无法正常继续,因为它的所有属性都已消失 null

MyEngine 呈现ConflictViewController 时,程序继续执行,当start 完成时,它返回到pressedButton:,当此方法关闭时,MyEngine 对象被释放。

我想知道是否有办法解决这个问题?有没有人以另一种方式做过这样的事情? 这里的问题是:如何在选择太复杂时正确地将用户的选择正确使用UIAlertView

抱歉,这个问题太长了,我尽可能地简化了它。感谢您的宝贵时间,非常感谢您提供任何链接、cmets 或任何形式的帮助

【问题讨论】:

【参考方案1】:

为什么要在 IBAction 中初始化 MyEngine *engine,如果您希望使用 MyEngine 对象,为什么不在您的 InitialViewController 中进行全局声明,然后在 IBaction 中调用 [engine start]。然后,当委托方法返回所选索引时,您可以将其应用于初始视图控制器中的全局 int 并继续前进。希望这是有道理的

【讨论】:

确实如此。说的有道理哈哈哈。不过,我希望我知道一种更好的方法来处理这种情况【参考方案2】:

让你的方法开始为

-(void) startWithDelegate:(id)del 
        ConflictViewcontroller *cvc = [[ConflictViewController alloc] initWithNibName:@"ConflictViewController" bundle:nil];
        cvc.modalPresentationStyle = UIModalPresentationFormSheet;
        cvc.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
        cvc.delegate = del;
        UIWindow *window = [(MyAppDelegate *) [[UIApplication sharedApplication] delegate] window];
        [[window rootViewController] presentModalViewController:cvc animated:YES];

-(IBAction)pressedButton:(id)sender 
    MyEngine *engine = [[MyEngine alloc] init];
    [engine startWithDelegate:self];

InitialViewController 中实现didResolveConflictChoice: 并在那里获取代理调用。如果合适,您可以使用UIActionSheet。

【讨论】:

这个解决方案的问题是我需要MyEngine 的属性才能使用didResolveConflictChoice:。我将看看 UIActionSheet。谢谢你的回答 不幸的是UIActionSheet只是一组按钮,和UIAlertView一模一样 试试上面的方法。否则,您可以将表格视图放在警报视图中,如本教程starterstep.wordpress.com/2009/03/24/…

UIAlertView 无法正常工作

】UIAlertView无法正常工作【英文标题】:UIAlertViewnotfunctioningcorrectly【发布时间】:2013-04-0320:51:39【问题描述】:我在尝试关闭UIAlertView时遇到问题。UIAlertView正确启动,并显示两个按钮:“是”和“否”。但是,当我选择是按钮... 查看详情

ARC 和 UIAlertView:发送到实例的无法识别的选择器

】ARC和UIAlertView:发送到实例的无法识别的选择器【英文标题】:ARCandUIAlertView:unrecognizedselectorsenttoinstance【发布时间】:2012-01-2019:43:23【问题描述】:这是我如何显示UIAlertView和委托clickedButtonAtIndex-UIAlertView*alert=[[UIAlertViewalloc]ini... 查看详情

如何更改 UIAlertView 按钮的宽度?

】如何更改UIAlertView按钮的宽度?【英文标题】:HowtochangewidthofUIAlertViewbuttons?【发布时间】:2010-06-3016:28:46【问题描述】:当用户在我的iPhone应用程序中开始执行特定操作时,我使用UIAlertView显示带有两个按钮选项的帮助消息:... 查看详情

uialertview 中的图像...?

】uialertview中的图像...?【英文标题】:Imageinuialertview...?【发布时间】:2013-01-0711:35:02【问题描述】:我知道这是一个常见问题,即如何在uiAlertView中显示另一个控件而不是消息。我有一个应用程序在我的视图中有图像,当用户... 查看详情

如何在 iOS7 中将 UIDatePicker 添加到 UIALertView

】如何在iOS7中将UIDatePicker添加到UIALertView【英文标题】:HowtoAddaUIDatePickertoUIALertViewiniOS7【发布时间】:2013-09-2907:22:56【问题描述】:在我的一种观点中,我已经实现了datepicker,方法是将日期选择器添加到Alertview。现在在iOS7中,... 查看详情

在运行时确定浏览器是不是太慢而无法优雅地处理复杂的 JavaScript/CSS 的最佳方法是啥?

】在运行时确定浏览器是不是太慢而无法优雅地处理复杂的JavaScript/CSS的最佳方法是啥?【英文标题】:What\'sthebestwaytodetermineatruntimeifabrowseristooslowtogracefullyhandlecomplexJavaScript/CSS?在运行时确定浏览器是否太慢而无法优雅地处理复... 查看详情

iOS 5:如何使用 UIPickerView 创建 UIAlertView? (iPhone / iPad)

】iOS5:如何使用UIPickerView创建UIAlertView?(iPhone/iPad)【英文标题】:iOS5:HowcanIcreateaUIAlertViewwithaUIPickerView?(iPhone/iPad)【发布时间】:2012-01-1317:42:03【问题描述】:我需要一种允许用户从列表中选择选项的方法,我正在考虑在UIAlertVie... 查看详情

如何取消并从uialertview调用用户操作(代码片段)

...提示。此外,我们应该在iOS8+上使用UIAlertController而不是UIAlertView。 查看详情

获取 JSON 失败时的 UIAlertView [重复]

】获取JSON失败时的UIAlertView[重复]【英文标题】:UIAlertViewforwhenfetchingJSONfails[duplicate]【发布时间】:2015-06-2918:00:59【问题描述】:当获取JSON数据失败时,我试图让这个UIAlertView运行,但我似乎无法让它工作。如果有人能告诉我如... 查看详情

如何在 Swift 中创建 UIAlertView?

】如何在Swift中创建UIAlertView?【英文标题】:HowwouldIcreateaUIAlertViewinSwift?【发布时间】:2014-07-2405:17:31【问题描述】:我一直在努力在Swift中创建一个UIAlertView,但由于某种原因,我无法正确使用该语句,因为我收到了这个错误... 查看详情

如何防止多个 UIAlertView 堆叠?

】如何防止多个UIAlertView堆叠?【英文标题】:HowtopreventmultipleUIAlertViewfromstackingup?【发布时间】:2011-08-1721:08:00【问题描述】:我在iOS中使用MPMoviePlayerController。我正在收听播放视频时可能出现的任何错误。在我的错误处理程序... 查看详情

在警报视图中单击 UIButton 时如何关闭关闭 UIAlertView

】在警报视图中单击UIButton时如何关闭关闭UIAlertView【英文标题】:HowtoturnoffdismissUIAlertViewwhenclickUIButtoninalertview【发布时间】:2014-12-0808:11:12【问题描述】:单击UIAlertView中的按钮时关闭关闭UIAlertView的正确方法是什么。当单击按... 查看详情

如何让 UIAlertView 在 iPhone 应用程序第一次启动时只出现一次?

】如何让UIAlertView在iPhone应用程序第一次启动时只出现一次?【英文标题】:HowdoImakeaUIAlertViewappearonlyonce,atthefirststart-upofaniPhoneapp?【发布时间】:2010-04-0514:16:49【问题描述】:我正在使用UIAlertView在应用启动后弹出一个弹出窗口... 查看详情

文本选择突出显示太多

】文本选择突出显示太多【英文标题】:TextSelectionHighlightingtoomuch【发布时间】:2012-07-2702:03:44【问题描述】:由于填充,文本选择在任一侧突出显示过多。http://jsfiddle.net/JamesKyle/pA7BJ/如何使用CSS解决此问题?我尝试了很多不同... 查看详情

UIAlertView 和 UIAlertController

】UIAlertView和UIAlertController【英文标题】:UIAlertViewAndUIAlertController【发布时间】:2015-09-1415:45:26【问题描述】:我在xcode6中使用SWIFT来开发我的应用程序,并且需要支持适用于iphone4和更高版本的应用程序...所以选择了“部署目标... 查看详情

当应用程序关闭或在后台运行时弹出 UIAlertView 窗口

】当应用程序关闭或在后台运行时弹出UIAlertView窗口【英文标题】:UIAlertViewwindowpopupwhenappisclosedorrunninginbackground【发布时间】:2013-03-1302:29:07【问题描述】:我对UIAlertView做了一些研究,我了解它的基础知识。这是我的功能要求-... 查看详情

如何响应 UIImagePickerController 生成的 UIAlertView

】如何响应UIImagePickerController生成的UIAlertView【英文标题】:HowtorespondtoUIImagePickerControllerGeneratedUIAlertView【发布时间】:2013-07-0906:50:45【问题描述】:我正在使用UIImagePickerController从我的应用程序中捕获视频,并且我已将视频最长... 查看详情

当应用程序处于前台时收到推送通知时,如何防止 UIAlertView 出现?

】当应用程序处于前台时收到推送通知时,如何防止UIAlertView出现?【英文标题】:HowanIpreventtheUIAlertViewfromappearingwhenapushnotificationisreceivedwhiletheappisintheforeground?【发布时间】:2015-04-1202:57:25【问题描述】:vardata=alert:"Yourdriverisher... 查看详情