i have method for showing Alertview in class named FAClass
// 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);
}
when try to call this method in another class
//FACViewController.M
#import "FACViewController.h"
#import "FAClass.h"
@interface FACViewController ()
@end
@implementation FACViewController
- (void)viewDidLoad
{
FAClass *Myclass = [[FAClass alloc]init];
[Myclass ShowAlert];
[super viewDidLoad];
}
the alertview shows but app crashing when clicking alertview buttons if uialertview delegate self else if delegate equal to NULL nothing happen i need to make uialertview delegate in self FAClass
You need to implement UIAlertView
delegate method in class where you are using alertView
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
Check alertView delegate methods here
initWithTitle:Nil
导致崩溃,更改为initWithTitle:@""
add one parameter to pass the current instance of view controller that is using your alert view.
- (void)ShowAlert:(id)usingInstance{
UIAlertView *FACUpdateAlert = [[UIAlertView alloc]initWithTitle:Nil message:@"Message" delegate:usingInstance cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
[FACUpdateAlert show];
}
now call the method:
- (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.
write this alert view delgate method in FACViewController if you want to use and same for other alertview delegate method.
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
}
I know its a very old thread but still giving it a shot, hope that it helps some one who has the same issue
Rather than calling the alert view in the view did load method try calling it in the view did appear method
The reason to this is that when alert view is called in the view did load the alert view delegate is not retained and hence your app crashes.
An easier way to check this is to add the dealloc method in the NSObject class of yours in this case FAClass
- (void)dealloc{
NSLog(@"I am leaving the house");
}
Hence its preferred to display the alert view only when the screen / view is fully loaded to avoid crashes since even if you call in view will appear or view did appear method your app would still crash.
Second way to resolve this is to have a strong property in your view controller
@property (nonatomic,strong) FAClass *popUp;
and then inside button's click event or view did load /appear of the viewcontroller.m file
if (self.popUp==nil) {
self.popUp = [FAClass new];
}
[self.popUp displayAuthPopUp];
Since EXC_BAD_ACCESS states that it is a memory issue, You should try to set the FAClass variable as iVar in FACViewController class as follows:
//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];
}
try and see if it works.
#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
替换。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.