iCode
iCode

Reputation: 1466

UIAlertView delegate to another class

A ViewContoller (SecondViewController) which is not visible shows an UIAlertView like this:

ViewController *viewc = [[ViewController alloc]init];
UIAlertView *alert =  [[UIAlertView alloc] initWithTitle:@"Fehler" message:@"Message" delegate:viewc.delegate cancelButtonTitle:@"Ok" otherButtonTitles: nil] ;

[alert performSelectorOnMainThread:@selector(show) withObject:nil waitUntilDone:NO];

In ViewController.h delegate is definded like this:

@property (strong, nonatomic) id<UIAlertViewDelegate> delegate;

But the delegate methods don't get called in ViewController.h. What am I doing wrong?

Upvotes: 0

Views: 897

Answers (1)

Jonathan
Jonathan

Reputation: 2383

You do not need to define a delegate for the second viewController and then assign that delegate to the UIAlertView delegate. Instead set the delegate of the UIAlertView to the second viewController.

ViewController *viewc = [[ViewController alloc]init];
UIAlertView *alert =  [[UIAlertView alloc] initWithTitle:@"Fehler" message:@"Message" delegate:viewc cancelButtonTitle:@"Ok" otherButtonTitles: nil] ;

[alert performSelectorOnMainThread:@selector(show) withObject:nil waitUntilDone:NO];

However, you do need to make sure that the second viewController conforms to the UIAlertViewDelegate protocol and implements the required methods.

Ex.

@interface SecondViewController : UIViewController <UIAlertViewDelegate>

@end

Also, remove this property from the second viewController as it is not needed @property (strong, nonatomic) id<UIAlertViewDelegate> delegate;.

Upvotes: 1

Related Questions