使 UIAlertView Button 触发功能 On Press
目前,我正在使用以下代码来呈现 UIAlertView:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Today's Entry Complete"
message:@"Press OK to submit your data!"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles: nil];
[alert show];
[alert release];
如何获取它,以便当按下“确定”时,它会触发一个函数,例如 -(void)submitData
Currently I am using the following code to present a UIAlertView:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Today's Entry Complete"
message:@"Press OK to submit your data!"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles: nil];
[alert show];
[alert release];
How do I get it so that when 'OK" is pressed, it triggers a function, say -(void)submitData
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
注意:
重要: UIAlertView 在 iOS 8 中已被弃用。(请注意,UIAlertViewDelegate 也已被弃用。)要在 iOS 8 及更高版本中创建和管理警报,请改为将 UIAlertController 与UIAlertControllerStyleAlert 的首选样式。
请查看此教程
“已弃用”是什么意思???
.h 文件
.m 文件
Swifty 方式是使用新的 UIAlertController 和闭包:
NOTE:
Important: UIAlertView is deprecated in iOS 8. (Note that UIAlertViewDelegate is also deprecated.) To create and manage alerts in iOS 8 and later, instead use UIAlertController with a preferredStyle of UIAlertControllerStyleAlert.
Please check this out tutorial
"deprecated" means???
.h file
.m file
The Swifty way is to use the new UIAlertController and closures:
如果您使用未在类接口中声明的多个 UIAlertView 实例,您还可以设置一个标记来标识委托方法中的实例,例如:
在类文件 myClass.m 顶部的某个位置
创建 UIAlertView:
委托方法:
If you are using multiple UIAlertView instances that are not declared in the class's interface you can also set a tag to identify instances in your delegate method, for example:
somewhere on top of your class file myClass.m
creating the UIAlertView:
the delegate method:
您需要在分配alertview时设置委托,然后使用UIAlertViewDelegate方法之一来调用您自己的方法,例如:
You need to set the delegate when allocating the alertview, then use one of the UIAlertViewDelegate methods to call your own method, for example:
在显示之前,您需要为
UIAlertView
设置delegate
。然后在委托回调中进行如下工作:我的 CWUIKit 项目位于 https://github.com/Jayway/CWUIKit 对
UIAlertView
进行了补充,允许您使用块执行相同的操作。重复使用相同的操作来创建、显示和处理警报:You need to setup the
delegate
for yourUIAlertView
, before showing it. Then do the work in the delegate callback as such:My CWUIKit project over at https://github.com/Jayway/CWUIKit has an addition to
UIAlertView
that allow you to do the same thing but with blocks. Redusing the same operation for both creating, showing and handling the alert to this:如果你想使用块也可以使用 MKAdditions 来实现即使对于多个 UIAlertView,这也很容易。
只需使用与此示例类似的代码:
您可以在本教程中找到更多信息:http://blog.mugunthkumar.com/coding/ios-code-block-based-uialertview-and-uiactionsheet
If you want to use blocks you can also use MKAdditions to achieve this easily even for multiple UIAlertViews.
Just use a code similar to this sample:
You can find more information in this tutorial: http://blog.mugunthkumar.com/coding/ios-code-block-based-uialertview-and-uiactionsheet
更多说明,
示例
(这是 iOS7 屏幕截图)
Little more clarification,
Example,
(it's iOS7 screenshot)
从 iOS8 Apple 提供新的
UIAlertController
类,您可以使用它来代替现已弃用的 UIAlertView,它也在折旧消息中说明所以你应该使用这样的东西
Swifty 方式是使用新的 UIAlertController 和闭包:
From iOS8 Apple provide new
UIAlertController
class which you can use instead of UIAlertView which is now deprecated, its is also stated in depreciation messageSo you should use something like this
The Swifty way is to use the new UIAlertController and closures: