Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/104.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 如何为UIAlertView中的按钮编写事件处理程序?_Objective C_Ios_Uialertview - Fatal编程技术网

Objective c 如何为UIAlertView中的按钮编写事件处理程序?

Objective c 如何为UIAlertView中的按钮编写事件处理程序?,objective-c,ios,uialertview,Objective C,Ios,Uialertview,假设我在obj c中有如下警告视图 UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"title" message:@"szMsg" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:@"download"]; [alert show]; [alert release]; 现在,警报视图上有两个按钮(确定和下载),如何为下载按钮编写事件处理

假设我在obj c中有如下警告视图

UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"title" message:@"szMsg" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:@"download"];
        [alert show];
        [alert release];

现在,警报视图上有两个按钮(确定和下载),如何为下载按钮编写事件处理程序?

首先,您需要将UIAlertViewDelegate添加到头文件中,如下所示:

头文件(.h)


执行
UIAlertViewDelegate
,并使用delegate方法

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
  if(buttonIndex == 0) {
    // Do something
  }
  else {
   // Do something
  }
}

现在,大多数iOS设备都有支持块的firmare版本,使用笨拙的回调API来处理按钮按下是一种不合时宜的做法。块是要走的路,请参见以下示例:


将您的UIAlertView声明为已知

UIAlertView *alertLogout=[[UIAlertView alloc]initWithTitle:@"Title" message:@"Stop Application?" delegate:self cancelButtonTitle:@"No" otherButtonTitles:@"Yes",nil]; 

[alertLogout show]; 

[alertLogout release];
将委托设置为self并实现此方法

-(void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    if(actionSheet== alertLogout) {//alertLogout
        if (buttonIndex == 0){

        }else if(buttonIndex==1){

        }
    }else if (actionSheet==alertComment) {//alertComment
        if (buttonIndex==0) {

        }
    }
}

Stack和Guillermo Ortega的答案可能是你会在几个UIAlertView中使用的,但不是十个。我经常使用和Lambda一样的东西,这是灵魂所建议的。这也是一个很好的选择,尽管如果嵌套块太多,您将开始看到它的缺点(除了您将依赖于另一个库之外)

处理多个内容的通常方法是使用一个handler对象。(
@interface MyAlertViewDelegate:NSObject@end
)使该对象成为警报视图的委托,并确保该对象至少在警报视图解除之前处于活动状态。 这当然会起作用,但可能是太多的工作

接下来是我的想法;依我看,它更简单,不需要任何第三方库,也不需要每个UIAlertView的ivar。只需一个额外的对象(
@property(nonatomic,strong)NSArray*modalaactions
)来存储当前UIAlertView将导致执行的操作

显示UIAlertView并作出相应反应 委托方法:

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    if (alertView.cancelButtonIndex != buttonIndex) {
        [self performModalActionAtIndex:buttonIndex];
    }
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (actionSheet.cancelButtonIndex != buttonIndex) {
        [self performModalActionAtIndex:buttonIndex];
    }
}
实际执行动作的部分:

- (void)performModalActionAtIndex:(NSInteger)index
{
    if (-1 < index && index < self.modalActions.count &&
        [self.modalActions[index] isKindOfClass:[NSString class]]) {
        SEL action = NSSelectorFromString(self.modalActions[index]);
        NSLog(@"action: %@", self.modalActions[index]);
        if ([self respondsToSelector:action]) {
// There is a situation with performSelector: in ARC.
// http://stackoverflow.com/questions/7017281/
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
        [self performSelector:action];
#pragma clang diagnostic pop
    }
    self.modalActions = nil;
}
委托方法:

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    if (alertView.cancelButtonIndex != buttonIndex) {
        [self performModalActionAtIndex:buttonIndex];
    }
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (actionSheet.cancelButtonIndex != buttonIndex) {
        [self performModalActionAtIndex:buttonIndex];
    }
}
为什么会这样: 这是因为两个原因:

首先,我从不提供同时具有委托的两个UIAlertView。(在我看来你不应该,它看起来不太好)。其次,因为在我的情况下(90%的情况下),操作的目标始终是相同的对象(在本例中:
self
)。即使您不满足上述条件,您甚至可以通过一些修改使用此方法:

  • 如果同时显示两个或多个UIAlerView或UIActionSheet(可能在iPad中),请使用字典来存储与特定UIAlertView/UIActionSheet关联的一组操作

  • 如果操作的目标不是
    self
    ,则需要在数组中存储对(目标和操作)。(模拟UIButtons的东西
    addTarget:action:…

无论哪种情况,在swift中存储目标和/或UIActionSheet/UIAlertView
[NSValue valueWithNonretainedObject:
都应该很方便:)

: 我们可以使用这个小代码块

    let alert = UIAlertController(title: "Alert", message: "This is an alert message", preferredStyle: UIAlertControllerStyle.Alert)

    let action = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: {(action:UIAlertAction) in print("This is in alert block")
    })

    alert.addAction(action)
    self.presentViewController(alert, animated: true, completion: nil)

您可以使用alertview的委托方法。-(void)alertView:(UIAlertView*)alertView单击按钮索引:(NSInteger)按钮索引{if(buttonIndex==0)//如果(buttonIndex==1)确定按钮操作//下载按钮操作}要将此方法称为set delegate of ur alertview to self。假设在.m文件中,我有10个uialerts和每个按钮中的两个按钮,那么将使用上述方法处理哪个警报?无论何时创建UIAlertView,都需要为其设置委托,即使用self或nil。如果在委托中使用self,则它会调用此方法;如果在委托中使用nil,则它不会调用此方法。很好,假设我有2个uialerts,并且在这两种情况下都将委托设置为self,第一个uialert包含(确定和下载)按钮,第二个包含(取消和上载)按钮现在我们需要单独的事件处理程序知道吗?如果您有两个以上的UIAlertView,并且所有视图都具有delegate=self。。然后,在这种情况下,为每个alertview分配一个单独的“标记”。根据这个标签(如果(alertView.tag==urTagname))你需要什么呢?现在它已经被弃用了,现在终于有了。所以现在不需要委托了?
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    if (alertView.cancelButtonIndex != buttonIndex) {
        [self performModalActionAtIndex:buttonIndex];
    }
}
- (void)performModalActionAtIndex:(NSInteger)index
{
    if (-1 < index && index < self.modalActions.count &&
        [self.modalActions[index] isKindOfClass:[NSString class]]) {
        SEL action = NSSelectorFromString(self.modalActions[index]);
        NSLog(@"action: %@", self.modalActions[index]);
        if ([self respondsToSelector:action]) {
// There is a situation with performSelector: in ARC.
// http://stackoverflow.com/questions/7017281/
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
        [self performSelector:action];
#pragma clang diagnostic pop
    }
    self.modalActions = nil;
}
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:title
                                                         delegate:self
                                                cancelButtonTitle:cancelButton  
                                           destructiveButtonTitle:nil 
                                                otherButtonTitles:button1, button2, button3, nil];
// Similarly, add one action per button at the proper index
self.modalActions = @[
                    NSStringFromSelector(@selector(actionForActionSheetButton1)),
                    NSStringFromSelector(@selector(actionForActionSheetButton2)),
                    NSStringFromSelector(@selector(actionForActionSheetButton3))];
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (actionSheet.cancelButtonIndex != buttonIndex) {
        [self performModalActionAtIndex:buttonIndex];
    }
}
    let alert = UIAlertController(title: "Alert", message: "This is an alert message", preferredStyle: UIAlertControllerStyle.Alert)

    let action = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: {(action:UIAlertAction) in print("This is in alert block")
    })

    alert.addAction(action)
    self.presentViewController(alert, animated: true, completion: nil)