- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我是 Objective-C 的新手,我只是想弄清楚我是否可以使用 block 或选择器作为 UIAlertView 的 UIAlertViewDelegate 参数 - 哪个更合适?
我已经尝试了以下方法,但它不起作用,所以我不确定我是否在正确的轨道上?
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Checked In"
message:responseString
delegate:^(UIAlertView * alertView, NSInteger buttonIndex)
{
NSLog(@"Done!");
}
cancelButtonTitle:@"OK"
otherButtonTitles: nil];
Cảm ơn!
câu trả lời hay nhất
好主意。这里是。就像警报 View 一样,只是添加了一个 block 属性,当警报被解除时会调用该属性。 (编辑 - 自原始答案以来我已经简化了这段代码。这是我现在在项目中使用的代码)
// AlertView.h
//
#import
@interface AlertView : UIAlertView
@property (copy, nonatomic) void (^completion)(BOOL, NSInteger);
- (id)initWithTitle:(NSString *)title message:(NSString *)message cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSArray *)otherButtonTitles;
@kết thúc
//
// AlertView.m
#import "AlertView.h"
@interface AlertView ()
@kết thúc
@implementation AlertView
- (id)initWithTitle:(NSString *)title message:(NSString *)message cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSArray *)otherButtonTitles {
self = [self initWithTitle:title message:message delegate:self cancelButtonTitle:cancelButtonTitle otherButtonTitles:nil];
if (self) {
for (NSString *buttonTitle in otherButtonTitles) {
[self addButtonWithTitle:buttonTitle];
}
}
return self;
}
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
if (self.completion) {
self.completion(buttonIndex==self.cancelButtonIndex, buttonIndex);
self.completion = nil;
}
}
@kết thúc
您可以扩展这个想法,为其他委托(delegate)方法提供 block ,但最常见的是 didDismiss。
这样调用它:
AlertView *alert = [[AlertView alloc] initWithTitle:@"Really Delete" message:@"Do you really want to delete everything?" cancelButtonTitle:@"Nevermind" otherButtonTitles:@[@"Yes"]];
alert.completion = ^(BOOL cancelled, NSInteger buttonIndex) {
if (!cancelled) {
[self deleteEverything];
}
};
[hiển thị cảnh báo];
关于iphone - 阻止 UIAlertViewDelegate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10082383/
我是一名 Android 开发人员。 我想在我的代码中使用自定义 UIAlertViewDelegate。 类似演示 import UIKit class ViewController: UIView
我正在定义一个符合 UIAlertViewDelegate 的类: class PaymentAlert: NSObject, UIAlertViewDelegate { var orderI
我有一个非常简单的流程运行,在每一轮简单的游戏之后计算分数,更新标签和所有正常的,非常简单的东西。我有一个 UIAlertView 可以通知玩家他/她的表现。我使用 UIAlertViewDelega
我有实现 UIAlertViewDelegate 的 Controller 。在实现中我有: - (void)alertView:(UIAlertView *)alertView clickedBut
我是 Objective-C 的新手,我只是想弄清楚我是否可以使用 block 或选择器作为 UIAlertView 的 UIAlertViewDelegate 参数 - 哪个更合适? 我已经尝试了以
我正在尝试使用一种可选的委托(delegate)方法来扩展 UIAlertViewDelegate 协议(protocol)。 界面 @protocol HPAlertViewDelegate; @i
我正在使用 iPhone 的 Logo (MobileSubstrate 插件),并为我的 .h 文件 @interface MyClass : NSObject 和 - (void)alertVi
UIAlertViewDelegate 协议(protocol)定义了两个方法,alertView:clickedButtonAtIndex: 和 alertView:didDismissWithBu
在早期的 Objective-C 中,我们在 UIAlertView 中使用了 delegate:self,这样我们就可以在索引处调用类似 buttonclicked 的方法....但现在在 swif
我有一个从文件中读取的导入序列,解压缩包含的文件并为每个文件创建相应的核心数据实体。这整个过程发生在后台,并且已经为每个线程等创建了一个单独的上下文,所以一切正常。 事实证明,这个特定导入序列的一个理
几天来我一直在寻找这个问题的解决方案并找到了一些类似的帖子,但似乎没有人发布关于如何阻止我正在使用的实例获得 dealloc's 的明确答案.希望我只是错过了它,或者有人可以帮助我停止伤脑筋.. 我有
我在 ViewController 以外的类中遇到了 UIAlertView 委托(delegate)的问题。一切正常,直到用户单击 OK 按钮 - 然后应用程序崩溃并显示 Thread 1: EXC
小伙伴们: 在我的测试应用程序的 viewController 中有两个按钮,我称之为“否”的正确按钮, 另一个是"is"。两个按钮会调用两个不同的函数,而当 用户按下其中一个按钮,我想向用户显示一个
我们曾经在 ViewController.h 中使用 UIAlertViewDelegate 让我们的自定义警报按钮执行一个操作,比如在警报窗口中按下 Play Again 按钮使游戏重新开始。 我们
我添加了一个alertView来向用户显示警报消息(见下文) -(void)connectionAlert { UIAlertView *alert = [[UIAlertView alloc
我的应用程序中的多个 ViewController 必须显示相同的 UIAlertView 并以相同的方式执行 –alertView:clickedButtonAtIndex: 方法。我不想在多个文件
đóng cửa. Câu hỏi này không tuân thủ các nguyên tắc của Stack Overflow. Hiện tại nó không chấp nhận câu trả lời. Các câu hỏi yêu cầu mã phải thể hiện sự hiểu biết tối thiểu về vấn đề đang được giải quyết. Bao gồm các giải pháp đã thử, tại sao chúng không hiệu quả và trước
我开始学习 Objective-C uikit,我遇到了一个问题。没有调用 UIAlertViewDelegate。谁能告诉我为什么?谢谢你! #import #import @interface
我有一个助手类,我想显示一个 UIAlertView,然后将执行一些 Web 服务操作。我想这样做,这样我就可以在其他地方重新使用警报 View 。 我是这样声明的: @interface FBLin
在发生以下情况之前,我以为我终于设法理解了委托(delegate)的概念:我更改了头文件以删除对委托(delegate)的引用,并且 Alert 仍然有效。唯一的区别是我丢失了代码提示。 //.h #
Tôi là một lập trình viên xuất sắc, rất giỏi!