如何使用 iOS 中的委派作为滑出菜单?
How do I use delegation in iOS for slide out menu?
我正在尝试弄清楚 iOS 中的授权。基本上,我有 classA
,其中包含 methodA
。我也有 classB
,我想从中调用 methodA
。
具体来说,我有一个名为 ViewControllerRootHome
的 class 和一个名为 ViewControllerRootHomeLeftPanel
的 class。 ViewControllerRootHome
中有一个方法叫做 movePanelToOriginalPosition
我想从 ViewControllerRootHomeLeftPanel
class.
调用这个方法
如有任何帮助,我们将不胜感激。哦,忘了说我还在为这个项目使用 Objective-C。
我会尝试一下。
假设您有一个 ViewController 叫做 ViewControllerA,另一个 ViewController 叫做 ViewControllerB。我们想从 B 中调用 A 中的一个方法。我们将如何实现这一目标?
简单。我们将在 B 中 定义 一个协议,A 将遵守该协议。让我在这里做。
#import ...
@protocol myProtocol; // Declare Protocol
@interface ViewControllerB : UIViewController
@property (nonatomic, weak)id <myProtocol> myDelegate; // Create Delegate property
@end // Notice this is AFTER the @end of the @interface declaration
@protocol myProtocol <NSObject> // Define Protocol
-(void)doSomething;
@end
好的,现在您已经定义了一个名为 myProtocol 的协议,您希望在 ViewControllerA[=15 中使用它=]
让我们在那里使用它。我们将不得不做几件事:第一,遵守协议。第二,将我们当前的 VC 设置为代表!
#import ...
#import "ViewControllerB" // IMPORT the VC with the Protocol
@interface ViewControllerA : UIViewController <myProtocol> // Conform to Protocl
@property (nonatomic)ViewControllerB *viewControllerB;
@end
注意我已经定义了 属性 类型 ViewControllerB。您需要以某种形式引用 ViewControllerB。这通常很容易实现,因为您通常会从 ViewControllerA 创建一个 ViewControllerB 的实例。否则它将需要在外部设置或在初始化时传递给 ViewControllerA 并在那里将其设置为 属性。
在 ViewControllerA.m 中,将 ViewControllerA 设置为 delegate:
self.ViewControllerB.myDelegate = self;
现在,您所要做的就是在 ViewControllerA 中定义协议中的方法,这样它就可以被调用:
-(void)doSomething
{
...
}
这就是您需要做的全部。但是,请注意 如果您有两个 ViewController 遵守彼此的协议,您可能必须在它们自己的头文件中声明协议。
编辑:如何调用方法。
如果要调用协议内部定义的方法。您将在 ViewControllerB 内这样做,如下所示:
if ([self.myDelegate respondsToSelector:@selector(doSomething)])
{
[self.myDelegate doSomething];
}
我正在尝试弄清楚 iOS 中的授权。基本上,我有 classA
,其中包含 methodA
。我也有 classB
,我想从中调用 methodA
。
具体来说,我有一个名为 ViewControllerRootHome
的 class 和一个名为 ViewControllerRootHomeLeftPanel
的 class。 ViewControllerRootHome
中有一个方法叫做 movePanelToOriginalPosition
我想从 ViewControllerRootHomeLeftPanel
class.
如有任何帮助,我们将不胜感激。哦,忘了说我还在为这个项目使用 Objective-C。
我会尝试一下。
假设您有一个 ViewController 叫做 ViewControllerA,另一个 ViewController 叫做 ViewControllerB。我们想从 B 中调用 A 中的一个方法。我们将如何实现这一目标?
简单。我们将在 B 中 定义 一个协议,A 将遵守该协议。让我在这里做。
#import ...
@protocol myProtocol; // Declare Protocol
@interface ViewControllerB : UIViewController
@property (nonatomic, weak)id <myProtocol> myDelegate; // Create Delegate property
@end // Notice this is AFTER the @end of the @interface declaration
@protocol myProtocol <NSObject> // Define Protocol
-(void)doSomething;
@end
好的,现在您已经定义了一个名为 myProtocol 的协议,您希望在 ViewControllerA[=15 中使用它=]
让我们在那里使用它。我们将不得不做几件事:第一,遵守协议。第二,将我们当前的 VC 设置为代表!
#import ...
#import "ViewControllerB" // IMPORT the VC with the Protocol
@interface ViewControllerA : UIViewController <myProtocol> // Conform to Protocl
@property (nonatomic)ViewControllerB *viewControllerB;
@end
注意我已经定义了 属性 类型 ViewControllerB。您需要以某种形式引用 ViewControllerB。这通常很容易实现,因为您通常会从 ViewControllerA 创建一个 ViewControllerB 的实例。否则它将需要在外部设置或在初始化时传递给 ViewControllerA 并在那里将其设置为 属性。
在 ViewControllerA.m 中,将 ViewControllerA 设置为 delegate:
self.ViewControllerB.myDelegate = self;
现在,您所要做的就是在 ViewControllerA 中定义协议中的方法,这样它就可以被调用:
-(void)doSomething
{
...
}
这就是您需要做的全部。但是,请注意 如果您有两个 ViewController 遵守彼此的协议,您可能必须在它们自己的头文件中声明协议。
编辑:如何调用方法。 如果要调用协议内部定义的方法。您将在 ViewControllerB 内这样做,如下所示:
if ([self.myDelegate respondsToSelector:@selector(doSomething)])
{
[self.myDelegate doSomething];
}