[英]Subclassing Delegate Issue
我使用以下方法在父类中创建一个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代表已更改为孩子,而不是与父母呆在一起。 每个子级View Controller都说它是委托人,我想为此工作,每个子级VC应该说父级是委托人。 我想念什么?
还:我正在使用情节提要。 这两个Child VC都是嵌入在NavigationController中的控件拖入式推送。 导航控制器>父>子1和子2
从子类创建对象并不意味着也将创建与超类不同的实例。 您只能同时拥有一个对象, 即一个 ChildViewController
和一个SuperViewController
。
当执行上述代码时, self
关键字将引用ChildViewController
的实际实例,该实例同时也是SuperViewController
的实例。
并且,如果将self
关键字记录在子类及其超类中,则会注意到它们实际上指向内存中的同一对象。
PS您可以尝试po (SuperViewController *) [[self mySession] delegate]
,您将获得期望的结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.