不可见的ViewContoller(SecondViewController
)会显示UIAlertView
,如下所示:
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];
在ViewController.h中,委托的定义如下:
@property (strong, nonatomic) id<UIAlertViewDelegate> delegate;
但委托方法不会在ViewController.h
中调用。我做错了什么?
答案 0 :(得分:1)
您不需要为第二个viewController定义委托,然后将该委托分配给UIAlertView委托。而是将UIAlertView的委托设置为第二个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];
但是,您需要确保第二个viewController符合UIAlertViewDelegate协议并实现所需的方法。
实施例
@interface SecondViewController : UIViewController <UIAlertViewDelegate>
@end
此外,从第二个viewController中删除此属性,因为它不需要
@property (strong, nonatomic) id<UIAlertViewDelegate> delegate;
。