【问题标题】:UIAlertView delegate to another classUIAlertView 委托给另一个类
【发布时间】:2013-11-30 13:40:51
【问题描述】:

不可见的 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 中没有调用委托方法。我做错了什么?

【问题讨论】:

  • @Greg 是集成方法[alertView show]
  • 是的,我确实没有注意到这一点。您是否刚刚分配了 viewc 并尝试在其上处理 UIAlertViewDelegate?如果你想处理委托,我相信你应该展示 viewc。

标签: ios iphone objective-c delegates uialertview


【解决方案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&lt;UIAlertViewDelegate&gt; delegate;.

【讨论】:

  • 方法仍然没有被触发。
  • 第二个viewController是否符合UIAlertViewDelegate协议?
  • 是的,两个 ViewController 都可以。
  • 只有第二个 ViewController 需要符合 UIAlertViewDelegate 协议,但实际上应该是呈现的 viewController。无论如何,我刚刚测试了这个,它对我有用。你有任何错误吗?
  • 谢谢你的回答,我现在用NSNotificationCenter解决了
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-12-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多