简体   繁体   English

无法使用委托

[英]Unable to use delegate

I'm new to objective c and trying to grasp the delegate methology and usage. 我是目标c的新手,正在尝试掌握委托人的方法和用法。 Currently i have Alertview class, and i need it to make a another class to call function of its. 目前,我有Alertview类,并且我需要它来制作另一个类来调用其功能。 I'v managed to do the same with with appdelegate class. 我设法用appdelegate类做同样的事情。 But can't do it with my own class. 但是我自己的班子做不到。

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

That's how I'm trying to access my method. 这就是我试图访问我的方法的方式。 Compilator see's this method in newControll, yet calling this gets unrecognizet selector. 编译器在newControll中看到此方法,但调用此方法将导致无法识别选择器。

Basicaly i need to call a function of which was already created earlier, from another class. 基本上,我需要从另一个类调用一个早已创建的函数。 I'm sorry if this is a simple solution, but i just can't grasp delegate and objective-c quite well yet. 很抱歉,如果这是一个简单的解决方案,但我还不能很好地理解委托和Objective-C。

Maybe not everyone get's what I need to do, so i'll try to explain once more. 也许不是每个人都能得到我需要做的,所以我将尝试再次解释。

I have object TableViewController . 我有对象TableViewController From this object inside I'm calling class AlertView to display some alert message. 我从这个对象里面调用AlertView类来显示一些警报消息。 And accordingly to user interaction in alert dialogs (password is ok, password isn't) I need to call method openSettings in my TableViewController or not call. 因此,根据警报对话框中的用户交互(密码确定,密码无效),我需要在TableViewController调用方法openSettings或不调用。 So how to do this? 那么该怎么做呢?

If your TableViewController is not your appdelegate class, you should use the object of TableViewController to use the method openSettings . 如果您的TableViewController不是您的appdelegate类,则应该使用TableViewController的对象来使用openSettings方法。

It should be something like, 应该是这样的

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

Assuming that you are moving to a new alertView . 假设您要移至新的alertView In your AlertView.h file add, 在您的AlertView.h文件中,

@property(nonatomic, retain) TableViewController *tableViewController;

And while creating the new alertView object, 在创建新的alertView对象时,

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

Now in your AlertView class, call it as, 现在在您的AlertView类中,将其命名为

[self.tableViewController openSettings];

Usage of appdelegate is not the way to do it. 无法使用appdelegate。

If you need some tutorials on iOS, check raywenderlich . 如果您需要iOS上的一些教程, 请查看raywenderlich

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM