如何在AppDelegate iPhone应用程序中使用@protocol? [英] How to use @protocol in AppDelegate iPhone app?

查看:44
本文介绍了如何在AppDelegate iPhone应用程序中使用@protocol?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用5个屏幕的iPhone应用程序.我想在UITabBarController中刷新屏幕4中的值.我在AppDelegate中添加了@protocol,但没有调用.这是我第一次使用@protocol,请您帮我解决这个问题,

I am working in iPhone app with 5 screens. I want to refresh the values in the screen 4th in UITabBarController. I have added @protocol in AppDelegate but it is not calling. This is the first time am using @protocol could you please help me to solve this issue,

AppDelegate.h

@protocol ReloadViewControllerDelegate <NSObject>

-(void) refreshViewController:(NSString *)result;

@end

id refreshViewControllerDelegate;

@property (nonatomic, retain) id refreshViewControllerDelegate;

,我已经综合了.

AppDelegare.m

@synthesize refreshViewControllerDelegate;

if ([refreshViewControllerDelegate conformsToProtocol:@protocol(ReloadViewControllerDelegate)]) 
{
   [refreshViewControllerDelegate performSelectorOnMainThread:@selector(refreshViewController:) withObject:@"YES" waitUntilDone:NO];                
}// Control not come inside of if Condition.... From here i want to update the fourthViewController..

但是控件不能放在if条件内.您能指导我哪里做错了吗?

But control not go inside of the if condition. Could you please guide me where am doing wrong?

在我的第4个ViewController.h

#import "AppDelegate"

@interface fourthViewController : UIViewController <ReloadViewControllerDelegate>

在我的第4个ViewController.m

-(void) refreshViewController:(NSString *)result
{
    NSLog(@"Result : %@", result);
}

任何人都可以帮助我做到这一点吗?预先感谢.

Can anyone please help me to do this? Thanks in advance.

推荐答案

您正在调用以下代码:

if ([refreshViewControllerDelegate conformsToProtocol:@protocol(ReloadViewControllerDelegate)]) 

但是 refreshViewControllerDelegate 是这样的:

id refreshViewControllerDelegate;

conformsToProtocol 检查对象是否声明它符合协议,而您的协议则不符合.如果要指定对协议的符合性,则需要:

conformsToProtocol checks to see if the object declares that it conforms to the protocol, which yours does not. If you want to specify conformity to a protocol you need to:

id<ReloadViewControllerDelegate> refreshViewControllerDelegate;

编辑

好,关于 performSelectorOnMainThread 问题...该方法在NSThread的类别中提供,并且未在NSObject协议中声明.因此,如果要调用它,则需要将您的类型声明为符合您的协议的NSObject.

OK, on the performSelectorOnMainThread problem... That method is provided in a category for NSThread, and is not declared in the NSObject protocol. So, if you want to call that, then you need to declare your type as NSObject, which conforms to your protocol.

NSObject<ReloadViewControllerDelegate> refreshViewControllerDelegate;

编辑

好的,看来这不是关于使用协议的简单问题,而是完整的教程.由于SO并不是这样的地方,所以我将尝试简要介绍一下...

OK, it looks like this is not a simple question about using a protocol, but a full tutorial. Since SO isn't the place for such, I'll try to give a brief one...

协议是接口声明.

@protocol ReloadChatViewControllerDelegate <NSObject>
- (void)refreshViewController:(NSString *)result;
@end

这表示镇上有一个新协议,名称为 ReloadChatViewControllerDelegate ,它也符合 NSObject 协议.任何采用新协议的类都必须提供 refreshViewController 的实现.您可以通过放置 @optional 部分使协议方法为可选.

That says there is a new protocol in town, with the name ReloadChatViewControllerDelegate and it also conforms to the NSObject protocol. Any class that adopts the new protocol must provide an implementation of refreshViewController. You can make a protocol method optional, by putting in an @optional section.

@protocol ReloadChatViewControllerDelegate <NSObject>
- (void)refreshViewController:(NSString *)result;
@optional
- (void)optRefresh;
@end

现在,让我们将协议的采用留待以后使用.假设您正在编写通用代码,并且只想知道给定的对象是否符合协议,如果是,则在其上调用方法.像...

Now, let's leave the adoption of the protocol for later. Say you are writing generic code, and you just want to know if the object you are given conforms to the protocol, and if so, invoke a method on it. Something like...

@interface Bar : NSObject
@property (nonatomic, weak) NSObject<ReloadChatViewControllerDelegate>  *refreshViewControllerDelegate;
- (void)blarg;
@end

现在,Bar类提供了一个委托属性,以便可以给它提供一些对象来帮助它完成一些工作.但是,该委托对象必须至少是 NSObject ,并符合 ReloadChatViewControllerDelegate 协议.

Now, the Bar class is providing a delegate property, so that it can be give some object that will help it do some work. However, that delegate object must at least be an NSObject, and conform to the ReloadChatViewControllerDelegate protocol.

现在,ObjC(和C)非常宽松,因此您可以将对象强制为所需的任何类型,但随后您应该得到崩溃.现在,当调用 blarg 时,将通知委托人做一些工作.

Now, ObjC (and C) is quite permissive, so you can force an object to be any type you want, but then you deserve the crash you get. Now, when blarg is called, the delegate is notified to do some work.

由于委托的属性类型已经表明它符合给定的协议,因此无需检查是否符合要求.我们可以只调用委托方法.请注意,我们必须查看对象是否实现了任何可选的协议方法.

Since the property type of the delegate already says it conforms to the given protocol, there is no need to check for conformity. We can just call the delegate method. Note that we must see if the object implements any optional protocol methods.

@implementation Bar
@synthesize refreshViewControllerDelegate = _refreshViewControllerDelegate;
- (void)blarg {
    // Do something, then invoke the delegate
    [self.refreshViewControllerDelegate
        performSelectorOnMainThread:@selector(refreshViewController:)
                         withObject:@"YES"
                      waitUntilDone:NO];
    if ([self.refreshViewControllerDelegate respondsToSelector:@selector(optRefresh)]) {
        [self.refreshViewControllerDelegate optRefresh];
    }
}
@end

但是,如果您想成为通用对象,并接受任何对象作为委托(也许您希望使委托符合某些给定协议的要求成为可选项),那么您可以接受普通的 id ,然后检查它是否符合要求.在这种情况下,您可以将您的委托声明为一个id(或其他类型).

However, if you want to be generic, and accept any object as a delegate (maybe you want to make it optional that the delegate conforms to some given protocol), then you can accept a plain id and then check to see it it conforms. In that case, you could declare your delegate as just an id (or some other type).

@property (nonatomic, weak) id refreshViewControllerDelegate;

现在,在您的代码中,您需要检查一致性.

Now, in your code, you need to check for conformity.

- (void)blarg {
    // Do something, then invoke the delegate
    if ([self.refreshViewControllerDelegate
            conformsToProtocol:@protocol(ReloadChatViewControllerDelegate)]) {
        [self.refreshViewControllerDelegate
            performSelectorOnMainThread:@selector(refreshViewController:)
                             withObject:@"YES" waitUntilDone:NO];
        if ([self.refreshViewControllerDelegate
                respondsToSelector:@selector(optRefresh)]) {
            [self.refreshViewControllerDelegate optRefresh];
        }
    }
}

好的,现在您已经定义了协议,并且您有代码可以调用协议中的方法.两个警告.

OK, now you have a protocol defined, and you have code that calls methods on the protocol. Two caveats.

首先,必须将委托设置为一个对象. nil 将对任何方法响应 false ,因此,它当然不符合要求,在发送任何消息时也不做任何事情.

First, the delegate has to be set to an object. nil will respond false for any method, so it will of course not conform, nor do anything when sent any message.

第二,您必须确保您的委托人声明符合协议.仅实现方法不符合.如果一个类没有明确指定符合协议,则 conformsToProtocol 将返回false,即使它实现了协议的方法.

Second, you have to make sure that your delegate declares conformity to the protocol. Just implementing the methods is not conformity. If a class does not explicitly specify that is conforms to a protocol, then conformsToProtocol will return false, even if it implements the methods of the protocol.

因此,让我们指定一些符合协议的类作为我们的委托.

So, let's specify some class that will act as our delegate by conforming to the protocol.

@interface Foo : NSObject<ReloadChatViewControllerDelegate>
- (void)refreshViewController:(NSString *)result;
@end
@implementation Foo
- (void)refreshViewController:(NSString *)result {
    NSLog(@"Look, ma, I'm refreshed with %@", result);
}
@end

它符合协议,提供了强制方法的实现,并省略了可选方法.

It conforms to the protocol, provides an implementation for the mandatory method, and omits the optional one.

现在,如果您运行此代码,则应该在所有出色的代码中看到该奇妙的代码.

Now, if you ran this code, you should see that marvelous code in all its splendor.

Foo *foo = [[Foo alloc] init];
Bar *bar = [[Bar alloc] init];
bar.refreshViewControllerDelegate = foo;
[bar blarg];

这篇关于如何在AppDelegate iPhone应用程序中使用@protocol?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆