1

I have created a custom alert view in which I created custom buttons and removed the default buttons. When I click the custom buttons, the alert view does not disappear. Please help me solve this problem. I am using the following code,

UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:nil message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:nil];
[alertView show];
UILabel *lbladdblock=[[UILabel alloc] initWithFrame:CGRectMake(100,25,100,30)];
[alertView addSubview:lbladdblockname];
sathiamoorthy
  • 1,488
  • 1
  • 13
  • 23
Anupama
  • 373
  • 2
  • 3
  • 17
  • Share your code here. – sathiamoorthy Feb 13 '14 at 07:20
  • UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:nil message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:nil]; [alertView show]; UILabel *lbladdblock=[[UILabel alloc] initWithFrame:CGRectMake(100,25,100,30)]; [alertView addSubview:lbladdblockname]; – Anupama Feb 13 '14 at 07:23
  • Show the code when you tap on the button. – KudoCC Feb 13 '14 at 07:28
  • Where do you declare and use the custom buttons? – Jeremy Feb 13 '14 at 07:31
  • -(void)gotoWelcomeScreen{ // profileInfoViewController *profile= [self.storyboard instantiateViewControllerWithIdentifier:@"profileInfo"]; scrollingViewController *profile= [self.storyboard instantiateViewControllerWithIdentifier:@"scroller"]; profile.userImagePath=regUserPhoto; profile.phonenum=regPhNumber; profile.selfregistrationId=[NSString stringWithFormat:@"%d",registeredid ]; [self presentModalViewController:profile animated:YES]; } – Anupama Feb 13 '14 at 07:46
  • the view is transferred when i clickthe label but alert view is not dis appear – Anupama Feb 13 '14 at 07:47
  • If the cancel button and other button, both are nil, how do you plan to make the alert view disappear? – iphondroid Feb 13 '14 at 08:15
  • i want to change design of the button so i create custom button – Anupama Feb 13 '14 at 08:36
  • https://github.com/wimagguc/ios-custom-alertview – Bhavesh Lakum Feb 13 '14 at 11:15

3 Answers3

1

1.include your .h file : UIAlertViewDelegate

2.please follow below implementation...

UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:nil message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:nil]; 
[alertView show];
UILabel *lbladdblock=[[UILabel alloc] initWithFrame:CGRectMake(100,25,100,30)];
[alertView addSubview:lbladdblockname];    
[self performSelector:@selector(dismiss:) withObject:alert1 afterDelay:1.0];

the dismiss method will be...

-(void)dismiss:(UIAlertView*)alert
{
    [alert dismissWithClickedButtonIndex:0 animated:YES];
}

Or you wants to custom view than use

 UIAlertView*testAlert=[[UIAlertView alloc] initWithFrame:CGRectMake(0, 0, 140, 140)];


UIButton*testButton=[UIButton buttonWithType:UIButtonTypeRoundedRect];
[testButton setFrame:CGRectMake(20, 20, 40, 40)];

[testButton setTitle:@"Hello" forState:UIControlStateNormal];
[testButton addTarget:self action:@selector(someAction) forControlEvents:UIControlEventTouchUpInside];
[testAlert addSubview:testButton];
 [testAlert show];

-(void)someAction

{
[testAlert removeFromSuperView];
}
Gajendra Rawat
  • 3,673
  • 2
  • 19
  • 36
1

If your class is subclass of UIAlertView then you can dismiss it by calling this method.

[alert dismissWithClickedButtonIndex:0 animated:TRUE];

you can use self in place of alert if you are using this method inside of class.

Rajesh Choudhary
  • 1,340
  • 8
  • 12
1

To work around this issue, you do like this:

define alertView in header, then:

- (void)viewDidLoad 
{
    alertView = [[UIAlertView alloc] initWithTitle:nil message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:nil];

    UILabel *lbladdblock=[[UILabel alloc] initWithFrame:CGRectMake(0,0,20,30)];
    [lbladdblock setText:@"custom Message"];

    UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [btn setTitle:@"Dismiss!" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(dismissMe) forControlEvents:UIControlEventTouchUpInside];

    [alertView addSubview:lbladdblock];
    [alertView addSubview:btn];

    [alertView show];
}

 -(void)dismissMe
{
    [alertview dismissWithClickedButtonIndex:0 animated:YES];
}

Also, AFAIK addSubView to alertView is impossible in ios7 so your code will work for previous versions only. See this thread.

Since you are not using built in features of UIAlertView (passing nil in all params)You should prefer some custom alert view. See the sample alert views in link.

Hope it helps!

Community
  • 1
  • 1
NeverHopeless
  • 11,077
  • 4
  • 35
  • 56