How to perform Callbacks in Objective-C

后端 未结 5 1353
无人及你
无人及你 2020-11-27 09:09

How to perform call back functions in Objective-C?

I would just like to see some completed examples and I should understand it.

5条回答
  •  盖世英雄少女心
    2020-11-27 09:44

    Here's an example that keeps the concepts of delegates out, and just does a raw call back.

    @interface Foo : NSObject {
    }
    - (void)doSomethingAndNotifyObject:(id)object withSelector:(SEL)selector;
    @end
    
    @interface Bar : NSObject {
    }
    @end
    
    @implementation Foo
    - (void)doSomethingAndNotifyObject:(id)object withSelector:(SEL)selector {
        /* do lots of stuff */
        [object performSelector:selector withObject:self];
    }
    @end
    
    @implementation Bar
    - (void)aMethod {
        Foo *foo = [[[Foo alloc] init] autorelease];
        [foo doSomethingAndNotifyObject:self withSelector:@selector(fooIsDone:)];
    }
    
    - (void)fooIsDone:(id)sender {
        NSLog(@"Foo Is Done!");
    }
    @end
    

    Typically the method -[Foo doSomethingAndNotifyObject:withSelector:] would be asynchronous which would make the callback more useful than it is here.

提交回复
热议问题