【发布时间】:2014-11-08 06:28:30
【问题描述】:
我为我的项目设计了自定义警报视图。 我使用@protocol 创建了一个单独的viewcontroller(.h 和.m)文件,以便代表获得alertview 按钮单击响应。我已经为视图控制器创建了一个对象(我使用自定义委托创建的自定义警报视图)。我在每个其他类中子查看自定义警报视图。它像正常的 UIAlertview 一样工作。
但是当我显示警报视图然后按主页按钮并返回到我的应用程序时会出现问题,它在 ios7 中冻结,在 ios6 中崩溃。
我应该怎么做才能解决这个问题。当我返回我的应用程序时,它(警报视图)应该正常工作。请提出您的想法来解决这个问题。
我无法找到苹果默认的 UIAlertview 在相同的情况下是如何工作的(dipaly alertview 并进入后台并出租到应用程序中,显示的 alertview 工作正常)。
我不使用 UIAlertvie
alert_cupboard=[[UIAlertView alloc]initWithTitle:@"Title" message:@"message" delegate:self cancelButtonTitle:nil otherButtonTitles:@"Yes",@"No", nil]; alert_cupboard.tag=0; [alert_cupboard show];
为此,我在我的代码中设计了用于创建自定义警报视图
CustomAlertView.h
@protocol AlertViewprotocal <NSObject>
@required
- (void)AlertSuccess:(id)result;
- (void)AlertFailure:(id)result;
@end
@interface CustomAlertView : UIViewController
{
UIView *view_alert;
UIImageView *img_alert_bg;
UIView *view_alert_popup;
UILabel *lbl_alert_title;
UILabel *lbl_alert_message;
UILabel *lbl_sep_horizontal;
UILabel *lbl_sep_vertical;
UIButton *btn_alert_OK;
UIButton *btn_alert_yes;
UIButton *btn_alert_no;
}
@property(nonatomic,weak)id<AlertViewprotocal> CustomAlertdelegate;
+(CustomAlertView *)singleton;
-(void)showAlertView:(UIViewController *)rootViewController:(NSInteger)option :(NSString *)message :(NSInteger)tag;
-(IBAction)action_alert_yes:(id)sender;
-(IBAction)action_alert_hide:(id)sender;
@end
CustomAlertView.m
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self)
{
// Custom initialization
float height=[UIScreen mainScreen].bounds.size.height;
float width=[UIScreen mainScreen].bounds.size.width;
view_alert=[[UIView alloc]initWithFrame:CGRectMake(0, 0,width , height)];
img_alert_bg=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0,width , height)];
[img_alert_bg setImage:[UIImage imageNamed:@"pop_up_bg.png"]];
[view_alert addSubview:img_alert_bg];
view_alert_popup=[[UIView alloc]initWithFrame:CGRectMake(IS_IPAD ? (234): 10, (height-156)/2, IS_IPAD ? (300):(width-(10*2)) , 156)];
[view_alert_popup setBackgroundColor:[UIColor whiteColor]];
view_alert_popup.layer.cornerRadius= 10.5; //IS_IPAD ?8.2:
lbl_alert_title=[[UILabel alloc]initWithFrame:CGRectMake(20, 5, 260, 35.0)];
[lbl_alert_title setTextColor:[UIColor blackColor]];
[lbl_alert_title setBackgroundColor:[UIColor clearColor]];
[lbl_alert_title setText:@"Title"];
lbl_alert_title.textAlignment = UITextAlignmentCenter;
lbl_alert_title.font = [UIFont fontWithName:@"Helvetica" size: 18.0]; //IS_IPAD ? 34.0 :
[view_alert_popup addSubview:lbl_alert_title];
lbl_alert_message=[[UILabel alloc]initWithFrame:CGRectMake(20, lbl_alert_title.frame.size.height, view_alert_popup.frame.size.width-(20*2), 70)];
[lbl_alert_message setTextColor:[UIColor blackColor]];
[lbl_alert_message setBackgroundColor:[UIColor clearColor]];
lbl_alert_message.font = [UIFont fontWithName:@"Helvetica" size: 16.0]; //IS_IPAD ? 34.0 :
lbl_alert_message.textAlignment = UITextAlignmentCenter;
lbl_alert_message.numberOfLines=3;
lbl_alert_message.lineBreakMode=UILineBreakModeWordWrap;
[view_alert_popup addSubview:lbl_alert_message];
lbl_sep_horizontal=[[UILabel alloc]initWithFrame:CGRectMake(0, (lbl_alert_message.frame.origin.y+lbl_alert_message.frame.size.height), view_alert_popup.frame.size.width, 1.0)];
[lbl_sep_horizontal setBackgroundColor:[UIColor lightGrayColor]];
[view_alert_popup addSubview:lbl_sep_horizontal];
lbl_sep_vertical=[[UILabel alloc]initWithFrame:CGRectMake(150, (lbl_alert_message.frame.origin.y+lbl_alert_message.frame.size.height), 1.0, 50.0)];
[lbl_sep_vertical setBackgroundColor:[UIColor lightGrayColor]];
[view_alert_popup addSubview:lbl_sep_vertical];
btn_alert_yes=[[UIButton alloc]initWithFrame:CGRectMake(0, (lbl_alert_message.frame.origin.y+lbl_alert_message.frame.size.height+10), 150, 30.0)];
[btn_alert_yes setBackgroundColor:[UIColor clearColor]];
[btn_alert_yes setTitleColor:[UIColor colorWithRed:(204.0/255.0) green:(50.0/255.0) blue:(100.0/255.0) alpha:1.0] forState:UIControlStateNormal];
[btn_alert_yes.titleLabel setFont:[UIFont fontWithName:@"Helvetica" size: 16.0]];
[btn_alert_yes setTitle:@"Yes" forState:UIControlStateNormal];
//[btn_alert_yes.titleLabel setText:@"Yes"];
[btn_alert_yes addTarget:self action:@selector(action_alert_yes:) forControlEvents:UIControlEventTouchUpInside];
[view_alert_popup addSubview:btn_alert_yes];
btn_alert_no=[[UIButton alloc]initWithFrame:CGRectMake(150, (lbl_alert_message.frame.origin.y+lbl_alert_message.frame.size.height+10), 150, 30.0)];
[btn_alert_no setBackgroundColor:[UIColor clearColor]];
[btn_alert_no setTitleColor:[UIColor colorWithRed:(204.0/255.0) green:(50.0/255.0) blue:(100.0/255.0) alpha:1.0] forState:UIControlStateNormal];
[btn_alert_no.titleLabel setFont:[UIFont fontWithName:@"Helvetica" size: 16.0]];
[btn_alert_no setTitle:@"No" forState:UIControlStateNormal];
//[btn_alert_no.titleLabel setText:@"No"];
[btn_alert_no addTarget:self action:@selector(action_alert_hide:) forControlEvents:UIControlEventTouchUpInside];
[view_alert_popup addSubview:btn_alert_no];
btn_alert_OK=[[UIButton alloc]initWithFrame:CGRectMake(0, (lbl_alert_message.frame.origin.y+lbl_alert_message.frame.size.height+10), 300, 30.0)];
[btn_alert_OK setBackgroundColor:[UIColor clearColor]];
[btn_alert_OK setTitleColor:[UIColor colorWithRed:(204.0/255.0) green:(50.0/255.0) blue:(100.0/255.0) alpha:1.0] forState:UIControlStateNormal];
[btn_alert_OK.titleLabel setFont:[UIFont fontWithName:@"Helvetica" size: 16.0]];
[btn_alert_OK setTitle:@"OK" forState:UIControlStateNormal];
//[btn_alert_OK.titleLabel setText:@"OK"];
[btn_alert_OK addTarget:self action:@selector(action_alert_hide:) forControlEvents:UIControlEventTouchUpInside];
[view_alert_popup addSubview:btn_alert_OK];
[view_alert addSubview:view_alert_popup];
[self.view addSubview:view_alert];
}
return self;
}
+(CustomAlertView *)singleton
{
if (shared)
{
shared=nil;
}
shared = [[CustomAlertView alloc] init];
return shared;
}
-(void)showAlertView:(UIViewController *)rootViewController:(NSInteger)option :(NSString *)message :(NSInteger)tag
{
lbl_alert_message.text=message;
view_alert.tag=tag;
if (option==1)
{
[btn_alert_OK setHidden:NO];
btn_alert_OK.tag=tag;
[btn_alert_yes setHidden:YES];
[btn_alert_no setHidden:YES];
[lbl_sep_horizontal setHidden:NO];
[lbl_sep_vertical setHidden:YES];
}
else
{
[btn_alert_OK setHidden:YES];
[btn_alert_yes setHidden:NO];
[btn_alert_no setHidden:NO];
[lbl_sep_horizontal setHidden:NO];
[lbl_sep_vertical setHidden:NO];
btn_alert_yes.tag=tag;
}
view_alert_popup.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.001, 0.001);
[UIView animateWithDuration:0.4/1.5 animations:^
{
view_alert_popup.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1.1, 1.1);
} completion:^(BOOL finished)
{
[UIView animateWithDuration:0.4/2 animations:^
{
[rootViewController.view addSubview:self.view];
view_alert_popup.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.9, 0.9);
} completion:^(BOOL finished)
{
[UIView animateWithDuration:0.4/2 animations:^{
view_alert_popup.transform = CGAffineTransformIdentity;
}];
}];
}];
}
-(IBAction)action_alert_yes:(id)sender
{
[self.view removeFromSuperview];
[self.CustomAlertdelegate AlertSuccess:view_alert];
}
-(IBAction)action_alert_hide:(id)sender
{
[self.view removeFromSuperview];
[self.CustomAlertdelegate AlertFailure:view_alert];
}
我已经在我的其他视图控制器中使用了客户警报视图
HomeView.h
@interface HomeScreen :UIViewController <AlertViewprotocal> //CustomAlertView
{
CustomAlertView *alert;
}
HomeView.m
-(void)viewWillAppear:(BOOL)animated
{
alert=[CustomAlertView singleton];
alert.CustomAlertdelegate=self;
}
- (void)viewWillDisappear:(BOOL)animated
{
alert.CustomAlertdelegate=nil;
}
-(IBAction)action_addRemove:(id)sender
{
UIButton *btn=sender;
self.add_row=btn.tag;
[alert showAlertView:self :2 :@"message" :0];
}
- (void)AlertSuccess:(id)result
{
UIView *vw=result;
NSLog(@"Success tag:%d",vw.tag);
switch (vw.tag)
{
case 0:
{
[self remove];
}
break;
case 1:
{
[self add];
}
break;
default:
break;
}
}
- (void)AlertFailure:(id)result
{
UIView *vw=result;
NSLog(@"Failure tag:%d",vw.tag);
}
我明白了
-[__NSArrayM action_alert_yes:]:无法识别的选择器发送到实例 0xc135350 堆栈跟踪:NSInvalidArgumentException
【问题讨论】:
-
您遇到了哪种崩溃?你能添加堆栈跟踪吗?
-
添加一个异常断点并告诉我们它在哪里崩溃
标签: ios objective-c iphone