简体   繁体   English

代表 - 如何使用?

[英]Delegate - How to Use?

I think I understand the logic behind a delegate. 我想我理解代表背后的逻辑。 I got more the problem to use it. 我使用它的问题更多了。 How many steps are involved? 涉及多少步骤? Do I have to use existing delegates? 我是否必须使用现有代表? Or can I use my one ones? 或者我可以使用我的一个?

In my example I got the AppDelegate that created many views (Objects / View Controllers) of the same type. 在我的例子中,我得到了AppDelegate,它创建了许多相同类型的视图(对象/视图控制器)。 Each view should somehow call a method on the AppDelegate to close itself. 每个视图都应该以某种方式调用AppDelegate上的方法来关闭它自己。 This would happen when a button within the view is touched. 触摸视图中的按钮时会发生这种情况。 The method call would include the reference of the view (self). 方法调用将包括视图的引用(self)。

So far I know from other languages responders, event listeners and so on. 到目前为止,我从其他语言中了解响应者,事件监听器等。 They are so simple to use. 它们使用起来非常简单。

Can anybody help me. 有谁能够帮助我。 I just found massive examples with a lot of code in the web. 我刚刚在网上找到了大量代码,其中包含大量代码。 It can't be that hard to just call a parent in Objective C. 在Objective C中调用父节点并不难。

An easy way to get what you want is to just start with one view. 获得所需内容的简单方法是从一个视图开始。 Then, have each other view be presented modally. 然后,以模态方式呈现每个其他视图。 When the button in the view is pressed do 当按下视图中的按钮时

[self dismissModalViewControllerAnimated:YES];

And here's something I made a while ago when I was starting iPhone development that might help you with delegates 这是我刚刚开始进行iPhone开发时可能会帮助代表们的事情

Delegates

//In parent .m file:
//assign the delegate
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if([segue.identifier isEqualToString:@"segueName"])
    {
        childController *foo = segue.destinationViewController;
        foo.delegate = self;
    }

}

//implement protocol method(s):
- (void) methodName:(dataType*) dataName
{
    //An example of what you could do if your data was an NSDate
    buttonLabel.titleLabel.text = [[date description] substringToIndex:10];
}

//In parent .h file:
//import child header
#import "ChildName.h"

//indicate conformity with protocol
@interface ParentName : UIViewController <ChildNameDelegate>

//In child .h file
//declare protocol
@protocol ChildNameDelegate
- (void) methodName:(dataType*) dataName;
@end

//declare delegate
@property (unsafe_unretained, nonatomic) id<ChildNameDelegate> delegate;


//In child .m file
//synthesize delegate
@synthesize delegate; 

//use method
- (IBAction)actionName:(id)sender 
{
    [delegate methodName:assignedData];
}

You can create your own: 你可以创建自己的:

In MyView1.h: 在MyView1.h中:

@class MyView1;

@protocol MyView1Delegate <NSObject>

- (void)closeMyView1:(MyView1 *)myView1;

@end

@interface MyView1 : NSObject
{
    id<MyView1Delegate> _delegate;
}

@property (assign, nonatomic, readwrite) id<MyView1Delegate> delegate;

...

@end

In MyView1.m: 在MyView1.m中:

@interface MyView1

@synthesize delegate = _delegate;

...

// The method that tells the delegate to close me
- (void)closeMe
{
    ....
    if ([_delegate respondsToSelector:@selector(closeMyView1:)])
    {
        [_delegate closeMyView1:self];
    }
}

@end

In AppDelegate.h: 在AppDelegate.h中:

#import "MyView1.h"

@interface AppDelegate <MyView1Delegate>
{
    MyView1 *_myView1;
}

...

@end

In AppDelegate.m: 在AppDelegate.m中:

- (void)someCreateViewMethod
{
    _myView1 = [[MyView1 alloc] initWithFrame:NSMakeRect(0, 0, 100, 200)];
    [_myView1 setDelegate:self];
    ...
}

I think you should use for this the NSNotificationCenter 我认为你应该使用NSNotificationCenter

in you AppDelegate.m 在你AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
...
...
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(buttonPushed:) name:@"ButtonPushedNotification" object:nil];
}

- (void)applicationWillTerminate:(UIApplication *)application
{
...
...
[[NSNotificationCenter defaultCenter] removeObserver:self];
}

this is the selector it will be called when the notification happens (we are still in the AppDelegate.m ) 这是通知发生时将调用它的选择器(我们仍然在AppDelegate.m中

- (void)buttonPushed:(NSNotification *)notification {
NSLog(@"the button pushed...");
}

and in the ViewController.m when the button pushed (inside the method), you should post a notification like this: 按下按钮(在方法内)时,在ViewController.m中 ,您应该发布如下通知:

{
...
[[NSNotificationCenter defaultCenter] postNotificationName:@"ButtonPushedNotification" object:nil];
...
}

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

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