自定义Alertview与代表没有故事板

时间:2014-09-14 17:32:15

标签: ios objective-c iphone

我为我的项目设计了自定义alerview。 我使用@protocol为委托创建一个单独的viewcontroller(.h和.m)文件,以获取alertview按钮单击响应。我为视图控制器创建了一个对象(我使用自定义委托创建的自定义alertview)。我在每个其他类中查看自定义alertview。它的工作正常UIAlertview。

但问题出现在我显示alertview然后按主页按钮并返回到我的应用程序时,它会在ios7中冻结并在ios6中崩溃。

我该怎么做才能解决这个问题。当我返回我的应用程序时,它(警报视图)应该正常工作。请提出你的想法来解决这个问题。

我无法找到苹果默认UIAlertview在同一时间的工作方式(dipaly alertview并进入后台并在应用程序中使用所显示的alertview工作正常)。

我不使用UIAlertvie

alert_cupboard = [[UIAlertView alloc] initWithTitle:@“Title”消息:@“message”delegate:self cancelButtonTitle:nil otherButtonTitles:@“Yes”,@“No”,nil];         alert_cupboard.tag = 0;         [alert_cupboard show];

为此,我在My code中设计了创建Custom Alertview

的设计
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];

}

我在其他视图控制器中使用了custome alertview

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

1 个答案:

答案 0 :(得分:-1)

我认为你没有把&#34; nil&#34;自定义警报视图末尾的参数。

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"error" message:@"message" delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:@"otherbutton", nil];

nil是最后一个参数。