Objective c 单击UIAlertview上的按钮后崩溃

Objective c 单击UIAlertview上的按钮后崩溃,objective-c,xcode,class,delegates,uialertview,Objective C,Xcode,Class,Delegates,Uialertview,我有一个在名为FAClass的类中显示Alertview的方法 // FAClass.H #import <Foundation/Foundation.h> @interface FAClass : NSObject<NSURLConnectionDataDelegate>{ } // FAClass.M @implementation FAClass{ } - (void)ShowAlert{ UIAlertView *FACUpdateAle

我有一个在名为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);
}
如果uialertview委托自身,则当单击alertview按钮时,alertview显示但应用程序崩溃;如果委托等于NULL,则不会发生任何情况 我需要在self 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.
如果要将此警报视图delgate方法用于其他警报视图委派方法,请在FACViewController中写入此警报视图delgate方法

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

 }

我知道这是一个非常古老的线程,但仍在尝试,希望它能帮助一些人谁有相同的问题

不要在view Dod load方法中调用警报视图,而是尝试在view Dod Aspect方法中调用它

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

检查这一点的一个更简单的方法是在您的NSObject类(在本例中为FAClass)中添加dealloc方法

- (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>
-(无效)显示警报

// 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
//FAClass.M
@实现类{
}    
-(无效)显示警报{
UIAlertView*FACUpdateAlert=[[UIAlertView alloc]initWithTitle:无消息:@“消息”委托:自取消按钮:@“取消”其他按钮:@“确定”,无];
[FacUpdateAllert show];
}
-(无效)alertView:(UIAlertView*)alertView单击按钮索引:(NSInteger)按钮索引
{
NSLog(@“%i”,按钮索引);
}
//////////////////
#进口
#进口
@界面FACViewController:UIViewController
{
}
@属性(强,非原子)FAClass*back;
///////////////
#导入“FACViewController.h”
@接口FACViewController()
@结束
@FACViewController的实现
-(无效)viewDidLoad
{
self.back=[[FAClass alloc]init];
[self.back ShowAlert];
[超级视图下载];
}
使用此代码非常有效

我在我的项目中发现了同样的问题,所以我用
UIAlertViewController
控制器替换了
uiAlertViewView
,因为
UIAlertview
在iOS 9中被弃用,在iOS 8之后的一段时间它工作不正常,所以请尝试用
UIAlertViewController
替换。我想你不是在写代理方法在你们班上。如果您不需要委托方法,只需UIAlertView*FACUpdateAlert=[[UIAlertView alloc]initWithTitle:Nil消息:@“消息”委托:Nil cancelButtonTitle:@“取消”其他ButtonTitles:@“确定”,Nil];[FacUpdateAllert show];像这样写你还没有写过alertView委托方法吗?你有没有实现任何UIAlertViewDelegate?试着放
Nil
=
Nil
你试过我的代码片段吗。
}
// 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