简体   繁体   中英

Subclassing Delegate Issue

I create an MCSession in a parent class with this:

- (void) setUpMultipeer{
    //  Setup peer ID
    self.myPeerID = [[MCPeerID alloc] initWithDisplayName:[UIDevice currentDevice].name];

    //  Setup session
    self.mySession = [[MCSession alloc] initWithPeer:self.myPeerID];
    self.mySession.delegate = self;

    //  Setup BrowserViewController
    self.browserVC = [[MCBrowserViewController alloc] initWithServiceType:@"chat" session:self.mySession];
    self.browserVC.delegate = self;

    //  Setup Advertiser
    self.advertiser = [[MCAdvertiserAssistant alloc] initWithServiceType:@"chat" discoveryInfo:nil session:self.mySession];
    [self.advertiser start];
}

I have two child classes that are each subclasses of the parent class. I try to log the number of connected peers in each subclass, both return 0. In addition, when logging self.mySession I get this:

mySession:<MCSession: 0x15d7aae0 MyPeerID = <MCPeerID: 0x15d7b360 DisplayName = Eric's iPhone> SecurityIdentity = (null) EncryptionPreference = Optional ConnectedPeers = (
) Delegate = <ChildViewController: 0x15d867f0>>

I never set the delegate to the child, but it appears to think the MCSession Delegate has been changed to the child instead of staying with the parent. Each Child View Controller says it is the delegate, I would think for this to work each child VC should say the parent is the delegate. What am I missing?

ALSO: I'm using storyboard. Both Child VC's are control-dragged push segues embedded in a NavigationController. NavController > Parent >Child 1 & Child 2

Creating an object from a subclass doesn't mean a separate instance from the super class will be created too. You'll only have one object which is a ChildViewController and a SuperViewController at the same time.

When the above code is executed, the self keyword references to the actual instance of ChildViewController which is also, at the same time, an instance of SuperViewController too.

And if you log the self keyword in the subclass and its super class, you will notice that they actually point to the same object in memory.

PS You can try po (SuperViewController *) [[self mySession] delegate] and you'll get what you expect.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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