0

我用这个在父类中创建一个 MCSession:

- (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];
}

我有两个子类,它们都是父类的子类。我尝试记录每个子类中连接的对等点的数量,都返回 0。此外,在记录时self.mySession我得到这个:

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

我从未将代表设置为孩子,但似乎认为 MCSession 代表已更改为孩子,而不是与父母呆在一起。每个子视图控制器都说它是委托,我认为为此每个子 VC 都应该说父是委托。我错过了什么?

另外:我正在使用情节提要。两个子 VC 都是嵌入在 NavigationController 中的控制拖动推送序列。导航控制器 > 父级 > 孩子 1 和孩子 2

4

1 回答 1

0

从子类创建对象并不意味着也将创建来自超类的单独实例。您将只有一个对象同时是 a ChildViewController和 a SuperViewController

执行上述代码时,self关键字引用的实际实例ChildViewController也是,同时也是SuperViewControllertoo的实例。

如果你self在子类和它的超类中记录关键字,你会注意到它们实际上指向内存中的同一个对象。

PS你可以试试po (SuperViewController *) [[self mySession] delegate],你会得到你所期望的。

于 2014-03-03T21:55:44.280 回答