繁体   English   中英

单击UIAlertview上的按钮后崩溃

[英]crash after clicking the button on a UIAlertview

我有在名为FAClass的类中显示Alertview的方法

// FAClass.H
#import <Foundation/Foundation.h>

@interface FAClass : NSObject<NSURLConnectionDataDelegate>{

}
// FAClass.M
@implementation FAClass{

}    
- (void)ShowAlert{

    UIAlertView  *FACUpdateAlert = [[UIAlertView alloc]initWithTitle:Nil message:@"Message" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
    [FACUpdateAlert show];

}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    NSLog(@"%i",buttonIndex);
}

当试图在另一个类中调用此方法时

//FACViewController.M
#import "FACViewController.h"
#import "FAClass.h"
@interface FACViewController ()

@end

@implementation FACViewController

- (void)viewDidLoad
{
    FAClass *Myclass = [[FAClass alloc]init];
    [Myclass ShowAlert];

    [super viewDidLoad];
}

alertview显示但是当点击alertview按钮时app崩溃如果uialertview委托自己,如果委托等于NULL没有任何反应我需要在自我FAClass中制作uialertview委托

在此输入图像描述

在此输入图像描述

在此输入图像描述

您需要在使用alertView的类中实现UIAlertView委托方法

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex

在这里检查alertView委托方法

initWithTitle:Nil导致崩溃,更改为initWithTitle:@""

添加一个参数以传递使用警报视图的视图控制器的当前实例。

- (void)ShowAlert:(id)usingInstance{

    UIAlertView  *FACUpdateAlert = [[UIAlertView alloc]initWithTitle:Nil message:@"Message" delegate:usingInstance cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
    [FACUpdateAlert show];

}

现在调用方法:

- (void)viewDidLoad
{
    FAClass *Myclass = [[FAClass alloc]init];
    [Myclass ShowAlert:self]; //here self is the current instance of FACViewController. so alertview will know that alert view is shown to FACViewController and hence it will not search the delegate method in FAClass that is what it is doing as per your code

    [super viewDidLoad];
}

// now you can use alertview delegate method here in FACViewController if you want or dont. it will work for both cases.

如果要对其他alertview委托方法使用和相同,请在FACViewController中编写此警报视图delgate方法。

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
 {

 }

我知道它是一个非常古老的线程,但仍然给它一个机会,希望它能帮助一个有同样问题的人

而不是在视图中调用警报视图确实加载方法尝试在视图中调用它确实出现方法

这样做的原因是,在视图中调用警报视图时,加载警报视图委托不会保留,因此您的应用程序崩溃。

检查这个的一个更简单的方法是在你的NSObject类中添加dealloc方法,在本例中为FAClass

- (void)dealloc{
 NSLog(@"I am leaving the house");
}

因此,它首选仅在屏幕/视图完全加载时显示警报视图以避免崩溃,因为即使您在视图中调用也会出现或查看确实显示您的应用程序仍会崩溃的方法。

解决此问题的第二种方法是在视图控制器中拥有强大的属性

@property (nonatomic,strong) FAClass *popUp;

然后在按钮的单击事件或视图中加载/显示viewcontroller.m文件

    if (self.popUp==nil) {
    self.popUp = [FAClass new];
}

[self.popUp displayAuthPopUp];

由于EXC_BAD_ACCESS声明它是内存问题,您应该尝试在FACViewController类中将FAClass变量设置为iVar,如下所示:

//FACViewController.M
#import "FACViewController.h"
#import "FAClass.h"
@interface FACViewController ()
{
      FAClass *Myclass
}
@end

@implementation FACViewController

- (void)viewDidLoad
{
     Myclass = [[FAClass alloc] initWithTitle:Nil message:@"Message" delegate:usingInstance cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
    [Myclass ShowAlert];

    [super viewDidLoad];
}

试着看看它是否有效。

#import <Foundation/Foundation.h>

@interface FAClass : NSObject<UIAlertviewdelegate>

{

}

- (void)ShowAlert;

// FAClass.M
@implementation FAClass{

}    
- (void)ShowAlert{

    UIAlertView  *FACUpdateAlert = [[UIAlertView alloc]initWithTitle:Nil message:@"Message" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
    [FACUpdateAlert show];

}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSLog(@"%i",buttonIndex);
}

//////////////////

#import <UIKit/UIKit.h>
#import <FAClass.h>


@interface FACViewController : UIViewController
{

}
@property(strong, nonatomic)FAClass *back;

///////////////


#import "FACViewController.h"

@interface FACViewController ()

@end

@implementation FACViewController

- (void)viewDidLoad
{
    self.back = [[FAClass alloc]init];
    [self.back ShowAlert];

    [super viewDidLoad];
}


use this code works perfectly

我在我的项目中发现了同样的问题所以我用UIAlertViewController控制器取代了UIAlertview ,因为UIAlertView在iOS 9中被弃用,而在iOS 8之后的一段时间它不能正常工作所以请尝试用UIAlertViewController替换。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM