Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Regarding the issue you are facing, as an immediate fix, instead of keeping 'alert' as a local object, try declaring it as a strong property of the class.</p> <p>But better keep 'customAlert' as subclass of 'UIAlertView' instead of having 'UIAlertView' as a property of customAlert.</p> <p>An example of a custom alert class (Haven't added much comments. Code is simple and self descriptive).</p> <p><strong>CustomAlert.h</strong></p> <pre><code> #import &lt;UIKit/UIKit.h&gt; @protocol customAlertDelegate&lt;NSObject&gt; - (void)pressedOnYES; - (void)pressedNO; @end @interface CustomAlert : UIAlertView - (CustomAlert *)initWithDelegate:(id)delegate; @property (weak) id &lt;customAlertDelegate&gt; delegate1; @end </code></pre> <p><strong>CustomAlert.m</strong></p> <pre><code>#import "CustomAlert.h" @implementation CustomAlert @synthesize delegate1; - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { // Initialization code } return self; } - (CustomAlert *)initWithDelegate:(id)delegate { self = [super initWithTitle:@"Title" message:@"Message" delegate:self cancelButtonTitle:@"No" otherButtonTitles:@"Yes", nil]; if (self) { //Assigning an object for customAlertDelegate self.delegate1 = delegate; } return self; } //Method called when a button clicked on alert view - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { if (buttonIndex) { [self.delegate1 pressedOnYES]; } else { [self.delegate1 pressedNO]; } } @end </code></pre> <p>Your View controller with delegate methods</p> <p><em>ViewController.h</em></p> <pre><code>#import &lt;UIKit/UIKit.h&gt; #import "CustomAlert.h" @interface ViewController : UIViewController &lt;customAlertDelegate&gt; @end </code></pre> <p><em>ViewController.m</em></p> <pre><code>#import "ViewController.h" @implementation ViewController - (IBAction)pressBtn:(id)sender { CustomAlert *alert=[[CustomAlert alloc] initWithDelegate:self] ; [alert show]; } - (void)pressedOnYES { //write code for yes } - (void)pressedNO { //write code for No } @end </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload