0

我是目标 c 的新手,并试图掌握委托方法和用法。目前我有 Alertview 类,我需要它来创建另一个类来调用它的函数。我已经设法对 appdelegate 类做同样的事情。但不能用我自己的班级来做。

                TableViewController *newControll = (TableViewController*)[UIApplication sharedApplication].delegate;
                [newControll openSettings];

这就是我试图访问我的方法的方式。编译器在 newControll 中看到了这个方法,但调用它会得到无法识别的选择器。

基本上我需要从另一个类调用之前已经创建的函数。如果这是一个简单的解决方案,我很抱歉,但我还不能很好地掌握委托和目标 c。

也许不是每个人都能得到我需要做的事情,所以我会尝试再解释一次。

我有对象TableViewController。从内部的这个对象中,我正在调用类AlertView来显示一些警报消息。因此,根据警报对话框中的用户交互(密码可以,密码不是),我需要openSettings在我的TableViewController或不调用中调用方法。那么如何做到这一点呢?

4

1 回答 1

1

如果你TableViewController不是你的 appdelegate 类,你应该使用对象TableViewController来使用方法openSettings

它应该是这样的,

TableViewController *newControll = [[TableViewController alloc] init];

假设您要搬到一个新的alertView. 在你的AlertView.h文件中添加,

@property(nonatomic, retain) TableViewController *tableViewController;

在创建新alertView对象时,

AlertView *alertView = [[AlertView alloc] init];
//some code..
alertView.tableViewController = self;

现在在你的AlertView课堂上,称之为,

[self.tableViewController openSettings];

使用 appdelegate 不是这样做的方法。

如果您需要一些有关 iOS 的教程,请查看 raywenderlich

于 2012-11-08T07:50:31.407 回答