Delegate - how to use?

I think I understand the logic behind the delegate. I am having a problem with this. How many steps are required? Should I use existing delegates? Or can I use one of them?

In my example, I got AppDelegate, which created many views (Object / View Controllers) of the same type. Each view must somehow call the AppDelegate method to close. This will happen when a button in the view is clicked. A method call will include a kind link (self).

So far, I know from other respondents, listeners of events, etc. They are so easy to use.

Can someone help me. I just found massive examples with lots of code on the Internet. It's not easy to just call a parent in Objective C.

+1
source share
3 answers

I think you should use NSNotificationCenter for this

in 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 that will be called when a notification appears (we are still in AppDelegate.m )

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

and in ViewController.m , when the button is pressed (inside the method), you should send a notification as follows:

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

An easy way to get what you want is to simply start with one look. Then take a look at each other's presentation. When the button in the view is pressed, do

[self dismissModalViewControllerAnimated:YES];

And here’s something I did some time ago when I started developing an iPhone that could help you with delegates

 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]; } 
+1
source

You can create your own:

In 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:

 @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:

 #import "MyView1.h" @interface AppDelegate <MyView1Delegate> { MyView1 *_myView1; } ... @end 

In AppDelegate.m:

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

All Articles