obaid
obaid

Reputation: 902

UIAlertView button in action issue

I have declared the UIAlertViewDelegate to call its methods like this way

-(IBAction)hidding{
    [self removeFromParentViewController];
    UIAlertView *alert1= [[UIAlertView alloc] initWithTitle:@"Logged in"
        message:[NSString stringWithFormat:@"Welcomes you"]
        delegate:self
        cancelButtonTitle:@"Ok"
        otherButtonTitles:nil];
    [alert1 show];
}


- (void)alertViewUIAlertView *)actionSheet clickedButtonAtIndexNSInteger)buttonIndex {
    // the user clicked one of the OK/Cancel buttons
    NSLog(@"clicking");
    if (buttonIndex == 0)
    {
        NSLog(@"ok");
    }
    else
    {
        NSLog(@"cancel");
    }
}

To check which button is clicked and perform some action. However When the UIAlertView appears and when I click on OK option it crashes and gives me the error of *"Program received signal: "EXC_BAD_ACCESS"*.

To be more specific I've declared this UIAlertView in 1stclass and then i'm comparing some parameteres in 2ndclass and from 2ndclass it is calling the 1stclass method which has this UIAlertView.

Upvotes: 0

Views: 1573

Answers (2)

obaid
obaid

Reputation: 902

Yes, I used and confirm it However Got the solution, it was like I had to create the @protocol class and declare the -(void)methods in it and then create a delegate in Appdelegate class for it, and also in oneclass. So, I called the @protocol class method and in turn it calls the oneclass method, NOTE: I inherited the @protocol method in oneclass and the issue got resolved. Here is the complete code for the solution this is the @protocol.h class

#import <Foundation/Foundation.h>
 #import <UIKit/UIKit.h>

@protocol SMLoginDelegate 

- (void)didDisconnection;
@end

This is my oneclass

#import "oneclass.h"

@interface oneclass : UIViewController<UITextFieldDelegate,SMLoginDelegate>
{


}
@end

oneclass.m

- (void)viewDidLoad
{

    [super viewDidLoad];

     AppDelegate *del1 = [self appDelegate];
    del1._loginDelegate = self;

    // Do any additional setup after loading the view from its nib.
}


appdelegate.h class
@interface FirstphaseAppDelegate {
__weak NSObject <SMLoginDelegate> *_loginDelegate;
}
@property (nonatomic, weak) id _loginDelegate;

appdelegate.m class

@synthesize _loginDelegate;

-(void)anymethod
{
[_loginDelegate didDisconnection];
}

Upvotes: 0

MCKapur
MCKapur

Reputation: 9157

- (void)alertViewUIAlertView *)actionSheet clickedButtonAtIndexNSInteger)buttonIndex
{
    // the user clicked one of the OK/Cancel buttons
    NSLog(@"clicking");
    if (buttonIndex == 0)
    {
        NSLog(@"ok");
    }
    else
    {
        NSLog(@"cancel");
    }
}

That method looks extremely weird to me. Should be like this:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    // the user clicked one of the OK/Cancel buttons
    NSLog(@"clicking");
    if (buttonIndex == 0)
    {
        NSLog(@"ok");
    }
    else
    {
        NSLog(@"cancel");
    }
}

you messed up the method name....

Upvotes: 1

Related Questions