Ios UIAlertView在实用程序类中删除

Ios UIAlertView在实用程序类中删除,ios,objective-c,uialertview,Ios,Objective C,Uialertview,在我的应用程序中,我需要在许多视图中使用alertview。因此,我所做的只是在实用程序类中编写一个alertview,并在任何地方使用它。这很好 我甚至尝试设置,但都没有成功 实用类 @interface SSUtility: NSObject<UIAlertViewDelegate> { } +(void)showAllert; @end @implementation SSUtility +(void)s

在我的应用程序中,我需要在许多视图中使用alertview。因此,我所做的只是在实用程序类中编写一个alertview,并在任何地方使用它。这很好

我甚至尝试设置
,但都没有成功

实用类

    @interface SSUtility: NSObject<UIAlertViewDelegate> {

    }

    +(void)showAllert;
    @end


    @implementation SSUtility    
         +(void)showAllert{
          UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"gotoappAppstore",@"") message:@"" delegate:self cancelButtonTitle:NSLocalizedString(@"Ok",@"") otherButtonTitles:nil];
          [alert show];
          [alert release];
        }
        @end

Now from my view

 -(void)pressButton{
[SSutility showAllert]

}
有人能帮我吗


提前感谢。

您在
UIAlertView
的init中设置了
delegate:nil

您应该设置为
delegate:self
,如下所示:

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"gotoappAppstore",@"") message:@"" delegate:self cancelButtonTitle:NSLocalizedString(@"Ok",@"") otherButtonTitles:nil];
以便在同一类中使用委托(又称为self)


作为旁注,如果您使用自动引用计数(ARC),您不需要
[警报发布]
(您的Xcode编译器应该就此向您发出警告)

通过将警报视图对象委托通常设置为所有者对象并实现–alertView:ClickedButtonIndex:方法,可以连接警报视图按钮响应方法

代码中需要4个部分:

  • 实例化UIAlertView对象
  • 向UIAlertView对象发送显示消息
  • 集合委托
  • 实现委托方法
  • 例如:

    UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:@"myTitle" message:@"myMessage" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitle:@"Another button"];
    [myAlertView setDelegate:self];
    [myAlertView show];
    
    
    - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
        {
        if (buttonIndex == 0) //index 0 is cancel, I believe
            {
            // code for handling cancel tap in your alert view
            }
        else if (buttonIndex == 1)
            {
            // code for handling button with index 1
            }
        }
    

    我建议您更熟悉学员的工作方式。这会再次出现。

    请共享所有相关代码,以设置您的
    UIAlertView
    设置。因此,基本上包括您的实用程序类,然后包括从其他类调用的这两个类(或另一个对象而非自身)?@Popeye,@pNre请检查我的代码。即使将代理设置为自身,结果也是一样的。不工作:(你是否设置了
    @接口实用性:NSObject
    ??@Raptor?我同意,但在他共享的代码中,它不在那里,我唯一能想到的是阻止它工作的是它丢失了。
    UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:@"myTitle" message:@"myMessage" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitle:@"Another button"];
    [myAlertView setDelegate:self];
    [myAlertView show];
    
    
    - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
        {
        if (buttonIndex == 0) //index 0 is cancel, I believe
            {
            // code for handling cancel tap in your alert view
            }
        else if (buttonIndex == 1)
            {
            // code for handling button with index 1
            }
        }